VB6连接各种类型的数据库

消费机介绍:https://item.taobao.com/item.htm?spm=a1z10.5-c.w4002-17663462238.23.363e58818tCwi2&id=21758416134https://item.taobao.com/item.htm?spm=a1z10.5-c.w4002-17663462238.23.363e58818tCwi2&id=21758416134 

一、连接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

参与评论 您还未登录,请先 登录 后发表或查看评论
相关推荐

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
©️2022 CSDN 皮肤主题:大白 设计师:CSDN官方博客 返回首页

打赏作者

津津有味道

你的鼓励将是我创作的最大动力

¥2 ¥4 ¥6 ¥10 ¥20
输入1-500的整数
余额支付 (余额:-- )
扫码支付
扫码支付:¥2
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、C币套餐、付费专栏及课程。

余额充值