共用方式為


SqlCeResultSet.Insert 方法 (SqlCeUpdatableRecord)

在基礎資料列集中插入指定的 SqlCeUpdatableRecord

命名空間:  System.Data.SqlServerCe
組件:  System.Data.SqlServerCe (在 System.Data.SqlServerCe.dll 中)

語法

'宣告
Public Sub Insert ( _
    record As SqlCeUpdatableRecord _
)
'用途
Dim instance As SqlCeResultSet
Dim record As SqlCeUpdatableRecord

instance.Insert(record)
public void Insert(
    SqlCeUpdatableRecord record
)
public:
void Insert(
    SqlCeUpdatableRecord^ record
)
member Insert : 
        record:SqlCeUpdatableRecord -> unit 
public function Insert(
    record : SqlCeUpdatableRecord
)

參數

備註

插入資料錄不會變更 SqlCeResultSet 目前的位置。

呼叫這個函式會立即執行插入,而不需要其他呼叫來完成作業。

如果指定資料錄的資料行比基礎資料列集定義的資料行還多,將會擲回例外狀況。如果指定的值比定義的資料行還少,則其餘資料行必須允許 null 值,或定義預設值。

如果在不可更新的 SqlCeResultSet 上呼叫此函數,或者讀取器不是位於有效的資料錄上,就會擲回 InvalidOperationException

範例

Dim conn As SqlCeConnection = Nothing

Try
    File.Delete("Test.sdf")

    Dim engine As New SqlCeEngine("Data Source = Test.sdf")
    engine.CreateDatabase()

    conn = New SqlCeConnection("Data Source = Test.sdf")
    conn.Open()

    Dim cmd As SqlCeCommand = conn.CreateCommand()
    cmd.CommandText = "CREATE TABLE myTable (col1 INT)"
    cmd.ExecuteNonQuery()

    cmd.CommandText = "SELECT * FROM myTable"

    Dim rs As SqlCeResultSet = cmd.ExecuteResultSet(ResultSetOptions.Updatable Or ResultSetOptions.Scrollable)

    Dim rec As SqlCeUpdatableRecord = rs.CreateRecord()

    ' Insert 10 records
    '
    Dim i As Integer
    For i = 0 To 9
        rec.SetInt32(0, i)
        rs.Insert(rec)
    Next i

    ' Update the 5th record
    '
    If True = rs.ReadAbsolute(5) Then
        rs.SetInt32(0, 55)
        rs.Update()
    End If

    ' Delete the 2nd record
    '
    If True = rs.ReadAbsolute(2) Then
        rs.Delete()
    End If
Catch e As Exception
    MessageBox.Show(e.Message)
Finally
    conn.Close()
End Try
SqlCeConnection conn = null;

try
{
    File.Delete("Test.sdf");

    SqlCeEngine engine = new SqlCeEngine("Data Source = Test.sdf");
    engine.CreateDatabase();

    conn = new SqlCeConnection("Data Source = Test.sdf");
    conn.Open();

    SqlCeCommand cmd = conn.CreateCommand();
    cmd.CommandText = "CREATE TABLE myTable (col1 INT)";
    cmd.ExecuteNonQuery();

    cmd.CommandText = "SELECT * FROM myTable";

    SqlCeResultSet rs = cmd.ExecuteResultSet(ResultSetOptions.Updatable |
        ResultSetOptions.Scrollable);

    SqlCeUpdatableRecord rec = rs.CreateRecord();

    // Insert 10 records
    //
    for (int i = 0; i < 10; i++)
    {
        rec.SetInt32(0, i);
        rs.Insert(rec);
    }

    // Update the 5th record
    //
    if (true == rs.ReadAbsolute(5))
    {
        rs.SetInt32(0, 55);
        rs.Update();
    }

    // Delete the 2nd record
    //
    if (true == rs.ReadAbsolute(2))
    {
        rs.Delete();
    }
}
catch (Exception e)
{
    MessageBox.Show(e.Message);
}
finally
{
    conn.Close();
}

請參閱

參考

SqlCeResultSet 類別

Insert 多載

System.Data.SqlServerCe 命名空間