c# -使用数据适配器从数据表更新SQL表- SQL表不更新



I select * from将Excel电子表格导入DataTable dt。我想获取这些值并更新SQL表。由于从原始Excel电子表格手动导入SQL, SQL表的存在有一个主键集。用户更新excel表,我需要更新SQL值。为了调用更新,我将dt.RowState设置为modified。我没有得到错误,但SQL表不更新。前面的测试表明我的SQL权限和连接是好的,我可以修改表。

connectionToSQL = new SqlConnection(SQLConnString);
connectionToSQL.Open();
var cmd = new SqlCommand("SELECT * FROM TAGS$",connectionToSQL);                 
var da = new SqlDataAdapter(cmd);
var b = new SqlCommandBuilder(da);
foreach (DataRow r in dt.Rows)
{
    r.SetModified();
}
da.Update(dt);   

试试这个:

using System.Data;
using System.Data.SqlClient;
using System;
namespace Q308507 {
    class Class1 
    {
        static void Main(string[] args) 
        {
            SqlConnection cn = new SqlConnection();
            DataSet CustomersDataSet = new DataSet();
            SqlDataAdapter da;
            SqlCommandBuilder cmdBuilder;
            // Set the connection string of the SqlConnection object
            // to connect to the SQL Server database in which you
            // created the sample table.
            cn.ConnectionString =
            "Server=server;Database=northwind;UID=login;PWD=password;";
            cn.Open();      
            // Initialize the SqlDataAdapter object by specifying a
            // Select command that retrieves data from the sample table.
            da = new SqlDataAdapter("select * from CustTest order by CustId", cn);
            // Initialize the SqlCommandBuilder object to automatically
            // generate and initialize the UpdateCommand,
            // InsertCommand, and DeleteCommand properties
            // of the SqlDataAdapter.
            cmdBuilder = new SqlCommandBuilder(da);
            // Populate the DataSet by running the Fill method
            // of the SqlDataAdapter.
            da.Fill(CustomersDataSet, "Customers");
            // Display the Update, Insert, and Delete commands
            // that were automatically generated
            // by the SqlCommandBuilder object.
            Console.WriteLine(
                "Update command Generated by the Command Builder : ");
            Console.WriteLine(
                "==================================================");
            Console.WriteLine(
                cmdBuilder.GetUpdateCommand().CommandText);
            Console.WriteLine("         ");
            Console.WriteLine(
                "Insert command Generated by the Command Builder : ");
            Console.WriteLine(
                "==================================================");
            Console.WriteLine(cmdBuilder.GetInsertCommand().CommandText);
            Console.WriteLine("         ");        
            Console.WriteLine(
                "Delete command Generated by the Command Builder : ");
            Console.WriteLine(
                "==================================================");
            Console.WriteLine(cmdBuilder.GetDeleteCommand().CommandText);
            Console.WriteLine("         ");
            // Write out the value in the CustName field before
            // updating the data using the DataSet.
            Console.WriteLine("Customer Name before Update : " +
                CustomersDataSet.Tables["Customers"].Rows[0]["CustName"]);
    
            // Modify the value of the CustName field.
            CustomersDataSet.Tables["Customers"].Rows[0]["CustName"] = "Jack";
            // Post the data modification to the database.
            da.Update(CustomersDataSet, "Customers");        
            Console.WriteLine("Customer Name updated successfully");
            // Close the database connection.
            cn.Close();
            // Pause
            Console.ReadLine();
        }
    }
}

我认为由SqlCommandBuilder生成的自动生成的SqlCommands并不真的适合您的情况(如果我正确理解了这个问题)。在SqlCommandBuilder生成的SQL Update语句的WHERE子句中,将所有列的值与它们的原始值(由DataRow中的原始数据值确定)进行比较。如果原始值在目标数据库中不匹配,则不会更新任何行。

SqlCommandBuilder的链接可能会有所帮助:

http://msdn.microsoft.com/en-us/library/ms971491.aspx

从该链接,试着理解:"adCriteriaAllCols",因为这是SqlCommandBuilder使用的。我怀疑你想要的是"adcriterikey"行为。

一个可能的解决方案可能是不使用SqlCommandBuilder,而只是自己编写INSERT/UPDATE/DELETE sqlcommand,并将它们附加到SqlDataAdapter。插入命令、更新命令和删除命令。
这里有一些示例代码:http://support.microsoft.com/kb/308055

EDIT: .net 2.0及以后版本的SqlCommandBuilder有一个ConflictOption属性。默认使用的是:CompareAllSearchableValues。尝试使用:OverwriteChanges,这将导致SQL语句中生成的WHERE子句只比较主键值。

我试着评论,但被告知重新阅读问题。所以我做了,它没有帮助:)你有很少的代码在这个例子中,它连接dt(你说这是从Excel填充)到数据库。您有变量connectionToSQL, cmd, dab。这些都连接到数据库。然后迭代dt,这不是。这就是为什么在我的评论中,我要求示例源代码,你在哪里修改行dt -因为我假设你会在某个地方,为了期望的变化会从Excel(填充dt)跳转到你的数据库。

我看到你正在调用da.Update(dt);尝试从数据库打开一个新的数据集,并通过dt中的行,将更改应用于新数据集中的行。据我所知——没有太多代码——没有发出命令,因为数据适配器知道dt的内部行不是来自它的数据源。这是我的尝试

最新更新