由于ADO对象不直接支持MySQL,所以必须先安装MyODBC, 后者也是一个免费产品,在 www.mysql.org上有下载,安装好了MyODBC, 就可以在ODBC数据源管理中配置一个数据源名称,把它指向你想连接的MySQL数据库。代码如下:Sub connectMySQL() '通过MyODBC去连接MySQL数据库,并将Microsoft SQL Server 7 '的数据转进mysql中 Dim sConnect As String, sSql As String, i As Long Dim cnMSSQL As New ADODB.Connection Dim cnMySQL As New ADODB.Connection '声明并创建对象 连接 Dim rs As New ADODB.Recordset '声明并创建对象 记录集 Dim cm As New ADODB.Command '声明并创建对象 命令 sConnect = "dsn=mysql1" '指定MySQL的数据源名称 cnMySQL.Open sConnect '连接到 mysql sConnect="Provider=SQLOLEDB.1;Persist Security Info=False;User ID=sa;pwd=123456;Initial Catalog=softdown;Data Source=ntserver" '连接到 ms sql server 7 cnMSSQL.Open sConnect 'sSql = "create table softinfo (softNum smallint,softname varchar(70),softdesc blob," & _ "softpath varchar(30),softleng varchar(10),softclass varchar(10),softsugest tinyint(1)," & _ "softdown smallint(4))" '创建新的MySQL数据表语句 sSql = "select * from softinfo order by softnum" rs.Open sSql, cnMSSQL, 1, 1 While Not rs.EOF sSql = "insert into softinfo values (" & Trim(rs(0).Value) & ",'" & Trim(rs(1).Value) & _ "','" & Trim(rs(2).Value) & "','" & Trim(rs(3).Value) & "','" & Trim(rs(4).Value) & _ "','" & Trim(rs(5).Value) & "'," & Trim(rs(6).Value) & "," & Trim(rs(7).Value) & ")" cm.ActiveConnection = cnMySQL cm.CommandType = adCmdText cm.CommandText = sSql cm.Execute rs.MoveNext Wend rs.Close Set rs = Nothing cnMySQL.Close Set cnMySQL = Nothing cnMSSQL.Close Set cnMSSQL = Nothing End Sub
VB使用ADODB控件连接。。有2种方法造作数据,一种是帮定控件,一种是使用代码,VB中设置连接 1、添加部件Mircrosoft ADO Data Control 6.0(OLEDB),把部件拖到窗体。 2、对ADO部件点右键选属性,选择使用连接字符串,按生成。 3、选择Mircosoft OLE DB Providar for SQL Server 按下一步 4、 1、输入服务器名称 2、使用指定的服务器信息 3、在服务器上选择数据库 这时就选择你在SQL Server建好的数据库就行了 5、测试连接可以看到连接是否成功! 以上是 操作控件来连接数据库, 以下是,使用代码来完成对数据库的操作/定义了Public conn As New ADODB.Connection Public rs As New ADODB.Recordset 连接数据库conn.ConnectionString= "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & App.Path & "\db1.mdb;Persist Security Info=False" ‘这里是数据源的位置。conn.Open rs.CursorLocation = adUseClient rs.Open "select * from files order by fileid", conn, 3, 2 绑定控件 Set txtid.DataSource = rs txtid.DataField = "fileid" 并通过两个按钮控制记录的上一条和下一条的读取 Private Sub cmdup_Click() rs.MovePrevious End Sub Private Sub cmddown_Click() rs.Movenext End Sub
在VB6中可用ADO及DATAGRID控件,用如下语句可更灵活: Private Sub Form_Load() Text1 = "" With Adodc1 Adodc1.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=E:\db1.mdb;Persist Security Info=False" Adodc1.RecordSource = "select name from b1 order by name" Adodc1.Refresh DataGrid1.Refresh End With End Sub Private Sub Text1_Change() Adodc1.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=E:\db1.mdb;Persist Security Info=False" Adodc1.RecordSource = "select name from b1 where name >='" & Text1 & "' order by name" Adodc1.Refresh'对查询更新 DataGrid1.Refresh End Sub 数据库用VB6编程语言,对新手而言,如何用好ADODC控件和DATAGRID控件相当重要。上面的原代码基于ADODC控件和DATAGRID控件的灵活应用。具体做法: 新建工程时选数据工程,此时VB6集成调试环境左边工具箱内已加载了有关数据库编程必须的控件。 然后在FORM1窗体中添加ADODC控件和DATAGRID控件,将DATAGRID1的属性DATASOURCE选ADODC1,打开ADODC1控件属性页使用连接字符串,选生成,在提供者选项中ACCESS数据库选MICROSOFT jet 4.0 OLE DB Provider,然后按要求连接数据库等。在ADODC1控件属性页使用连接字符串空白文本窗口中就有一长串字符串,注意该字符串可复制到程序代码用于编程。ADODC1控件属性页的数据源内有命令文本(SQL)编写窗口可编写SQL查询语言。该窗口的SQL语句可复制到程序代码用于编程。 SQL查询语言主要结构为: Select 查询字段 from 表名 Where 查询条件语句 [排序语句或分组语句] 查询字段必须分别用(西文)逗号分开或就用一个*号代替,上述查询中排序语句建议最好应用。 SQL查询语言如有错程序运行时告诉你出错,作相应改动即可。本人一般先按上述连接,SQL用"SELECT * FROM TabelName"作调试,无问题,用一个按钮孔件将前述需复制的连接字符串和SQL查询语言先复制于按钮控件的CLICK事件中备着,以便放着以后使用。然后删去ADODC1孔件,再添加ADODC1控件再添其他内容。 以上不知是否说清楚?SQL查询语言多写多用就会熟练,以上供提问者参考。
Public conn As New ADODB.ConnectionPublic rs As New ADODB.RecordsetPublic addFlag As BooleanPublic Function OpenCn() As BooleanDim mag As StringOn Error GoTo strerrmagSet conn = New ADODB.Connectionconn.ConnectionTimeout = 25conn.Provider = "sqloledb"conn.Properties("data source").Value = "127.0.0.1" '服务器的名字conn.Properties("initial catalog").Value = "chart" '库名conn.Properties("integrated security").Value = "SSPI" '登陆类型conn.OpenOpenCn = TrueaddFlag = TrueExit Functionstrerrmag:mag = "Data can't connect"Call MsgBox(mag, vbOKCancel, "Error:Data connect")addFlag = FalseEndEnd FunctionPublic Sub clocn()On Error Resume NextIf conn.State <> adStateClosed Then conn.CloseSet conn = NothingEnd SubPublic Function openrs(ByVal strsql As String) As Boolean '连接数据库记录集Dim mag As String Dim rpy As BooleanOn Error GoTo strerrmagSet rs = New ADODB.RecordsetIf addFlag = False Then rpy = TrueWith rs.ActiveConnection = conn.CursorLocation = adUseClient.CursorType = adOpenKeyset.LockType = adLockOptimistic.Open strsqlEnd WithaddFlag = Trueopenrs = TrueExit Functionstrerrmag:mag = "data not connect"Call MsgBox(mag, vbOKCancel, "error:connect")openrs = FalseEndEnd FunctionPublic Sub clors()On Error Resume NextIf rs.State <> adStateClosed Then rs.CloneSet rs = NothingEnd Sub