private void button6_Click(object sender, System.EventArgs e)
{
this.student_conn.Open();
int pos=this.BindingContext[this.dsStudent1,"student"].Position;
this.dsStudent1.Tables["student"].Rows[pos].Delete();
->this.sqldaStudent.Update(this.dsStudent1,"student");
this.dsStudent1.Tables["student"].AcceptChanges();
this.student_conn.Close();
}
在运行以上代码时会出现以下错误:
“未处理的“System.Data.SqlClient.SqlException”类型的异常出现在 system.data.dll 中。
其他信息: 系统错误。”
错误代码行是“->”所指向的行。
我想进行单表的添加,删除和插入操作,我在一些资料上看到要想把对DataSet的修改应用到数据库就要使用SqlDataAdapter的Update方法,但是始终会出现上面的错误,请问在这里是不是应该使用Update方法来向数据库提交DataSet的更改?如果是那么Update方法应该如何使用?
如果有代码也可以贴上来或发到我的信箱:[email protected],谢谢.
{
this.student_conn.Open();
int pos=this.BindingContext[this.dsStudent1,"student"].Position;
this.dsStudent1.Tables["student"].Rows[pos].Delete();
->this.sqldaStudent.Update(this.dsStudent1,"student");
this.dsStudent1.Tables["student"].AcceptChanges();
this.student_conn.Close();
}
在运行以上代码时会出现以下错误:
“未处理的“System.Data.SqlClient.SqlException”类型的异常出现在 system.data.dll 中。
其他信息: 系统错误。”
错误代码行是“->”所指向的行。
我想进行单表的添加,删除和插入操作,我在一些资料上看到要想把对DataSet的修改应用到数据库就要使用SqlDataAdapter的Update方法,但是始终会出现上面的错误,请问在这里是不是应该使用Update方法来向数据库提交DataSet的更改?如果是那么Update方法应该如何使用?
如果有代码也可以贴上来或发到我的信箱:[email protected],谢谢.
解决方案 »
- StackOverflowExcep怎么处理?
- IE代理如何取消?
- 请问这个问题在什么地方?
- 哪位大哥有Asp.net(C#)开发的OA办公系统,传给小弟一下!现给100分!!!!!!!!!!!
- Visual Studio 2003(.Net)里面有些类的方法和属性在2005的版本中没有定义!??
- 用DataTable.NewRow()得到的行,为什么不能添加到相同结构的其它表中? 这个行在NewRow()方法内已经被表连系起来了吗?
- 获取COOKIE的问题!
- ListView的CheckBox的问题
- 邮件客户端
- dataGrid问题
- 还是制作安装程序的问题
- C#中每个窗口都有一个cs文件,如何实现这些窗口的数据之间的相互传递?
改为
this.sqldaStudent.Update(this.dsStudent1.GetChanges(),"student");
我的功能是这样:从DataGrid中点击一行,删除选中的那一行。我通过BindingContext[this.dsStudent1,"student"].Position得到选中行的行号,再通过dsStudent1.Tables["student"].Rows[pos].Delete()方法删除对应选中行号的那一行。而delete()只是从数据集中删除,并没有实际从数据库中删除,然后就用DataAdapter的Update方法把修改应用到数据库。这个过程中没有SQL语句啊?
建议你使用CurrencyManager cm=(CurrencyManager)this.BindingContext[this.dsStudent1,"student"]
cm.REMOVEAT(cm.position);方便些
一般用向导做且最后命令能正确生成的update应该没问题的
SqlCommandBuilder builder=new SqlCommandBuilder(SqlDataAdapter);
/// save button
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btn_Save_Click(object sender, System.EventArgs e)
{
ComLogOutput.WriteLogFile("Button Save Click Begin"); if(!CheckUpdateMustInput())
{
return;
}
if(!CheckUpdateStyleInput())
{
return;
} DataSet dsUpt = GetUpdateDataSet(); if(dsUpt.GetChanges() == null)
{
//lbl_Err.Text = "No data changed,please input again";
lbl_Err.Text = ComLocalDiff.GetMessageById("0016",UserInfo.LangCode); }
else
{
UpdateDataBase(dsUpt);
} ComLogOutput.WriteLogFile("Button Save Click END");
} /// <summary>
/// update delete in db
/// </summary>
/// <param name="dstUpt"></param>
private void UpdateDataBase(DataSet dsUpt)
{
try
{
string strSql = GetSearchSql(); //update data
MstAreaDB mstDB = new MstAreaDB();
mstDB.UpdateArea(dsUpt,strSql); //research data
this.Session["dsp_MstArea_DgData"]=null;
BindDataGrid(dg_MstArea.CurrentPageIndex); //ComJScript.AlertNormal(this,"Action complete sucessfully!");
ComJScript.AlertNormal(this,ComLocalDiff.GetMessageById("0018",UserInfo.LangCode));
}
//Update Area
public void UpdateArea(DataSet dsArea,string strSqlQuery)
{
ComDbInterface db = new ComDbInterface(); try
{
db.BeginTran();
db.DataSetDBUpDate(strSqlQuery,dsArea);
db.CommitTran();
}
catch(Exception err)
{
db.RollBackDb();
throw err;
}
} protected void DataSetUpdateDB(string pSQL,DataSet resultData)
{
OleDbDataAdapter myDataAdapter = new OleDbDataAdapter();
myDataAdapter.SelectCommand = new OleDbCommand(pSQL,CSystem.Connection,CSystem.Transaction);
OleDbCommandBuilder custCB = new OleDbCommandBuilder(myDataAdapter); resultData.Tables[0].TableName = "Table";
myDataAdapter.Update(resultData);
}
{
this.student_conn.Open();
int pos=this.BindingContext[this.dsStudent1,"student"].Position;
this.dsStudent1.Tables["student"].Rows[pos].Delete();
->this.sqldaStudent.Update(this.dsStudent1,"student");
this.dsStudent1.Tables["student"].AcceptChanges();
this.student_conn.Close();
}
对上面的代码修改,
private void button6_Click(object sender, System.EventArgs e)
{
//this.student_conn.Open();//在单次更新下让sqlDataAdapter自己管理连接。
DataRow dr =((DataRowView)this.BindingContext[this.dsStudent1,"student"]).Row;
dr.Delete();
this.sqldaStudent.Update(this.dsStudent1,"student");
//this.dsStudent1.Tables["student"].AcceptChanges();//在调用SqlDataAdapter.Update后自动调用AcceptChanges
//this.student_conn.Close();
}
请确认你是否对SqlDataAdapter.DeleteCommand属性是否附于了实际删除数据的SqlCommand对象,
可以参考,
http://blog.csdn.net/zhzuo/archive/2004/08/06/67037.aspx