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

cjj*_*per 3 c# dataadapter rowstate

我从Excel电子表格中选择*到dt.我想获取这些值并更新SQL表.(SQL表存在是因为从原始Excel电子表格手动导入到SQL,具有主键设置.用户更新excel表,我需要更新SQL值.)我将dt.RowState设置为已修改努力调用更新.我没有错误,但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);   
Run Code Online (Sandbox Code Playgroud)

Kei*_*sta 8

试试这个...

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();
   }
 }
}
Run Code Online (Sandbox Code Playgroud)