调用存储过程的数据集,如何用SqlDataAdapter实现批量更新 GetChanges方法本身就获得所更新的行。 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 ds.GetChanges(DataRowState.Modified).Tables[0].Rows.Count这句应该提到for外,比如:DataSet tempSet=ds.GetChanges(DataRowState.Modified);DataTable table=tempSet.Tables[0];int count=table.Count;for(int i=0;i<count;i++){DataRow= table.Rows[0];.........}代码估计就优化到这,再慢,把Update语句中的Where条件在数据库中设为表的索引 谢谢,你这种方法确实比我现在的写法优化很多,用你这个应该会快很多 再慢估计也没办法了,下面我把我现在写的贴出来,现在是一行一行执行更新,上次我也试过FOR循环里组织更新语句,最后一次性执行,不过感觉提升效果不明显,大家觉得呢 int iRows = ds.GetChanges(DataRowState.Modified).Tables[0].Rows.Count; for (int i = 0; i < ds.GetChanges(DataRowState.Modified).Tables[0].Rows.Count; i++) { FInterID = (int)ds.GetChanges(DataRowState.Modified).Tables[0].Rows[i]["FInterID"]; FEntryID = (int)ds.GetChanges(DataRowState.Modified).Tables[0].Rows[i]["FEntryID"]; FDate_FL = ds.GetChanges(DataRowState.Modified).Tables[0].Rows[i]["发料日期"].ToString(); FName_JX = ds.GetChanges(DataRowState.Modified).Tables[0].Rows[i]["机型"].ToString(); FDate_CG = ds.GetChanges(DataRowState.Modified).Tables[0].Rows[i]["采购回复时间"].ToString(); FNote_CG = ds.GetChanges(DataRowState.Modified).Tables[0].Rows[i]["采购备注"].ToString(); FNote_Plan = ds.GetChanges(DataRowState.Modified).Tables[0].Rows[i]["计划备注"].ToString(); FName_CGY = ds.GetChanges(DataRowState.Modified).Tables[0].Rows[i]["采购员(手录)"].ToString(); ; strSQL = "exec pro_bj_InsertData '" + FInterID.ToString() + "','" + FEntryID.ToString() + "','" + FDate_FL + "','" + FName_JX + "','" + FDate_CG + "','" + FNote_CG + "','" + FNote_Plan + "','" + FName_CGY + "'"; conn = new SqlConnection(constr); conn.Open(); SqlCommand sc = new SqlCommand(strSQL, conn); sc.ExecuteNonQuery(); } ds.AcceptChanges(); 假如我要将table里的所有记录写到数据库的表中,能否不用for循环一条条写,可以一次性写入吗? Insert 可以,Update和DELETE不可以System.Data.SqlClient.SqlBulkCopy这个类可以实现最快速的批量插入另外 conn = new SqlConnection(constr); conn.Open();这两句移到for循环以外. Insert 可以,一次性写入怎么写啊?另外这存在一个问题:既然要用一次性写入,那原报表里的数据就需要先删除,否则就造成重复了 其实用你昨天所说的方法我修改程序后感觉执行效率明显提升,应该可以达到要求了 我只是想做得更完善 string strCon = "Server=WRET-MOSY688YVW\\MRGLL;User Id=sa;Pwd=;DataBase=db_17";//定义数据库连接字符串 SqlConnection sqlcon;//声明数据库连接对象 SqlDataAdapter sqlda;//声明数据库桥接器对象 DataSet myds;//声明数据集对象 private void Form1_Load(object sender, EventArgs e) { sqlcon = new SqlConnection(strCon);//实例化数据库连接对象 sqlda = new SqlDataAdapter("select * from tb_PDic", sqlcon);//实例化数据库桥接器对象 myds = new DataSet();//实例化数据集 sqlda.Fill(myds);//填充数据集CodeGo.net dataGridView1.DataSource = myds.Tables[0];//对DataGridView控件进行数据绑定 //设置DataGridView控件中的数据以隔行换色的形式显示 foreach (DataGridViewRow dgvRow in dataGridView1.Rows)//遍历所有行 { if (dgvRow.Index % 2 == 0)//判断是否是偶数行 { //设置偶数行颜色 dataGridView1.Rows[dgvRow.Index].DefaultCellStyle.BackColor = Color.LightGoldenrodYellow; } else//奇数行 { //设置奇数行颜色 dataGridView1.Rows[dgvRow.Index].DefaultCellStyle.BackColor = Color.LightSeaGreen; } } } //执行批量更新操作 private void button1_Click(object sender, EventArgs e) { myds.Tables.Clear();//清空数据集 sqlcon = new SqlConnection(strCon);//实例化数据库连接对象 sqlda = new SqlDataAdapter("select * from tb_PDic", sqlcon);//实例化数据库桥接器对象 //给SqlDataAdapter的UpdateCommand属性指定执行更新操作的SQL语句 sqlda.UpdateCommand = new SqlCommand("update tb_PDic set Name=@name,Money=@money where ID=@id", sqlcon); //添加参数并赋值 sqlda.UpdateCommand.Parameters.Add("@name", SqlDbType.VarChar, 20, "Name"); sqlda.UpdateCommand.Parameters.Add("@money", SqlDbType.VarChar,9, "Money"); SqlParameter prams_ID = sqlda.UpdateCommand.Parameters.Add("@id", SqlDbType.Int); prams_ID.SourceColumn = "id";//设置@id参数的原始列 prams_ID.SourceVersion = DataRowVersion.Original;//设置@id参数的原始值 sqlda.Fill(myds);//填充数据集 //使用一个for循环更改数据集myds中的表中的值 for (int i = 0; i < myds.Tables[0].Rows.Count; i++) { myds.Tables[0].Rows[i]["Name"] = dataGridView1.Rows[i].Cells[1].Value.ToString(); myds.Tables[0].Rows[i]["Money"] = Convert.ToDecimal(dataGridView1.Rows[i].Cells[2].Value); } //调用Update方法提交更新后的数据集myds,并同步更新数据库数据 sqlda.Update(myds); dataGridView1.DataSource = myds.Tables[0];//对DataGridView控件进行数据绑定 } 用C#重画菜单,要求实现Visual studio2008菜单风格 gridview字符太长改成省略号的问题 在winform中 如何屏蔽alt键 c#使用‘母版’后,FindControl的问题 怎么才能通过命令行打开Winform程序(求高手解答) 郁闷:Listbox有数据就是不能显示。 C# winform中的webBrower生成鼠标点击事件 c#中的值传递与引用传递 请问一下,做了一个服务程序,现在有个问题,就是如何设置服务成自启动或者手动? 请指教: 如何删除 指定的数组元素 zedGraph控件 鼠标移动获取坐标 string.format哪些字符要转义
DataSet tempSet=ds.GetChanges(DataRowState.Modified);
DataTable table=tempSet.Tables[0];
int count=table.Count;
for(int i=0;i<count;i++)
{
DataRow= table.Rows[0];
.........
}代码估计就优化到这,再慢,把Update语句中的Where条件在数据库中设为表的索引
int iRows = ds.GetChanges(DataRowState.Modified).Tables[0].Rows.Count;
for (int i = 0; i < ds.GetChanges(DataRowState.Modified).Tables[0].Rows.Count; i++)
{
FInterID = (int)ds.GetChanges(DataRowState.Modified).Tables[0].Rows[i]["FInterID"];
FEntryID = (int)ds.GetChanges(DataRowState.Modified).Tables[0].Rows[i]["FEntryID"];
FDate_FL = ds.GetChanges(DataRowState.Modified).Tables[0].Rows[i]["发料日期"].ToString();
FName_JX = ds.GetChanges(DataRowState.Modified).Tables[0].Rows[i]["机型"].ToString();
FDate_CG = ds.GetChanges(DataRowState.Modified).Tables[0].Rows[i]["采购回复时间"].ToString();
FNote_CG = ds.GetChanges(DataRowState.Modified).Tables[0].Rows[i]["采购备注"].ToString();
FNote_Plan = ds.GetChanges(DataRowState.Modified).Tables[0].Rows[i]["计划备注"].ToString();
FName_CGY = ds.GetChanges(DataRowState.Modified).Tables[0].Rows[i]["采购员(手录)"].ToString(); ;
strSQL = "exec pro_bj_InsertData '" + FInterID.ToString() + "','" + FEntryID.ToString() + "','" + FDate_FL + "','" + FName_JX + "','" + FDate_CG + "','" + FNote_CG + "','" + FNote_Plan + "','" + FName_CGY + "'";
conn = new SqlConnection(constr);
conn.Open();
SqlCommand sc = new SqlCommand(strSQL, conn);
sc.ExecuteNonQuery();
}
ds.AcceptChanges();
conn.Open();
这两句移到for循环以外.
其实用你昨天所说的方法我修改程序后感觉执行效率明显提升,应该可以达到要求了 我只是想做得更完善
SqlConnection sqlcon;//声明数据库连接对象
SqlDataAdapter sqlda;//声明数据库桥接器对象
DataSet myds;//声明数据集对象
private void Form1_Load(object sender, EventArgs e)
{
sqlcon = new SqlConnection(strCon);//实例化数据库连接对象
sqlda = new SqlDataAdapter("select * from tb_PDic", sqlcon);//实例化数据库桥接器对象
myds = new DataSet();//实例化数据集
sqlda.Fill(myds);//填充数据集CodeGo.net
dataGridView1.DataSource = myds.Tables[0];//对DataGridView控件进行数据绑定
//设置DataGridView控件中的数据以隔行换色的形式显示
foreach (DataGridViewRow dgvRow in dataGridView1.Rows)//遍历所有行
{
if (dgvRow.Index % 2 == 0)//判断是否是偶数行
{
//设置偶数行颜色
dataGridView1.Rows[dgvRow.Index].DefaultCellStyle.BackColor = Color.LightGoldenrodYellow;
}
else//奇数行
{
//设置奇数行颜色
dataGridView1.Rows[dgvRow.Index].DefaultCellStyle.BackColor = Color.LightSeaGreen;
}
} }
//执行批量更新操作
private void button1_Click(object sender, EventArgs e)
{
myds.Tables.Clear();//清空数据集
sqlcon = new SqlConnection(strCon);//实例化数据库连接对象
sqlda = new SqlDataAdapter("select * from tb_PDic", sqlcon);//实例化数据库桥接器对象
//给SqlDataAdapter的UpdateCommand属性指定执行更新操作的SQL语句
sqlda.UpdateCommand = new SqlCommand("update tb_PDic set Name=@name,Money=@money where ID=@id", sqlcon);
//添加参数并赋值
sqlda.UpdateCommand.Parameters.Add("@name", SqlDbType.VarChar, 20, "Name");
sqlda.UpdateCommand.Parameters.Add("@money", SqlDbType.VarChar,9, "Money");
SqlParameter prams_ID = sqlda.UpdateCommand.Parameters.Add("@id", SqlDbType.Int);
prams_ID.SourceColumn = "id";//设置@id参数的原始列
prams_ID.SourceVersion = DataRowVersion.Original;//设置@id参数的原始值
sqlda.Fill(myds);//填充数据集
//使用一个for循环更改数据集myds中的表中的值
for (int i = 0; i < myds.Tables[0].Rows.Count; i++)
{
myds.Tables[0].Rows[i]["Name"] = dataGridView1.Rows[i].Cells[1].Value.ToString();
myds.Tables[0].Rows[i]["Money"] = Convert.ToDecimal(dataGridView1.Rows[i].Cells[2].Value);
}
//调用Update方法提交更新后的数据集myds,并同步更新数据库数据
sqlda.Update(myds);
dataGridView1.DataSource = myds.Tables[0];//对DataGridView控件进行数据绑定
}