C# SQLDataAdapter - INSERT 问题
C# SQLDataAdapter - Issues with INSERT
请参阅为我启动这一切的问题:C# SQLDataAdapter - Not inserting data into DB
我正在开发一个档案系统,我应该说,它是建立在它之上的。但是我点击 var rowsAffected = sqlAdapter.InsertCommand.ExecuteNonQuery();
后,我的最新一期看起来像这样:
Additional information: The parameterized query '(@TableID int,@RowID int,@RowState tinyint,@UserID int,@XMLHisto' expects the parameter '@TableID', which was not supplied.
这里可能有点棘手..我试图插入的表,它们没有任何关联的主键或外键。不是我的设计。我想知道上面的 execute() 调用是否因此与 INSERT 有问题。我没有任何需要设置为@@Identity 的东西,因为……好吧,我没有任何可用的东西。
想法?
更新:
这里是查询和 sql cmd..
string strInsertSQL = "INSERT INTO <tableName> (TableID, RowID, ModifyDate, RowState, UserID, XMLHistory, RowDescription) " +
"VALUES (@TableID, @RowID, GetDate(), @RowState, @UserID, @XMLHistory, @RowDescription); SET @NewID = @@Identity"; // SET statement needed?
sqlComm = new SqlCommand(strInsertSQL, sqlConnArchive);
sqlComm.Parameters.Add("@TableID", SqlDbType.Int, 4, "TableID"); // Not sure if this is needed since primary key doesn't exist?
// More params added here
更新:
一时兴起,我想看看 InsertCommand.Parameters 持有什么。我看了一下@TableID 值,它是 Null。
sqlAdapter.Fill(myDS.myTable); // myTable.count = 7k+
var x = sqlAdapter.InsertCommand.Parameters; // x-@tableID value = null
var rowsAffected = sqlAdapter.InsertCommand.ExecuteNonQuery();
我感觉这就是我看到此错误的原因。有人对我如何解决这个问题有任何想法吗?
错误是因为它是空的。将 TableID 列设为 table.
中的标识
我没有看到您实际在哪里设置参数值。您添加了参数,但您的代码并未显示您曾使用值填充参数。
sqlComm = new SqlCommand(strInsertSQL, sqlConnArchive);
var tableIdParam = new SqlParameter("@TableId", SqlDbType.Int, 4);
tableIdParam.Value = 99; //whatever id value you want to set here
sqlComm.Parameters.Add(tableIdParam);
另一方面,如果 TableId 列具有身份说明,则修改您的 sql 字符串并完全不使用 TableId - 数据库将在插入时提供它。
是的,当然你没有提供价值..
用你的方法
sqlComm.Parameters.Add("@TableID", SqlDbType.Int, 4, "TableID")
// the value '4' here specifies the size of the column not value.
// for example max length of a string if you are about to supply a varchar
.
您可以通过两种方法提供值
或者
sqlComm.Parameters.Add("@TableID", SqlDbType.Int).Value = 4;
或
sqlComm.Parameters.AddWithValue("@TableID", 4);
您正在创建数据适配器,但您使用数据适配器的方式就像使用 SqlCommand。
数据适配器的使用方法请参考:Using Data Adapter
要使用 sql 命令,
string strInsertSQL = "INSERT INTO <tableName> (TableID, RowID, ModifyDate, RowState, UserID, XMLHistory, RowDescription) " +
"VALUES (@TableID, @RowID, GetDate(), @RowState, @UserID, @XMLHistory, @RowDescription)"
sqlComm = new SqlCommand(strInsertSQL, sqlConnArchive);
sqlComm.Parameters.Add("@TableID", SqlDbType.Int).Value = 1; //Set it to the value you want to insert
..... the rest of the parameters
sqlConnArchive.Open();
sqlComm.ExecuteNonQuery();
请参阅为我启动这一切的问题:C# SQLDataAdapter - Not inserting data into DB
我正在开发一个档案系统,我应该说,它是建立在它之上的。但是我点击 var rowsAffected = sqlAdapter.InsertCommand.ExecuteNonQuery();
后,我的最新一期看起来像这样:
Additional information: The parameterized query '(@TableID int,@RowID int,@RowState tinyint,@UserID int,@XMLHisto' expects the parameter '@TableID', which was not supplied.
这里可能有点棘手..我试图插入的表,它们没有任何关联的主键或外键。不是我的设计。我想知道上面的 execute() 调用是否因此与 INSERT 有问题。我没有任何需要设置为@@Identity 的东西,因为……好吧,我没有任何可用的东西。
想法?
更新:
这里是查询和 sql cmd..
string strInsertSQL = "INSERT INTO <tableName> (TableID, RowID, ModifyDate, RowState, UserID, XMLHistory, RowDescription) " +
"VALUES (@TableID, @RowID, GetDate(), @RowState, @UserID, @XMLHistory, @RowDescription); SET @NewID = @@Identity"; // SET statement needed?
sqlComm = new SqlCommand(strInsertSQL, sqlConnArchive);
sqlComm.Parameters.Add("@TableID", SqlDbType.Int, 4, "TableID"); // Not sure if this is needed since primary key doesn't exist?
// More params added here
更新:
一时兴起,我想看看 InsertCommand.Parameters 持有什么。我看了一下@TableID 值,它是 Null。
sqlAdapter.Fill(myDS.myTable); // myTable.count = 7k+
var x = sqlAdapter.InsertCommand.Parameters; // x-@tableID value = null
var rowsAffected = sqlAdapter.InsertCommand.ExecuteNonQuery();
我感觉这就是我看到此错误的原因。有人对我如何解决这个问题有任何想法吗?
错误是因为它是空的。将 TableID 列设为 table.
中的标识我没有看到您实际在哪里设置参数值。您添加了参数,但您的代码并未显示您曾使用值填充参数。
sqlComm = new SqlCommand(strInsertSQL, sqlConnArchive);
var tableIdParam = new SqlParameter("@TableId", SqlDbType.Int, 4);
tableIdParam.Value = 99; //whatever id value you want to set here
sqlComm.Parameters.Add(tableIdParam);
另一方面,如果 TableId 列具有身份说明,则修改您的 sql 字符串并完全不使用 TableId - 数据库将在插入时提供它。
是的,当然你没有提供价值..
用你的方法
sqlComm.Parameters.Add("@TableID", SqlDbType.Int, 4, "TableID")
// the value '4' here specifies the size of the column not value.
// for example max length of a string if you are about to supply a varchar
.
您可以通过两种方法提供值
或者
sqlComm.Parameters.Add("@TableID", SqlDbType.Int).Value = 4;
或
sqlComm.Parameters.AddWithValue("@TableID", 4);
您正在创建数据适配器,但您使用数据适配器的方式就像使用 SqlCommand。
数据适配器的使用方法请参考:Using Data Adapter
要使用 sql 命令,
string strInsertSQL = "INSERT INTO <tableName> (TableID, RowID, ModifyDate, RowState, UserID, XMLHistory, RowDescription) " +
"VALUES (@TableID, @RowID, GetDate(), @RowState, @UserID, @XMLHistory, @RowDescription)"
sqlComm = new SqlCommand(strInsertSQL, sqlConnArchive);
sqlComm.Parameters.Add("@TableID", SqlDbType.Int).Value = 1; //Set it to the value you want to insert
..... the rest of the parameters
sqlConnArchive.Open();
sqlComm.ExecuteNonQuery();