vb.net create table
时间: 2024-01-18 12:17:38 浏览: 154
在VB.NET中创建表格可以使用ADO.NET来实现。以下是一个示例代码:
```vb
Imports System.Data.SqlClient
Public Class Form1
Private Sub btnCreateTable_Click(sender As Object, e As EventArgs) Handles btnCreateTable.Click
Dim connectionString As String = "Data Source=YourServerName;Initial Catalog=YourDatabaseName;Integrated Security=True"
Dim tableName As String = "YourTableName"
Using connection As New SqlConnection(connectionString)
connection.Open()
Dim createTableQuery As String = "CREATE TABLE " & tableName & " (ID INT PRIMARY KEY, Name VARCHAR(50), Age INT)"
Dim command As New SqlCommand(createTableQuery, connection)
command.ExecuteNonQuery()
MessageBox.Show("Table created successfully.")
connection.Close()
End Using
End Sub
End Class
```
这段代码使用了`SqlConnection`类来建立与数据库的连接,并使用`SqlCommand`类执行SQL语句来创建表格。你需要将`YourServerName`替换为你的服务器名称,`YourDatabaseName`替换为你的数据库名称,`YourTableName`替换为你想要创建的表格名称。
阅读全文