C# SQLDataAdapter - 不将数据插入数据库 [英] C# SQLDataAdapter - Not inserting data into DB

查看:23
本文介绍了C# SQLDataAdapter - 不将数据插入数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我得到的:用户从他们想要存档的数据库名称的清单框中选择一个.切换到位以捕捉选择.

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

适配器:

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;

问题:如您所见,sqlConn 是绑定到 SELECT 命令的连接.而 sqlConnArchive 与 INSERT 相关联.这里的想法是,如果您愿意,我可以从 DB_1 中选择数据,然后使用相同的 SQLDataAdapter 将其插入到 DB_2 中.但我遇到的问题是试图插入.选择工作正常,在这一行 sqlAdapter.Fill(myDS.tableName); 一旦填充执行数据就在那里.但是 INSERT 不起作用.

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.

一些事情:

  • 我进行了测试,看看 SQLDataAdapter 是否可能无法处理多个数据源/连接,并进行了切换,因此它指向同一个 DB 只是不同的表,并且我看到了相同的结果.

  • 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.

我已经确认问题不在 INSERT 查询中.

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

没有错误,只是调试中的步骤.

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

我尝试了几种 .Update() 排列,但都没有奏效.我被分配的这个项目,在整个过程中,.Fill(); 似乎是将数据提交回数据库.

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..

任何帮助非常表示感谢.

请注意 - 我试图更加强调非常"这个词,但受到我的工具集的限制.显然 SOF 不支持粗体、闪烁、下划线、火焰或嵌入的音乐.

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.

推荐答案

我想你想要 ExecuteNonQuery.

I think you want ExecuteNonQuery.

var rowsAffected = sqlAdapter.InsertCommand.ExecuteNonQuery();

这将执行语句,然后返回受影响的行数.Fill 方法不会运行任何 InsertCommand.

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

这篇关于C# SQLDataAdapter - 不将数据插入数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆