还是SQL语句更新问题,上一贴没有解决问题。

aspxstudio 2003-12-12 03:21:09
cmd=new OleDbCommand();
cmd.Connection=cnn;
if (update)
cmd.CommandText="update IndexFormula set Formula=@Formula where ID=@ID";
else
cmd.CommandText="insert into IndexFormula(ID,Formula) values (@ID,@Formula)";

cmd.Parameters.Add(new OleDbParameter("@ID",OleDbType.Integer));
cmd.Parameters["@ID"].Value=plateIndex;

cmd.Parameters.Add(new OleDbParameter("@Formula",OleDbType.VarChar,60));
cmd.Parameters["@Formula"].Value=indexFormula;

try
{
cmd.ExecuteNonQuery();
}
catch(System.Exception error)
{
MessageBox.Show("保存数据到数据库时出错!错误信息:"+error.Message.ToString(),"设备提示");
CloseConnection();
}
不知怎么回事,能插入新记录,但是不能更新。欺人!?
源贴:
http://expert.csdn.net/Expert/topic/2553/2553472.xml?temp=.7690393
希望大家多关照。
...全文
28 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
枫叶 2003-12-16
  • 打赏
  • 举报
回复
应该是一次查询,只能添加到一个表中,不管是adapter还是command。
参见:
ms-help://MS.VSCC.2003/MS.MSDNQTR.2003FEB.2052/cpref/html/frlrfsystemdataconstraintcollectionclasstopic.htm
在建立关系时,外键约束自动已经建立。
aspxstudio 2003-12-13
  • 打赏
  • 举报
回复
我不想用DataSet,我想知道为什么我的语句参数不起作用呢。
雪狼1234567 2003-12-13
  • 打赏
  • 举报
回复
首先取得数据,放到DataGrid里

System.Data.SqlClient.SqlConnection conn = new System.Data.SqlClient.SqlConnection("server=localhost;database=northWind;uid=sa;password=110");
conn.Open();
System.Data.SqlClient.SqlDataAdapter da = new System.Data.SqlClient.SqlDataAdapter("select * from student",conn);
dt = new System.Data.DataSet();
da.Fill(dt,"student");

然后绑定数据集和DataGrid
DataGrid.SetDataBinding(dt,"student");
如果需要,可以绑定TextBox来作录入,而用DataGrid显示
this.textBox16.DataBindings.Add("Text",dt,"student.stuno");
然后进行数据的操作如:
增加:
this.BindingContext[dt,"student"].AddNew();
删除:
this.BindingContext[dt,"student"].RemoveAt(this.BindingContext[dt,"student"].Position);
最后把结果写回数据库:


// sqlInsertCommand1
//
this.sqlInsertCommand1.CommandText = "INSERT INTO student(stuno, name) VALUES (@stuno, @name)";
this.sqlInsertCommand1.Connection = this.conn;
this.sqlInsertCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@stuno", System.Data.SqlDbType.VarChar, 4, "stuno"));
this.sqlInsertCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@name", System.Data.SqlDbType.VarChar, 50, "name"));
//
// sqlUpdateCommand1
//
this.sqlUpdateCommand1.CommandText = "UPDATE student SET stuno = @stuno, name = @name WHERE (stuno = @Original_stuno)";
this.sqlUpdateCommand1.Connection = this.conn;
this.sqlUpdateCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@stuno", System.Data.SqlDbType.VarChar, 4, "stuno"));
this.sqlUpdateCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@name", System.Data.SqlDbType.VarChar, 50, "name"));
this.sqlUpdateCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@Original_stuno", System.Data.SqlDbType.VarChar, 4, System.Data.ParameterDirection.Input, false, ((System.Byte)(0)), ((System.Byte)(0)), "stuno", System.Data.DataRowVersion.Original, null));

// sqlDeleteCommand1
//
this.sqlDeleteCommand1.CommandText = "DELETE FROM student WHERE (stuno = @Original_stuno)";
this.sqlDeleteCommand1.Connection = this.conn;
this.sqlDeleteCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@Original_stuno", System.Data.SqlDbType.VarChar, 4, System.Data.ParameterDirection.Input, false, ((System.Byte)(0)), ((System.Byte)(0)), "stuno", System.Data.DataRowVersion.Original, null));

this.sqlDa.DeleteCommand = this.sqlDeleteCommand1;
this.sqlDa.InsertCommand = this.sqlInsertCommand1;
this.sqlDa.UpdateCommand = this.sqlUpdateCommand1;
try
{
sqlDa.Update(dt.GetChanges,"student");
return true;
}
catch(System.Data.SqlClient.SqlException ex)
{

return false;
}
finally
{
conn.Close();
}


//-------------------------------------------
//批量增加10条记录,然后一次写回到数据库
//-------------------------------------

conn = new System.Data.SqlClient.SqlConnection("server=localhost;database=hos;uid=sa;pwd=");
conn.Open();
da = new System.Data.SqlClient.SqlDataAdapter("select * from test",conn);
dt1 = new System.Data.DataSet();
da.Fill(dt1,"test");
DataRow myRow;
for(int i=0;i<10;i++)
{
myRow = dt1.Tables[0].NewRow();
// Then add the new row to the collection.
myRow["ID"] = i.ToString();
dt1.Tables[0].Rows.Add(myRow);
}

System.Data.SqlClient.SqlDataAdapter daa = new System.Data.SqlClient.SqlDataAdapter("Select ID From test", conn);
System.Data.SqlClient.SqlCommandBuilder MyCB = new System.Data.SqlClient.SqlCommandBuilder(daa);
daa.MissingSchemaAction = MissingSchemaAction.AddWithKey;
daa.Update(dt1, "test");
CMIC 2003-12-12
  • 打赏
  • 举报
回复
字段名称最好不要用ID,数据库中ID的类型是什么?
aspxstudio 2003-12-12
  • 打赏
  • 举报
回复
to gshope(北京.Net)
直接拼行,可是我想我综合新建和更新在一起,如上代码。

to yaoyaonet(绿洲)
你的方法我试一下。

to kongfh(Horse In Red)
ID 唯一,运行无异常信息,就是数据库中记录不变!?!!
dldl 2003-12-12
  • 打赏
  • 举报
回复
你用的连接是sqlserver2000吗?如果是2000的话上面的可以,但是如果是access的话就要把=@Formula 变成 =? 了。
kongfh 2003-12-12
  • 打赏
  • 举报
回复
首先要确保ID字段值唯一,如果不唯一则数据库无法确认你要更新的是哪一条记录;
最好把异常的信息帖出来,要不然不好判断:)
peng_dw 2003-12-12
  • 打赏
  • 举报
回复
你设定一个断点,看看cmd.CommandText的结果,然后把这句SQL语句放在其他数据库工具里执行一下看看会不会出错。
gshope 2003-12-12
  • 打赏
  • 举报
回复
不要用参数,直接拼SQL语句
cmd.CommandText="update IndexFormula set Formula='"+TextBox1.Text+"'";
大户翁 2003-12-12
  • 打赏
  • 举报
回复
cmd.CommandText="update IndexFormula set Formula=@Formula where ID=@ID";
--------》
cmd.CommandText="update IndexFormula set Formula=?where ID=?"; 试试

110,567

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧