vb中如何执行SQL语句
在text1中输入SQL查询语句(查询表yuanbiao中的数据),单击command1,查询结果以表的形式在DataGrid1中表示出来(新表名为xinbiao),怎样...
在text1中输入SQL查询语句(查询表yuanbiao中的数据),单击command1,查询结果以表的形式在DataGrid1中表示出来(新表名为xinbiao),怎样设置?代码怎样写?(我用的是access数据库)
展开
1个回答
2013-07-03
展开全部
Private Sub Command1_Click()
On Error GoTo errhandle
Dim conn As New ADODB.Connection, rs As New ADODB.Recordset
If Trim(Text1.Text) = "" Then
MsgBox "没有输入sql语句"
Exit Sub
Else
If Left(Trim(Text1.Text), 6) = "select" Then
MsgBox "不是查询语句"
Exit Sub
End If
End If
conn.Open "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & App.Path & "test.mdb;Persist Security Info=False"
rs.Open Trim(Text1.Text), conn, adOpenKeyset, adLockOptimistic
Set DataGrid1.DataSource = rs
DataGrid1.Refresh
Set rs = Nothing
Set conn = Nothing
rs.Close
conn.Close
Exit Sub
errhandle:
MsgBox "输入的sql语句有误"
End Sub
On Error GoTo errhandle
Dim conn As New ADODB.Connection, rs As New ADODB.Recordset
If Trim(Text1.Text) = "" Then
MsgBox "没有输入sql语句"
Exit Sub
Else
If Left(Trim(Text1.Text), 6) = "select" Then
MsgBox "不是查询语句"
Exit Sub
End If
End If
conn.Open "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & App.Path & "test.mdb;Persist Security Info=False"
rs.Open Trim(Text1.Text), conn, adOpenKeyset, adLockOptimistic
Set DataGrid1.DataSource = rs
DataGrid1.Refresh
Set rs = Nothing
Set conn = Nothing
rs.Close
conn.Close
Exit Sub
errhandle:
MsgBox "输入的sql语句有误"
End Sub
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询