執行資料庫目錄作業
若要執行 CREATE TABLE 或 CREATE PROCEDURE 陳述式等命令來修改資料庫或目錄,請使用適當的 SQL 陳述式與 Connection 物件來建立 Command 物件。 使用 Command 物件的 ExecuteNonQuery 方法執行命令。
下列程式碼範例會在 Microsoft SQL Server 資料庫中建立預存程序。
' Assumes connection is a valid SqlConnection.
Dim queryString As String = "CREATE PROCEDURE InsertCategory " & _
"@CategoryName nchar(15), " & _
"@Identity int OUT " & _
"AS " & _
"INSERT INTO Categories (CategoryName) VALUES(@CategoryName) " & _
"SET @Identity = @@Identity " & _
"RETURN @@ROWCOUNT"
Dim command As SqlCommand = New SqlCommand(queryString, connection)
command.ExecuteNonQuery()
// Assumes connection is a valid SqlConnection.
string queryString = "CREATE PROCEDURE InsertCategory " +
"@CategoryName nchar(15), " +
"@Identity int OUT " +
"AS " +
"INSERT INTO Categories (CategoryName) VALUES(@CategoryName) " +
"SET @Identity = @@Identity " +
"RETURN @@ROWCOUNT";
SqlCommand command = new SqlCommand(queryString, connection);
command.ExecuteNonQuery();
請參閱
- 使用命令修改資料
- 命令和參數
- ADO.NET 概觀 \(部分機器翻譯\)