亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

關于SqlDataAdapter的幾個Command屬性的疑問

關于SqlDataAdapter的幾個Command屬性的疑問

慕萊塢森 2018-12-07 10:44:54
http://topic.csdn.net/u/20071119/17/9316fdc5-5993-4720-a8e7-0d4ab5e28528.html?上面的問題就是我碰到的我想操作的是利用SqlDataAdapter的幾個Command屬性(InsertCommand,UpdateCommand,DeleteCommand)來更新數據庫代碼:SqlConnection conn = new SqlConnection(@"Data Source=.\sqlexpress;Initial Catalog=newsystem;Integrated Security=True");? SqlDataAdapter da = new SqlDataAdapter("select * from comment", conn);??//這里的構造函數直接實例化了SelectCommand所需要的SqlCommand了吧?? DataSet ds = new DataSet("myds");? da.FillSchema(ds, SchemaType.Source, "comment");? da.Fill(ds, "comment");??//接下來設置InsertCommand所需要的SqlCommandSqlCommand incmd=new SqlCommand("insert into comment (****) values(****)",conn);? da.InsertCommand = incmd;??//接下來是更新到數據庫? da.Update(ds.Tables["comment"]);? ds.Tables["comment"].AcceptChanges();??可到數據庫里一看,悲劇發生了:沒有插入該條記錄!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!網上一查:有些說要實例化SqlCommandBuilder,可我覺得我的InsertCommand所需要的SqlCommand都寫好了,不需要這樣吧!SqlCommandBuilder好像只是適用于直接修改DataSet,由SqlCommandBuilder自動生成所需的SQL語句:? SqlConnection conn = new SqlConnection(@"Data Source=.\sqlexpress;Initial Catalog=newsystem;Integrated Security=True");? SqlDataAdapter da = new SqlDataAdapter("select * from comment", conn);? SqlCommandBuilder cmdb = new SqlCommandBuilder(da);? DataSet ds = new DataSet("myds");? da.FillSchema(ds, SchemaType.Source, "comment");? da.Fill(ds, "comment");? ds.Tables["comment"].Rows[5]["content"] = "Can you help me???";? da.Update(ds.Tables["comment"]);? ds.Tables["comment"].AcceptChanges();
查看完整描述

1 回答

?
慕后森

TA貢獻1802條經驗 獲得超5個贊

public static SqlDataAdapter CreateCustomerAdapter( SqlConnection connection)
{
    SqlDataAdapter adapter = new SqlDataAdapter();

    // Create the SelectCommand.
    SqlCommand command = new SqlCommand("SELECT * FROM Customers " +
        "WHERE Country = @Country AND City = @City", connection);

    // Add the parameters for the SelectCommand.
    command.Parameters.Add("@Country", SqlDbType.NVarChar, 15);
    command.Parameters.Add("@City", SqlDbType.NVarChar, 15);

    adapter.SelectCommand = command;

    // Create the InsertCommand.
    command = new SqlCommand(
        "INSERT INTO Customers (CustomerID, CompanyName) " +
        "VALUES (@CustomerID, @CompanyName)", connection);

    // Add the parameters for the InsertCommand.
    command.Parameters.Add("@CustomerID", SqlDbType.NChar, 5, "CustomerID");
    command.Parameters.Add("@CompanyName", SqlDbType.NVarChar, 40, "CompanyName");

    adapter.InsertCommand = command;

    // Create the UpdateCommand.
    command = new SqlCommand(
        "UPDATE Customers SET CustomerID = @CustomerID, CompanyName = @CompanyName " +
        "WHERE CustomerID = @oldCustomerID", connection);

    // Add the parameters for the UpdateCommand.
    command.Parameters.Add("@CustomerID", SqlDbType.NChar, 5, "CustomerID");
    command.Parameters.Add("@CompanyName", SqlDbType.NVarChar, 40, "CompanyName");
    SqlParameter parameter = command.Parameters.Add(
        "@oldCustomerID", SqlDbType.NChar, 5, "CustomerID");
    parameter.SourceVersion = DataRowVersion.Original;

    adapter.UpdateCommand = command;

    // Create the DeleteCommand.
    command = new SqlCommand(
        "DELETE FROM Customers WHERE CustomerID = @CustomerID", connection);

    // Add the parameters for the DeleteCommand.
    parameter = command.Parameters.Add(
        "@CustomerID", SqlDbType.NChar, 5, "CustomerID");
    parameter.SourceVersion = DataRowVersion.Original;

    adapter.DeleteCommand = command;

    return adapter;
}

查看完整回答
反對 回復 2019-01-21
  • 1 回答
  • 0 關注
  • 356 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號