C# SQLDataAdapter - Issues with INSERT

耗尽温柔 提交于 2020-01-16 05:05:08

问题


Please refer to the question that started it all for me: C# SQLDataAdapter - Not inserting data into DB

I'm developing an archival system, well building on to it I should say. But my latest issue looks like this once I hit 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.

Now here's where it potentially gets a little tricky.. the tables that I'm trying to INSERT into, they don't have any primary or foreign keys associated to them. Not my design. I'm wondering if the above execute() call is having troubles with the INSERT because of this. I dont' have anything that I need to set as @@Identity because.. well I don't have anything like that available.

Thoughts?

Update:

As requested here's the query and the 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

Update:

On a whim I wanted to check to see what the InsertCommand.Parameters held. I took a look at @TableID value and it was Null.

sqlAdapter.Fill(myDS.myTable); // myTable.count = 7k+
var x = sqlAdapter.InsertCommand.Parameters; // x-@tableID value = null
var rowsAffected = sqlAdapter.InsertCommand.ExecuteNonQuery();

I have a feeling that's why I'm seeing this error. Anyone have any thoughts on how I can resolve this?


回答1:


I don't see where you're actually setting the value of the parameter(s). You add the parameter, but your code doesn't show that you ever populate the parameter with a value.

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);

If, on the other hand, the TableId column has an identity specifcation, then modify your sql string and leave TableId out of it altogether - the database will provide it on insert.




回答2:


The error is because it is null. Make the TableID column as Identity in the table.




回答3:


Yes of course you are not supplying the value..

In your method

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

.

You can supply a value by two methods

Either

sqlComm.Parameters.Add("@TableID", SqlDbType.Int).Value = 4;

Or

sqlComm.Parameters.AddWithValue("@TableID", 4);



回答4:


You are creating data adapter but the way you use the data adapter is like using a SqlCommand.

Refer this for way to use data adapter: Using Data Adapter

To use a sql command,

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(); 


来源:https://stackoverflow.com/questions/32387164/c-sharp-sqldataadapter-issues-with-insert

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!