每次用ExecuteNonQuery都会错!!

xxqqhh_nn 2002-06-26 07:02:19
每次用ExecuteNonQuery都会错!!
我想在特定的地方修改数据,代码如下:
string strUpdt = " UPDATE studentmessage SET unit = '"+ unit.Text + "' ,unitid = '" + unitid.Text + "' , studentno ='0 ' , birthyear = '" + birthyear.Text + "',studentsort='" + studentsort.Text + "',studentsource='" + studentsourcesort + "'WHERE studentno = " + 0;
SqlCommand myCommand2 = new SqlCommand ( strUpdt , sqlConnection1 ) ;
myCommand2.ExecuteNonQuery ( ) ;!!!!!!!这里错!!!!!
sqlConnection1.Close ( ) ;
我还用对过以上方式的数据添加,修改!
不知道利用数据集的修改记录该怎么写。
DataRow anyRow =dataSet61.studentmessage.Rows[0];
anyRow["unit"]= unit.Text;
anyRow["unitid"]= unitid.Text;
anyRow["报考号"]= 0;
anyRow["birthyear"]= birthyear.Text;
anyRow["studentsort"]= studentsort.Text ;
anyRow["studentsourcesort"]= studentsourcesort.Text ;
dataSet61.studentmessage.Rows.Add(anyRow);!!!!这里错!!!!
sqlDataAdapter1.Update(dataSet61);!!!!!这里错!!!!
我这么写程序提示:数据库有一条记录,怎样修改以上的程序呢?
望指教。
...全文
38 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
juqiang 2002-06-27
  • 打赏
  • 举报
回复
cath一个exception,把错误贴上来看看吧!
redcaff_l 2002-06-27
  • 打赏
  • 举报
回复
到底有什么错?
xxqqhh_nn 2002-06-27
  • 打赏
  • 举报
回复
up!
xxqqhh_nn 2002-06-27
  • 打赏
  • 举报
回复
我的意图是修改呀,不是添加!
johnage 2002-06-26
  • 打赏
  • 举报
回复
为知道这些对你是否有帮助

using System;
using System.Data;
using System.Data.SqlClient;
using System.Data.Common;

public class executingacommand
{
public static void Main()
{
executingacommand myexecutingacommand = new executingacommand();
myexecutingacommand.Run();
}

public void Run()
{

SqlConnection myConnection = new SqlConnection("server=(local)\\NetSDK;Trusted_Connection=yes;database=northwind");
SqlCommand myCommand = new SqlCommand();
SqlTransaction myTrans;

// Open the connection.
myConnection.Open();

// Assign the connection property.
myCommand.Connection = myConnection;

// Begin the transaction.
myTrans = myConnection.BeginTransaction();

// Assign transaction object for a pending local transaction
myCommand.Transaction = myTrans;

try
{
// Restore database to near it's original condition so sample will work correctly.
myCommand.CommandText = "DELETE FROM Region WHERE (RegionID = 100) OR (RegionID = 101)";
myCommand.ExecuteNonQuery();

// Insert the first record.
myCommand.CommandText = "Insert into Region (RegionID, RegionDescription) VALUES (100, 'MidWestern')";
myCommand.ExecuteNonQuery();

// Insert the second record.
myCommand.CommandText = "Insert into Region (RegionID, RegionDescription) VALUES (101, 'MidEastern')";
myCommand.ExecuteNonQuery();

myTrans.Commit();
Console.WriteLine("两个记录都已写入数据库!");
}
catch(Exception e)
{
myTrans.Rollback();
Console.WriteLine(e.ToString());
Console.WriteLine("两个记录都未写入数据库!");
}
finally
{
myConnection.Close();
}
}
}

esupermary 2002-06-26
  • 打赏
  • 举报
回复
另外,看了一下你的下面的DataSet,也有问题。
你的DataRow anyRow =dataSet61.studentmessage.Rows[0];命令会使anyRow成为dataSet61.studentmessage表的第一行的内容。
改正如下:
DataRow anyRow =dataSet61.studentmessage.NewRow();
anyRow["unit"]= unit.Text;
anyRow["unitid"]= unitid.Text;
anyRow["报考号"]= 0;
anyRow["birthyear"]= birthyear.Text;
anyRow["studentsort"]= studentsort.Text ;
anyRow["studentsourcesort"]= studentsourcesort.Text ;
dataSet61.studentmessage.Rows.Add(anyRow);
sqlDataAdapter1.Update(dataSet61);

esupermary 2002-06-26
  • 打赏
  • 举报
回复
我看也许是因为你的SQL语句有问题吧。你最好用查询生成器生成这个UPDATE语句然后再做一些更改,这样就没有问题了。从你的SQL上看,可能是因为WHERE语句后的条件没有加上括号。
bigrongshu 2002-06-26
  • 打赏
  • 举报
回复
用数据集更新时候update()前先创建一个SqlCommandBuilder对象

如SqlCommandBuilder cb= New SqlCommandBuilder(MyDataAdapter);
dreammaster 2002-06-26
  • 打赏
  • 举报
回复
string strUpdt = " UPDATE studentmessage SET unit = '"+ unit.Text + "' ,unitid = '" + unitid.Text + "' , studentno ='0 ' , birthyear = '" + birthyear.Text + "',studentsort='" + studentsort.Text + "',studentsource='" + studentsourcesort + "' WHERE studentno = " + 0;
SqlCommand myCommand2 = new SqlCommand ( strUpdt , sqlConnection1 ) ;

在WHere前加上空格看看。

110,569

社区成员

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

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

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