C# SQLDataAdapter - Not inserting data into DB

試著忘記壹切 提交于 2019-12-13 05:21:25

问题


Here's what I got: User selects from a checklistbox of database names one they'd like to archive. Switch case in place to catch the selection.

case "userSelection":
                sqlAdapter = CreateMyAdapter("dbName", true, sqlconn, null);
                sqlAdapter.SelectCommand.CommandText += "";
                sqlAdapter.Fill(myDS.tableName);
                sqlAdapter.Dispose();

The adapter:

private SqlDataAdapter CreateMyAdapter(string TableName, bool IncludeUpdates, SqlConnection sqlConn, SqlTransaction sqlTran)
{
    SqlDataAdapter sqlAdapter = null;
    SqlConnection sqlConnArchive = new SqlConnection();

    strSQL = "SELECT " + TableName + ".* FROM " + TableName;
    sqlAdapter = new SqlDataAdapter(strSQL, sqlConn);

    // Right here, I create another sqlConnection that is pointed to
    // another datasource.

    sqlConnArchive = getThisOtherConnection();

    SqlCommand sqlComm;

    if (IncludeUpdates)
    {
        string strInsertSQL = "<insertQuery>";

        sqlComm = new SqlCommand(strInsertSQL, sqlConnArchive);
        sqlComm.Parameters.Add("@TableID", SqlDbType.Int, 0, "TableID");
        // More params here...

        sqlAdapter.InsertCommand = sqlComm;

        // Update

        // Delete

    }
 }
        return sqlAdapter;

The issue: As you can see sqlConn is the connection that is tied to the SELECT command. And sqlConnArchive is tied to the INSERT. The thought here is that I could select the data from DB_1 if you will, and insert it into DB_2 using the same SQLDataAdapter. But the issue that I'm running into is trying to insert. The select works fine, and at this line sqlAdapter.Fill(myDS.tableName); once fill executes the data is there. But the INSERT isn't working.

A few things:

  • I tested to see if perhaps SQLDataAdapter couldn't handle multiple datasources/connections, switched things around so it was pointing the the same DB just different tables, and I'm seeing the same results.

  • I've confirmed that the issue does not reside within the INSERT query.

  • There are no errors, just steps right over in debug.

  • I have tried several permutations of .Update() and none of them worked. This project that I've been assigned, throughout the entire thing it appears that .Fill(); is what is submitting the data back to the DB.

  • I've tested the database side and connectivity is a go. No issues with login, etc etc..

Any help is greatly appreciated.

Please note - I tried to place an even larger emphasis on the word "greatly" but was limited by my toolset. Apparently SOF doesn't support bold, blink, underline, flames, or embedded music.


回答1:


I think you want ExecuteNonQuery.

var rowsAffected = sqlAdapter.InsertCommand.ExecuteNonQuery();

This executes the statement and then returns the number of rows affected. The Fill method won't run any InsertCommands.



来源:https://stackoverflow.com/questions/32385295/c-sharp-sqldataadapter-not-inserting-data-into-db

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