做项目时遇到的一个问题。 有一个功能是导入数据。 我就用MySqlDataAdapter(Update方法)实现的。 当导入完成时,这时候如果在访问数据库 connection连接也能成功。直接执行sql语句也行。 就是File DataSet后,DataSet仍然是空。 sql语句也没有毛病。 只是在导入之后会出现这个问题。很纳闷
下面方法是读数据的
public static DataSet Query(string SQLString)
{
using (MySqlConnection connection = new MySqlConnection(connectionString))
{
DataSet ds = new DataSet();
try
{
connection.Open();
MySqlDataAdapter command = new MySqlDataAdapter(SQLString, connection);
command.Fill(ds, "tempds");
}
catch (MySql.Data.MySqlClient.MySqlException ex)
{
throw new Exception(ex.Message);
}
return ds;
}
}
------------------------------------------------------------------------------
下面是导入数据的
foreach (DataTable item in ds.Tables)
{
tempError = item.TableName;
DataSet tempds = new DataSet();
DataTable temptable = item.Copy();
tempds.Tables.Add(temptable);
string tableName = item.TableName;
using (MySqlConnection connection = new MySqlConnection(DBUtility.DbHelperMySQL.connectionString))
{
MySqlCommand myCommand1 = new MySqlCommand("delete from " + tableName, connection);
MySqlCommand myCommand = new MySqlCommand("select * from " + tableName, connection);
connection.Open();
int number = myCommand1.ExecuteNonQuery();
MySqlDataAdapter myAdapter = new MySqlDataAdapter(myCommand);
MySqlCommandBuilder myCommandBuilder = new MySqlCommandBuilder(myAdapter);
myAdapter.UpdateCommand = myCommandBuilder.GetUpdateCommand();
myAdapter.Update(tempds, tableName);
connection.Close();
}
tempError = "";
}