求助:VB中用adodc执行SQL语句的例子
VB中用adodc和datagrid控件执行SQL语句例子,一个简单的例子就可以,用adodc和datagrid控件,一个文本框内容来填写性别,例如:查询性别like='...
VB中用adodc和datagrid控件执行SQL语句例子,一个简单的例子就可以 ,用adodc和datagrid控件,一个文本框内容来填写性别,例如:查询 性别 like = '男'即可。不要写连接字符,直接说控件的连接就可以了!谢谢!
展开
3个回答
展开全部
下面是一个连接数据库的模块,可以使用它来连接SQL数据库,使用起来比ADO控件还简单:
代码如下:
Public rsNew As ADODB.Recordset
Public CnNew As ADODB.Connection
Public addFlag As Boolean
'连接数据库
Public Function OpenCn() As Boolean
Dim Mag As String
On Error GoTo strErrMag
Set CnNew = New ADODB.Connection
CnNew.ConnectionTimeout = 25
CnNew.Provider = "sqloledb"
CnNew.Properties("data source").value = "" 'SQL服务器的名
CnNew.Properties("initial catalog").value = "pubs"'库名
CnNew.Properties("integrated security").value = "SSPI"'登陆类型
'con.Properties("user id").value = "sa"
'con.Properties("password").value = "wwww"
CnNew.Open
OpenCn = True
addFlag = True
Exit Function
strErrMag:
Mag = "数据库末连接"
Call MsgBox(Mag, vbOKCancel, "错误:数据库连接")
addFlag = False
End
End Function
Public Sub Clocn()
'闭关数据库
On Error Resume Next
If CnNew.State <> adStateClosed Then CnNew.Close
Set CnNew = Nothing
End Sub
Public Function OpenRs(ByVal strSql As String) As Boolean
'连接数据库记录集
Dim Mag As String
Dim rpy As Boolean
On Error GoTo strErrMag
Set rsNew = New ADODB.Recordset
If addFlag = False Then rpy = OpenCn
With rsNew
.ActiveConnection = CnNew
.CursorLocation = adUseClient
.CursorType = adOpenKeyset
.LockType = adLockOptimistic
.Open strSql
End With
addFlag = True
OpenRs = True
Exit Function
strErrMag:
Mag = "数据库数据集末连接"
Call MsgBox(Mag, vbOKOnly, "错误:数据库连接")
OpenRs = False
End
End Function
Public Sub cloRs()
'闭关数据集
On Error Resume Next
If rsNew.State <> adStateClosed Then rsNew.Close
Set rsNew = Nothing
End Sub
只要在你的标准模块中加入该代码,你就可以使用其中的函数来连接数据源了。
代码如下:
Public rsNew As ADODB.Recordset
Public CnNew As ADODB.Connection
Public addFlag As Boolean
'连接数据库
Public Function OpenCn() As Boolean
Dim Mag As String
On Error GoTo strErrMag
Set CnNew = New ADODB.Connection
CnNew.ConnectionTimeout = 25
CnNew.Provider = "sqloledb"
CnNew.Properties("data source").value = "" 'SQL服务器的名
CnNew.Properties("initial catalog").value = "pubs"'库名
CnNew.Properties("integrated security").value = "SSPI"'登陆类型
'con.Properties("user id").value = "sa"
'con.Properties("password").value = "wwww"
CnNew.Open
OpenCn = True
addFlag = True
Exit Function
strErrMag:
Mag = "数据库末连接"
Call MsgBox(Mag, vbOKCancel, "错误:数据库连接")
addFlag = False
End
End Function
Public Sub Clocn()
'闭关数据库
On Error Resume Next
If CnNew.State <> adStateClosed Then CnNew.Close
Set CnNew = Nothing
End Sub
Public Function OpenRs(ByVal strSql As String) As Boolean
'连接数据库记录集
Dim Mag As String
Dim rpy As Boolean
On Error GoTo strErrMag
Set rsNew = New ADODB.Recordset
If addFlag = False Then rpy = OpenCn
With rsNew
.ActiveConnection = CnNew
.CursorLocation = adUseClient
.CursorType = adOpenKeyset
.LockType = adLockOptimistic
.Open strSql
End With
addFlag = True
OpenRs = True
Exit Function
strErrMag:
Mag = "数据库数据集末连接"
Call MsgBox(Mag, vbOKOnly, "错误:数据库连接")
OpenRs = False
End
End Function
Public Sub cloRs()
'闭关数据集
On Error Resume Next
If rsNew.State <> adStateClosed Then rsNew.Close
Set rsNew = Nothing
End Sub
只要在你的标准模块中加入该代码,你就可以使用其中的函数来连接数据源了。
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
请将数据库路径及名称改为你自己的数据库据库路径及名称。将Text1的属性页的属性DataSource设置Adodc1,属性DataField设写为性别。将SQL语句的表名改为你的数据表名。代码如下:
sql = "select * from b1 Where 性别 Like'" & Text1 & "%'"
Adodc1.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=D:\Program Files\Microsoft Visual Studio\VB98\管理系统.mdb;Persist Security Info=False"
Adodc1.RecordSource = sql
Adodc1.Refresh
DataGrid1.Refresh
sql = "select * from b1 Where 性别 Like'" & Text1 & "%'"
Adodc1.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=D:\Program Files\Microsoft Visual Studio\VB98\管理系统.mdb;Persist Security Info=False"
Adodc1.RecordSource = sql
Adodc1.Refresh
DataGrid1.Refresh
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
Adodc.connectionstring="........."
Adodc.CommandType = adCmdText
Adodc.RecordSource = "select * from 表 WHERE 性别 = '" & text1.text & "'" '性别好像就不必用like了吧
Adodc.Refresh '这一句相当的重要.
Set DataGrid.DataSource = Adodc
希望对你有所帮助.如果满意我的回答,请采纳.谢谢
Adodc.CommandType = adCmdText
Adodc.RecordSource = "select * from 表 WHERE 性别 = '" & text1.text & "'" '性别好像就不必用like了吧
Adodc.Refresh '这一句相当的重要.
Set DataGrid.DataSource = Adodc
希望对你有所帮助.如果满意我的回答,请采纳.谢谢
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询