執行資料庫目錄作業 (ADO.NET)
更新: November 2007
若要執行 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();