大橙子网站建设,新征程启航
为企业提供网站建设、域名注册、服务器等服务
在系统中建立ADO公共查询模块
创新互联是一家集网站建设,凤台企业网站建设,凤台品牌网站建设,网站定制,凤台网站建设报价,网络营销,网络优化,凤台网站推广为一体的创新建站企业,帮助传统企业提升企业形象加强企业竞争力。可充分满足这一群体相比中小企业更为丰富、高端、多元的互联网需求。同时我们时刻保持专业、时尚、前沿,时刻以成就客户成长自我,坚持不断学习、思考、沉淀、净化自己,让我们为更多的企业打造出实用型网站。
Public Function ExecuteSQL(ByVal SQL _
As String, MsgString As String) _
As ADODB.Recordset '执行SQL语句并返回Recordset对象
Dim cnn As ADODB.Connection
Dim rst As ADODB.Recordset
Dim sTokens() As String
On Error GoTo ExecuteSQL_Error
sTokens = Split(SQL)
Set cnn = New ADODB.Connection
cnn.Open CONN
'打开连接
If InStr("INSERT,DELETE,UPDATE", _
UCase$(sTokens(0))) Then
cnn.Execute SQL
MsgString = sTokens(0) _
" 查询成功"
Else
Set rst = New ADODB.Recordset
rst.Open Trim$(SQL), cnn, _
adOpenKeyset, _
adLockOptimistic
Set ExecuteSQL = rst
MsgString = "查询到" rst.RecordCount _
" 条记录 "
'获取记录的条数
End If
ExecuteSQL_Exit:
Set rst = Nothing
Set cnn = Nothing
Exit Function
ExecuteSQL_Error:
MsgString = "查询错误: " _
Err.Description
Resume ExecuteSQL_Exit
End Function
然后再应用模块中就可以使用
Dim txtSQL As String
Dim mrc As ADODB.Recordset
Dim MsgText As String
txtSQL = "select * from ... "
Set mrc = ExecuteSQL(txtSQL, MsgText)
查询了
1、打开代码窗口,添加引用:Imports System.Data.SqlClient。
2、输入以下代码:
“Public conn1 As SqlConnection = New SqlConnection _
("server=192.168.1.79; Initial Catalog= student; User ID= panqe;PWD=shentai768@")”,vb就已经成功连接sql数据库了。
3、代码详解:声明关键字Public(因为是全局变量,所以用Public 来声明)。
4、连接参数。
5、如果SQL 数据库就在本机,则用以下代码连接:
("server=.; Integrated Security=False;Initial Catalog= student; User ID= panqe;PWD=shentai768@")。
6:如果代码太长,影响可读性,可以用空格加"_"后,回车换行。
Public myconn As New ADODB.Connection
Public myrecord As New ADODB.Recordset
Public Sub main()
gdatabasepassword = 数据库密码
gdatabaseusrname = 数据库用户名
gdatabasename = 表名
gipstr = 服务器IP地址
connstring = "Provider=SQLOLEDB.1;Password=" gdatabasepassword ";Persist Security Info=True;User ID=" gdatabaseusrname ";Initial Catalog=" gdatabasename ";Data Source=" gipstr
Set myconn = New ADODB.Connection
myconn.ConnectionString = connstring
myconn.Open
至此数据库就连接上了,可以访问指定的数据表了
窗体文件里这样写
Private Sub Form_Load()
Dim mysql As String
'On Error GoTo errortrap2
connstring = "Provider=SQLOLEDB.1;Password=" gdatabasepassword ";Persist Security Info=True;User ID=" gdatabaseusrname ";Initial Catalog=" gdatabasename ";Data Source=" gipstr
'Debug.Print connstring
Set myconn = New ADODB.Connection
mysql = "select data , BedCode,CODE,PatientName,UpdateTime from MachineData "
'mysql = "select identitycard,sex from patient where name='" List1.Text "'"
myconn.ConnectionString = connstring
myconn.Open
Set myrecord = New ADODB.Recordset
'myrecord.Open "patient", myconn, adOpenDynamic, adLockReadOnly
myrecord.Open mysql, myconn, adOpenDynamic, adLockReadOnly
With myrecord
While Not .EOF()
List1.AddItem .Fields("CODE") "--" .Fields("BedCode") "--" .Fields("PatientName")
List2.AddItem .Fields("DATA")
List3.AddItem .Fields("UpdateTime")
.MoveNext
Wend
End With
GoTo normalend2:
errortrap2: MsgBox "严重错误!!!"
End
normalend2:
myrecord.Close
'''''''''''''''''''''''''''''''''
End Sub