日期:2014-05-18  浏览次数:21879 次

C# 如何将dataGridView1里的数据批量插入、更新到SQL数据库
1、由于有大量数据,怎么考虑事务的处理?
2、dataGridView1编辑时未绑定数据源,且当记录未满屏时,有相应多的空白行填充,是否插入或更新时不考虑这些空白行?
3、当更改dataGridView1里的数据时,如果某些行没有修改,未修改的行是否可以不再重复向数据库提交写入或更新操作?
4、听说可能用SqlDataAdapter操作,却未找到相关文档,能否实现上面说的功能?




------解决方案--------------------
SqlCommandBuilder 类
------解决方案--------------------
C# code

/// <summary>
        /// 执行多条SQL语句,实现数据库事务。
        /// </summary>
        /// <param name="SQLStringList">多条SQL语句</param>        
        public static int ExecuteSqlTran(List<String> SQLStringList)
        {
            using (MySqlConnection conn = new MySqlConnection(connectionString))
            {
                conn.Open();
                MySqlCommand cmd = new MySqlCommand();
                cmd.Connection = conn;
                MySqlTransaction tx = conn.BeginTransaction();
                cmd.Transaction = tx;
                try
                {
                    int count = 0;
                    for (int n = 0; n < SQLStringList.Count; n++)
                    {
                        string strsql = SQLStringList[n];
                        if (strsql.Trim().Length > 1)
                        {
                            cmd.CommandText = strsql;
                            count += cmd.ExecuteNonQuery();
                        }
                    }
                    tx.Commit();
                    return count;
                }
                catch
                {
                    tx.Rollback();
                    return 0;
                }
            }
        }

------解决方案--------------------
更新
DataSet ds = new DataSet();
SqlDataAdapter sda;

SqlCommandBuilder scb = new SqlCommandBuilder(sda);
sda.Update(ds);
this.dataGridView1.DataSource = ds.Tables[0];
事务使用
TransactionScope scope = new 
TransactionScope()) {}
------解决方案--------------------
探讨
更新
DataSet ds = new DataSet();
SqlDataAdapter sda;

SqlCommandBuilder scb = new SqlCommandBuilder(sda);
sda.Update(ds);
this.dataGridView1.DataSource = ds.Tables[0];
事务使用
TransactionScope scop……

------解决方案--------------------
可以考虑
transaction = connection.BeginTransaction();
try{
SqlCommandBuilder.GetDeleteCommand().ExecuteNonQuery();
SqlCommandBuilder.GetUpdateCommand().ExecuteNonQuery();
SqlBulkCopy.WriteToServer(DataTable,DataRowState.Added);
catch{transaction.Rollback();}
finally{connection.Close();}
------解决方案--------------------
额~~~少个transaction.Commit();
------解决方案--------------------
protected void GridView1_RowUpdating1(object sender, GridViewUpdateEventArgs e)
{
string c_id = GridView1.DataKeys[e.RowIndex].Value.ToString();

string c_user = ((TextBox)(GridView1.Rows[e.RowIndex].Cells[2].Controls[0])).Text.ToString();
string c_stardate = ((TextBox)(GridView1.Rows[e.RowIndex].Cells[3].Controls[0])).Text.ToString();
string c_costName = ((TextBox)(GridView1.Rows[e.RowIndex].Cells[4].Controls[0])).Text.ToString();
string c_price = ((TextBox)(GridView1.Rows[e.RowIndex].Cells[5].Controls[0])).Text.ToString();
string c_real = ((TextBox)(GridView1.Rows[e.RowIndex].Cells[6].Controls[0])).Text.ToString();
string c_costMoney = ((TextBox)(GridView1.Rows[e.RowIndex].Cells[7].Controls[0])).Text.ToString();
string c_userName = ((TextBox)(GridView1.Rows[e.RowIndex].Cells[8].Controls[0])).Text.ToString();
string c_remarks = ((TextBox)(GridView1.Rows[e.RowIndex].Cells[9].Controls[0])).T