数据库开发002测试SQL语句
程序员文章站
2022-05-07 14:36:14
...
Imports System.Data.OleDb
Public Class Form1
Dim conn As OleDbConnection
Dim comm As OleDbCommand
Dim dataAdapter As OleDbDataAdapter
Dim ds As New DataSet
Dim dt As New DataTable
Private Function GetConnection() As OleDbConnection
Return New OleDbConnection(My.Settings.personConnectionString)
End Function
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Try
conn = GetConnection()
'Dim strComm As String = ""'这里需要输入完整的SQL语句
'Dim strComm As String = "select * from grade where 姓名 like '%" & TextBox1.Text & "%'"
'Dim strComm As String = insert into grade(学号,姓名,数学) values (99,'小蔡',128)
'Dim strComm As String = delete from grade where 姓名='小蔡'
'Dim strComm As String = update grade set 数学=165 where 姓名='小蔡'
conn.Open()
comm = New OleDbCommand(TextBox1.Text, conn)
comm.ExecuteNonQuery()
conn.Close()
MsgBox("成功")
Catch ex As Exception
MsgBox("有错")
End Try
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
ds.Clear()
DataGridView1.DataSource = ds
conn = GetConnection()
Dim strComm As String = "select * from grade"
dataAdapter = New OleDbDataAdapter(strComm, conn)
dataAdapter.Fill(ds, "grade")
DataGridView1.DataSource = ds.Tables("grade")
DataGridView1.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill
End Sub
Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
ds.Clear()
DataGridView1.DataSource = ds
'Dim strComm As String = "select * from grade where 性别='男'"
Dim strComm As String = "select * from grade where 姓名 like '%" & TextBox1.Text & "%'"
'Dim strComm As String =insert into grade(学号,姓名,数学) values (99,'小蔡',128)
'Dim strComm As String =delete from grade where 姓名='小蔡'
'Dim strComm As String =update grade set 数学=165 where 姓名='小蔡'
'Dim strComm As String = TextBox1.Text '
dataAdapter = New OleDbDataAdapter(strComm, GetConnection)
dataAdapter.Fill(ds, "grade")
DataGridView1.DataSource = ds.Tables("grade")
DataGridView1.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill
End Sub
End Class