將dataGridView中的添加/刪除等修改儲存至資料庫

來源:互聯網
上載者:User

 

Imports System.Data
Imports System.Data.SqlClient

Public Class Form1Class Form1

     Dim ds As DataSet
     Dim da As SqlDataAdapter
     Dim cb As SqlCommandBuilder
     Dim cmd As SqlCommand
     Dim conn As New SqlConnection("data source=localhost;Database=HAWK;Integrated Security=SSPI;")


     Private Sub Form1_Load()Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
          conn.Open()
          cmd = conn.CreateCommand
          cmd.CommandText = "Select sID,sName,class,pro from studentInfo"
          da = New SqlDataAdapter(cmd)
          ds = New DataSet()
          da.Fill(ds)
          DataGridView1.DataSource = ds.Tables(0)

     End Sub

     Private Sub Button1_Click()Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSave.Click
          cmd.CommandText = "Select sID,sName,class,pro from studentInfo"
          da = New SqlDataAdapter(cmd)
          cb = New SqlCommandBuilder(da)
          da.Update(ds)
          DataGridView1.Update()

     End Sub
End Class

---------------------------------------------------------------------------------------------------------

C#範例程式碼如下:

using System.Data;
using System.Data.SqlClient;

public partial class Form1 : Form
    {


        DataSet ds =new DataSet();
        SqlDataAdapter da=new SqlDataAdapter();
        SqlCommandBuilder cb=new SqlCommandBuilder();
        SqlCommand cmd=new SqlCommand();
        private string sql = "select * from TEST_Table";
        private string connString = "Data Source=.;User ID=sa;Password=123;Initial Catalog=Test;";

private void Form1_Load(object sender, EventArgs e)
        {

            SqlConnection conn = new SqlConnection(connString);
            SqlDataAdapter da = new SqlDataAdapter(sql, conn);

           // cmd = conn.CreateCommand();
            // cmd.CommandText = "select * from TEST_Table";
            // SqlDataAdapter da = new SqlDataAdapter(cmd);

            // private string tableName = "test";
            //da.Fill(ds, "test");
            //dataGridView1.DataSource = ds.Tables[tableName];

            da.Fill(ds);
            dataGridView1.DataSource = ds.Tables[0];
        }

   private void button1_Click(object sender, EventArgs e) //儲存修改
        {
            SqlConnection conn = new SqlConnection(connString);
            cmd = conn.CreateCommand();
            cmd.CommandText = "select * from Test_Table";
            SqlDataAdapter da = new SqlDataAdapter(cmd);
            SqlCommandBuilder cb = new SqlCommandBuilder(da);
         da.Update(ds);
         dataGridView1.Update();
        }
    }

--------------------------------------------------------------------------------------結束

 

儲存的重點就在於使用了SqlCommandBuilder 成員,SqlCommandBuilder 成員的說明如下:

 

SqlCommandBuilder 成員。

備忘

SqlDataAdapter 不會自動產生實現 DataSet 的更改與關聯的 SQL Server 執行個體之間的協調所需的 Transact-SQL 陳述式。但是,如果設定了 SqlDataAdapter 的 SelectCommand 屬性,則可以建立一個 SqlCommandBuilder 對象來自動產生用於單表更新的 Transact-SQL 陳述式。然後,SqlCommandBuilder 將產生其他任何未設定的 Transact-SQL 陳述式。

一旦設定 DataAdapter 屬性,SqlCommandBuilder 就將其自身註冊為 RowUpdating 事件的接聽程式。一次只能將一個 SqlDataAdapter 與一個 SqlCommandBuilder 對象(或相反)互相關聯。

為了產生 INSERT、UPDATE 或 DELETE 語句,SqlCommandBuilder 會自動使用 SelectCommand 屬性來檢索所需的中繼資料集。如果在檢索中繼資料後(例如在第一次更新後)更改 SelectCommand,則應調用 RefreshSchema 方法來更新中繼資料。

SelectCommand 還必須至少返回一個主鍵列或唯一的列。如果什麼都沒有返回,就會產生 InvalidOperation 異常,不產生命令。

SqlCommandBuilder 還使用由 SelectCommand 引用的 Connection、CommandTimeout 和 Transaction 屬性。如果修改了任何這些屬性或者替換了 SelectCommand 本身,使用者則應調用 RefreshSchema。否則,InsertCommand、UpdateCommand 和 DeleteCommand 屬性將保留它們以前的值。

如果調用 Dispose,則會解除 SqlCommandBuilderSqlDataAdapter 的關聯,並且不再使用所產生的命令。

樣本

[Visual Basic, C#, C++] 下面的樣本使用 SqlCommand 以及 SqlDataAdapter 和 SqlConnection 從資料來源選擇行。給該樣本傳遞一個初始化的 DataSet、一個連接字串、一個查詢字串(它是一個 Transact-SQL SELECT 語句)和一個包含資料庫表名稱的字串。然後該樣本建立一個 SqlCommandBuilder

[Visual Basic] Public Shared Function SelectSqlSrvRows(myConnection As String, mySelectQuery As String, myTableName As String) As DataSet    Dim myConn As New SqlConnection(myConnection)    Dim myDataAdapter As New SqlDataAdapter()    myDataAdapter.SelectCommand = New SqlCommand(mySelectQuery, myConn)    Dim cb As SqlCommandBuilder = New SqlCommandBuilder(myDataAdapter)    myConn.Open()    Dim ds As DataSet = New DataSet    myDataAdapter.Fill(ds, myTableName)    ' Code to modify data in DataSet here     ' Without the SqlCommandBuilder this line would fail.    myDataAdapter.Update(ds, myTableName)    myConn.Close()End Function 'SelectSqlSrvRows[C#] public static DataSet SelectSqlSrvRows(string myConnection, string mySelectQuery, string myTableName){   SqlConnection myConn = new SqlConnection(myConnection);   SqlDataAdapter myDataAdapter = new SqlDataAdapter();   myDataAdapter.SelectCommand = new SqlCommand(mySelectQuery, myConn);   SqlCommandBuilder cb = new SqlCommandBuilder(myDataAdapter);   myConn.Open();   DataSet ds = new DataSet();   myDataAdapter.Fill(ds, myTableName);   //code to modify data in DataSet here   //Without the SqlCommandBuilder this line would fail   myDataAdapter.Update(ds, myTableName);   myConn.Close();   return ds;}

 

 

 

要求

命名空間: System.Data.SqlClient

平台: Windows 98, Windows NT 4.0, Windows ME, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 系列, .NET Framework 精簡版

程式集: System.Data (在 System.Data.dll 中)

請參見

SqlCommandBuilder 成員 | System.Data.SqlClient 命名空間

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.