VB..net CREATE TABLE
时间: 2024-09-24 11:29:52 浏览: 56
在VB.NET中,创建数据库表也使用SQL命令,虽然语法与标准SQL类似,但通过ADO.NET连接到数据库并执行查询操作。以下是一个基本的示例,展示如何使用`SqlCommand`来创建表:
```vb.net
Imports System.Data.SqlClient
Dim connectionString As String = "Data Source=YourServer;Initial Catalog=YourDatabase;Integrated Security=True;"
Dim commandText As String = "CREATE TABLE Employees (" +
"ID INT PRIMARY KEY," +
"FirstName VARCHAR(50), " +
"LastName VARCHAR(50)," +
"Email VARCHAR(100)" +
")"
Using connection As New SqlConnection(connectionString)
Using command As New SqlCommand(commandText, connection)
' 连接打开前先设置CommandType和Connection
command.CommandType = CommandType.Text
connection.Open()
' 执行SQL命令
Dim result As Integer = command.ExecuteNonQuery()
If result > 0 Then
Console.WriteLine("Table created successfully.")
Else
Console.WriteLine("Failed to create table.")
End If
End Using
End Using
```
这里的关键点是使用`SqlConnection`和`SqlCommand`对象,并确保在执行`ExecuteNonQuery()`之前设置了正确的数据库连接。
阅读全文