3

select * fromはExcelスプレッドシートをにDataTable 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);   
4

3 に答える 3

9

これを試して:

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();
        }
    }
}
于 2011-11-17T17:52:04.747 に答える
2

SqlCommandBuilderによって生成された自動生成されたSqlCommandsは、実際には適切ではないと思います(質問を正しく理解している場合)。SqlCommandBuilderによって生成されたSQLUpdateステートメントのWHERE句では、すべての列の値が元の値(DataRowの元のデータ値によって決定される)と比較されます。宛先データベースで元の値が一致しない場合、行は更新されません。

SqlCommandBuilderへのこのリンクは役立つかもしれません:

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

そのリンクから、「adCriteriaAllCols」を理解してみてください。これは、SqlCommandBuilderが使用するものです。あなたが望むのは「AdCriteriaKey」の振る舞いだと思います。

考えられる解決策の1つは、SqlCommandBuilderを使用せず、INSERT / UPDATE / DELETE SqlCommandsを自分で作成し、それらをSqlDataAdapter.InsertCommand、UpdateCommand、およびDeleteCommandにアタッチすることです。http://support.microsoft.com/kb/308055
にいくつかのサンプルコードがあります

編集:.netバージョン2.0以降のSqlCommandBuilderには、ConflictOptionプロパティがあります。使用されるデフォルトは、CompareAllSearchableValuesです。:OverwriteChangesを使用してみてください。これにより、SQLステートメントで生成されたWHERE句で主キー値のみが比較されます。

于 2011-11-19T01:02:57.303 に答える
1

コメントしてみましたが、質問を読み直すように言われました。dtだから私はそうしました、そしてそれは役に立ちませんでした:)その例では、データベースに結び付ける(これはExcelから入力されていると言う)コードはほとんどありません。変数、、、connectionToSQLおよびがありcmdます。これらはデータベースに接続されています。次に、を繰り返しますが、そうではありません。これが私のコメントで、dtの行を変更するソースコードの例を尋ねた理由です-変更がExcel(入力された)からデータベースにジャンプすることを期待するために、どこかにそれがあると推測したためです。dabdtdt

da.Update(dt);データベースから新しいデータセットを開いて、の行を調べ、新しいデータセットの行にdt変更を適用してみてくださいと呼んでいるようです。dt私の知る限り、コードはそれほど多くありませんが、データアダプターはの内部行がデータソースからのものではないことを知っているため、コマンドは発行されません。とにかく、それは私の刺し傷です。

于 2011-11-17T23:41:14.280 に答える