C# - 使用 DataAdapter 从 DataTable 更新 SQL 表 - SQL 表不更新

标签 c# dataadapter rowstate

<分区>

我从 Excel 电子表格中选择 * DataTable dt 中。我想获取这些值并更新 SQL 表。 SQL 表存在是因为从原始 Excel 电子表格手动导入到 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();
        }
    }
}

关于C# - 使用 DataAdapter 从 DataTable 更新 SQL 表 - SQL 表不更新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8171647/

相关文章:

c# - ReSharper 单元测试未在 bin 目录中运行

c# - 显示 WPF TextBox 的剩余字符,该文本框的 MaxLength 受到限制

c# - 在不知道驱动程序类型的情况下创建适配器

c# - 使用 DataAdapter 填充表

c# - 如何从数据表中删除所有行

c# - ASP.NET 核心中间件

c# - 错误 : Update requires a valid UpdateCommand when passed DataRow collection with modified rows

c# - RowState.Added 和 DataRowVersion.Original 之间有什么区别

c# - List.Contains() 的循环实现看起来比内置的更快。是吗?如果是这样,为什么?