首页 新闻 会员 周边 捐助

关于.net连接mysql的问题:connection连接成功 MySqlDataAdapter File有时为空

0
悬赏园豆:30 [已关闭问题] 关闭于 2012-03-30 09:47

 做项目时遇到的一个问题。 有一个功能是导入数据。 我就用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 = "";
                }

不不点点的主页 不不点点 | 初学一级 | 园豆:65
提问于:2012-02-07 11:11
< >
分享
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册