VB6连接各种类型的数据库
一、连接VFP数据库
-
Dim CNN As New ADODB.Connection
-
Dim rssys As New ADODB.Recordset
-
If CNN.state = 1 Then CNN.Close
-
CNN.ConnectionString = "Driver={Microsoft Visual FoxPro Driver};SourceType=.DBc;SourceDb=" + Trim(Text1)
-
CNN.Open
-
If CNN.state = 1 Then
-
rssys.CursorLocation = adUseClient
-
rssys.Open "select * from sys ", CNN, adOpenDynamic, adLockReadOnly
-
End If
二、连接Ms SQL数据库
-
Dim CNN As New ADODB.Connection
-
If loginid = 0 Then
-
CNN.ConnectionString = "Provider=SQLOLEDB.1;Integrated Security=SSPI;Persist Security Info=False;Initial Catalog=RongShiYKT;Data Source=" & Trim(sqlserver)
-
ElseIf loginid = 1 Then
-
CNN.ConnectionString = "Provider=SQLOLEDB.1;Persist Security Info=False;User ID=sa;pwd=123456;Initial Catalog=RongShiYKT" & ";Data Source=" & Trim(sqlserver)
-
End If
-
CNN.Mode = adModeShareDenyNone
-
CNN.CommandTimeout = 0
-
CNN.Open
-
Dim cmd As New ADODB.Command
-
Set cmd.ActiveConnection = cn
-
cmd.CommandType = adCmdStoredProc
-
cmd.CommandText = "DealMealData"
-
cmd.Parameters(1) = rscx.Fields("ID")
-
cmd.Execute
-
三、连接Access数据库
-
Dim CNN As New ADODB.Connection
-
CNN.ConnectionString = "PROVIDER=Microsoft.Jet.OLEDB.4.0;Data Source= " & App.Path & "\Elevator.mdb;Persist Security Info=true;Jet OLEDB:Database Password=123456"
-
If CNN.State = 1 Then CNN.Close
-
CNN.Open
四、连接MySql数据库
-
Dim CNN As New ADODB.Connection
-
Dim mysqlstor As New ADODB.Command
-
CNN.ConnectionString = " DRIVER={MySQL ODBC 5.2 unicode Driver};" & "SERVER=" & hostserver & "; DATABASE=" & dbserver & ";UID=" & user & ";PWD=" & pwd & "; OPTION=3"
-
If CNN.State = 1 Then CNN.Close
-
CNN.Open
-
CREATE DEFINER=`rsadmin`@`%` PROCEDURE `Stor_Add_Cust`(IN Cust0 varchar(60) character set gbk,
-
IN Cont1 varchar(20) character set gbk,
-
IN Mobi2 varchar(15) character set gbk,
-
IN Tel3 varchar(15) character set gbk,
-
IN Fax4 varchar(15) character set gbk,
-
IN QQ5 varchar(15) character set gbk,
-
IN Addr6 varchar(80) character set gbk,
-
IN ShopA7 varchar(60) character set gbk,
-
IN ShopT8 varchar(15) character set gbk,
-
IN ShopF9 varchar(15) character set gbk,
-
IN Email10 varchar(30) character set gbk,
-
IN Area111 varchar(18) character set gbk,
-
IN Rema12 varchar(80) character set gbk,
-
IN User13 int,
-
IN Remind14 varchar(250) character set gbk,
-
IN WebId15 int,
-
OUT KHBHStr varchar(10),OUT CustId int)
-
BEGIN /* 增加一个新客户*/
-
select max(custoCode) into @xfdh from customer;
-
set @xfdh= LPAD(right( @xfdh,6)+1,10,'KHBH0000000000');
-
set KHBHStr=@xfdh;
-
if not isnull(Cust0) then
-
Insert into Customer(CustoCode,CustoName,AreaCode,Mobile,Address,Fax,Contact,Tel,QQ,Remark,ShopAdd,ShopTel,ShopFax,E_MAIL,inputdate,UserID,Remind,WebstaId) values(@xfdh,Cust0,Area111,Mobi2,Addr6,Fax4,Cont1,Tel3,QQ5 ,Rema12,ShopA7,ShopT8,ShopF9,Email10,sysdate(),User13,Remind14,WebId15);
-
select Id into CustId from customer where CustoCode=@xfdh;
-
end if;
-
END;
-
mysqlstor.ActiveConnection = CNN
-
mysqlstor.CommandText = "Stor_Add_Cust"
-
mysqlstor.CommandType = adCmdStoredProc
-
mysqlstor.Prepared = True
-
mysqlstor.Parameters(0) = Trim(Text4.Text)
-
mysqlstor.Parameters(1) = Trim(Text6.Text)
-
mysqlstor.Parameters(2) = Trim(Text12.Text)
-
mysqlstor.Parameters(3) = Trim(Text3.Text)
-
mysqlstor.Parameters(4) = Trim(Text2.Text)
-
mysqlstor.Parameters(5) = Trim(Text11.Text)
-
mysqlstor.Parameters(6) = Trim(Text5.Text)
-
mysqlstor.Parameters(7) = Trim(Text13.Text)
-
mysqlstor.Parameters(8) = Trim(Text15.Text)
-
mysqlstor.Parameters(9) = Trim(Text14.Text)
-
mysqlstor.Parameters(10) = Trim(Text7.Text)
-
mysqlstor.Parameters(11) = selnewbm
-
mysqlstor.Parameters(12) = Trim(Text8.Text)
-
mysqlstor.Parameters(13) = czyid
-
mysqlstor.Parameters(14) = Trim(Text16.Text)
-
mysqlstor.Parameters(15) = Val(Mid(Combo4.Text, 1, 3))
-
mysqlstor.Parameters(16) = ""
-
mysqlstor.Parameters(17) = 0
-
mysqlstor.Execute
-
dwbh = mysqlstor.Parameters(16)
-
selekhid = mysqlstor.Parameters(16)
-
selcustid = mysqlstor.Parameters(17)
-
sqlstr = "call Stor_InOutMat('" & selck & "'," & Val(Label20.Caption) & "," & Val(Text10) & ",'" & czymc & "','" & Trim(Text11) & "'," & czyid & ")"
-
CNN.Execute sqlstr
-
五、连接Oracle数据库
-
Dim CNN As New ADODB.Connection
-
CNN.ConnectionString = "Driver={Microsoft ODBC for Oracle};Server=SvrName;UID=UserID;PWD=Password;"
-
If CNN.State = 1 Then CNN.Close
-
CNN.Open
六、通过.udl通用数据连接文件连接数据库
-
Dim cn As New ADODB.Connection
-
cn.ConnectionString = "File Name=" & App.Path & "\ConnectDatabase.udl;"
-
cn.Mode = adModeShareDenyNone
-
cn.CommandTimeout = 0
-
cn.Open
七、编辑修改.udl通用数据连接文件
-
Dim s As String
-
Dim filename As String
-
Dim BytesBuffer() As Byte
-
Dim i As Long
-
filename = App.Path & "\ConnectDatabase.udl"
-
On Error Resume Next
-
s = "[oledb]" & vbCrLf & "; Everything after this line is an OLE DB initstring" & vbCrLf & "Provider=SQLOLEDB.1;Integrated Security=SSPI;Persist Security Info=False;User ID=sa;Initial Catalog=master;Data Source=(local)" & vbCrLf
-
BytesBuffer = VBA.StrConv(VBA.StrConv(s, vbUnicode), vbFromUnicode)
-
ReDim BytesBuffer0(1) As Byte
-
BytesBuffer0(0) = 255
-
BytesBuffer0(1) = 254
-
If VBA.Len(VBA.Trim(VBA.Dir(filename))) > 0 Then
-
DeleteFile filename
-
End If
-
i = VBA.FreeFile
-
Open filename For Binary Access Write As #i
-
Put #i, , BytesBuffer0
-
Put #i, , BytesBuffer
-
Close #i