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前加上空格看看。
SqlCommand myCommand2 = new SqlCommand ( strUpdt , sqlConnection1 ) ;在WHere前加上空格看看。
你的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);
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();
}
}
}