共用方式為


HOW TO:在資料庫中插入新的資料錄

更新:2007 年 11 月

若要在資料庫中插入新的資料錄,您可以使用 TableAdapter.Update 方法,或其中一個 TableAdapter 的 DBDirect 方法 (尤其是 TableAdapter.Insert 方法)。如需詳細資訊,請參閱 TableAdapter 概觀

如果您的應用程式並未使用 TableAdapter,您就可以使用命令物件來進行互動並在資料庫中插入新的資料錄 (例如,SqlCommand)。

當您的應用程式使用資料集儲存資料時,請使用 TableAdapter.Update 方法。Update 方法會將所有變更 (更新、插入和刪除) 傳送至資料庫中。

當您的應用程式使用物件儲存資料,或是您想要更精密地控制在資料庫中建立新資料錄的方式時,請使用 TableAdapter.Insert 方法。

如果 TableAdapter 並沒有任何 Insert 方法,就是表示此 TableAdapter 是設定成使用預存程序,或者其 GenerateDBDirectMethods 屬性設定為 false。請嘗試從 DataSet 設計工具之內,將 TableAdapter 的 GenerateDBDirectMethods 屬性設定為 true,然後儲存資料集以重新產生 TableAdapter。如果 TableAdapter 仍然沒有 Insert 方法,則此資料表可能無法提供足夠的結構描述資訊,以區別個別的資料列 (例如,資料表上沒有設定任何主索引鍵)。

使用 TableAdapter 插入新的資料錄

TableAdapter 會根據應用程式的需求,提供不同的方式來將新資料錄插入資料庫中。

如果您的應用程式使用資料集儲存資料,您就只要在資料集中將新資料錄加入至所需的 DataTable,然後呼叫 TableAdapter.Update 方法即可。TableAdapter.Update 方法會採用 DataTable 中的任何變更,並將這些變更傳送至資料庫中 (包括已修改和已刪除的資料錄)。

若要使用 TableAdapter.Update 方法將新資料錄插入資料庫中

  1. 將新資料錄加入至所需的 DataTable,方法是建立新的 DataRow 並將它加入至 Rows 集合中。如需詳細資訊,請參閱 HOW TO:將資料列加入至 DataTable

  2. 將新資料列加入至 DataTable 之後,請呼叫 TableAdapter.Update 方法。您可以傳入整個 DataSetDataTableDataRow 的陣列,或是單一 DataRow,藉以控制要更新的資料量。

    下列程式碼將示範如何將新資料錄加入至 DataTable,然後呼叫 TableAdapter.Update 方法,將新資料列儲存至資料庫中 (此範例會使用 Northwind 資料庫的 Region 資料表)。

    ' Create a new row.
    Dim newRegionRow As NorthwindDataSet.RegionRow
    newRegionRow = Me.NorthwindDataSet._Region.NewRegionRow()
    newRegionRow.RegionID = 5
    newRegionRow.RegionDescription = "NorthWestern"
    
    ' Add the row to the Region table
    Me.NorthwindDataSet._Region.Rows.Add(newRegionRow)
    
    ' Save the new row to the database
    Me.RegionTableAdapter.Update(Me.NorthwindDataSet._Region)
    
    // Create a new row.
    NorthwindDataSet.RegionRow newRegionRow;
    newRegionRow = northwindDataSet.Region.NewRegionRow();
    newRegionRow.RegionID = 5;
    newRegionRow.RegionDescription = "NorthWestern";
    
    // Add the row to the Region table
    this.northwindDataSet.Region.Rows.Add(newRegionRow);
    
    // Save the new row to the database
    this.regionTableAdapter.Update(this.northwindDataSet.Region);
    

如果您的應用程式會使用物件在應用程式中儲存資料,您就可以使用 TableAdapter.Insert 方法,直接在資料庫中建立新的資料列。Insert 方法會接受每個資料行的個別值做為參數。當您呼叫此方法時,就會使用傳入的參數值,將新的資料錄插入資料庫中。

下列程序會使用 Northwind 資料庫的 Region 資料表做為範例。

若要使用 TableAdapter.Insert 方法將新資料錄插入資料庫中

  • 請呼叫 TableAdapter 的 Insert 方法,並傳入每個資料行的值做為參數。

    注意事項:

    如果您沒有可用的執行個體,請針對您想使用的 TableAdapter 執行個體化。

    Dim regionTableAdapter As New NorthwindDataSetTableAdapters.RegionTableAdapter
    
    regionTableAdapter.Insert(5, "NorthWestern")
    
    NorthwindDataSetTableAdapters.RegionTableAdapter regionTableAdapter = 
        new NorthwindDataSetTableAdapters.RegionTableAdapter();
    
    regionTableAdapter.Insert(5, "NorthWestern");
    

使用命令物件插入新資料錄

下列範例會使用命令物件,直接將資料錄插入資料庫中。如需使用命令物件來執行命令和預存程序 (Stored Procedure) 的詳細資訊,請參閱將資料擷取至您的應用程式中

下列程序會使用 Northwind 資料庫的 Region 資料表做為範例。

若要使用命令物件將新資料錄插入資料庫中

  • 建立新的命令物件,然後設定其 Connection、CommandType 和 CommandText 屬性。

    Dim sqlConnection1 As New System.Data.SqlClient.SqlConnection("YOUR CONNECTION STRING")
    
    Dim cmd As New System.Data.SqlClient.SqlCommand
    cmd.CommandType = System.Data.CommandType.Text
    cmd.CommandText = "INSERT Region (RegionID, RegionDescription) VALUES (5, 'NorthWestern')"
    cmd.Connection = sqlConnection1
    
    sqlConnection1.Open()
    cmd.ExecuteNonQuery()
    sqlConnection1.Close()
    
    System.Data.SqlClient.SqlConnection sqlConnection1 = 
        new System.Data.SqlClient.SqlConnection("YOUR CONNECTION STRING");
    
    System.Data.SqlClient.SqlCommand cmd = new System.Data.SqlClient.SqlCommand();
    cmd.CommandType = System.Data.CommandType.Text;
    cmd.CommandText = "INSERT Region (RegionID, RegionDescription) VALUES (5, 'NorthWestern')";
    cmd.Connection = sqlConnection1;
    
    sqlConnection1.Open();
    cmd.ExecuteNonQuery();
    sqlConnection1.Close();
    

安全性

您必須擁有嘗試連接之資料庫的存取權,以及在所需資料表中執行插入的使用權限。

請參閱

工作

HOW TO:刪除資料庫中的資料錄

HOW TO:更新資料庫中的資料錄

HOW TO:從物件中將資料儲存至資料庫

其他資源

資料存取使用者入門

連接至 Visual Studio 中的資料

準備您的應用程式以接收資料

將資料擷取至您的應用程式中

顯示 Windows 應用程式之表單上的資料

在您的應用程式中編輯資料

驗證資料

儲存資料