怎样用VB添加数据库表,删除记录
1个回答
展开全部
'引入命名空间
Imports System.Data.SqlClient
'建立数据连接
Dim strcon As String = "integrated security=sspi;" & "server=(local);database=master"
Dim mycon As New SqlConnection(strcon)
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
'建立数据命令
Dim mycmd As New SqlCommand
mycmd.Connection = mycon
mycmd.CommandType = CommandType.Text
mycmd.CommandText = "create database student on" & _
"( NAME = student_dat,FILENAME = 'D:\data\student.mdf')" & _
"log on" & _
"( NAME = 'student_log'," & " FILENAME = 'D:\data\student.ldf')"
mycon.Open()
'执行数据命令
Try
mycmd.ExecuteNonQuery()
MsgBox("数据库创建成功!")
Catch ex As Exception
MessageBox.Show(ex.Message, "请注意!", MessageBoxButtons.OK, MessageBoxIcon.Stop)
Finally
End Try
'关闭连接
mycon.Close()
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
Dim mycmd1 As New SqlCommand
mycmd1.Connection = mycon
mycmd1.CommandType = CommandType.Text
mycmd1.CommandText = "CREATE TABLE info(学号 int,姓名 varchar(50) NOT NULL,班级 varchar(50),出生年月 datetime,户籍 varchar(50) null)"
mycon.Open()
'执行数据命令
Try
mycmd1.ExecuteNonQuery()
MsgBox("表创建成功!")
Catch ex As Exception
MessageBox.Show(ex.Message, "请注意!", MessageBoxButtons.OK, MessageBoxIcon.Stop)
Finally
End Try
'关闭连接
mycon.Close()
End Sub
Imports System.Data.SqlClient
'建立数据连接
Dim strcon As String = "integrated security=sspi;" & "server=(local);database=master"
Dim mycon As New SqlConnection(strcon)
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
'建立数据命令
Dim mycmd As New SqlCommand
mycmd.Connection = mycon
mycmd.CommandType = CommandType.Text
mycmd.CommandText = "create database student on" & _
"( NAME = student_dat,FILENAME = 'D:\data\student.mdf')" & _
"log on" & _
"( NAME = 'student_log'," & " FILENAME = 'D:\data\student.ldf')"
mycon.Open()
'执行数据命令
Try
mycmd.ExecuteNonQuery()
MsgBox("数据库创建成功!")
Catch ex As Exception
MessageBox.Show(ex.Message, "请注意!", MessageBoxButtons.OK, MessageBoxIcon.Stop)
Finally
End Try
'关闭连接
mycon.Close()
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
Dim mycmd1 As New SqlCommand
mycmd1.Connection = mycon
mycmd1.CommandType = CommandType.Text
mycmd1.CommandText = "CREATE TABLE info(学号 int,姓名 varchar(50) NOT NULL,班级 varchar(50),出生年月 datetime,户籍 varchar(50) null)"
mycon.Open()
'执行数据命令
Try
mycmd1.ExecuteNonQuery()
MsgBox("表创建成功!")
Catch ex As Exception
MessageBox.Show(ex.Message, "请注意!", MessageBoxButtons.OK, MessageBoxIcon.Stop)
Finally
End Try
'关闭连接
mycon.Close()
End Sub
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询