zoukankan      html  css  js  c++  java
  • VB连接MYSQL数据的方法

    原文链接:http://hanbaohong.iteye.com/blog/704800

    第一步:上网http://dev.mysql.com/downloads/connector/odbc/下载mysql-connector-odbc-5.1.6-win32.msi

    第二步:安装mysql-connector-odbc-5.1.6-win32.msi

    第三步:运行VB并新建一标准EXE工程, 通过菜单 工程->引用 打开”引用”对话框, 找到 Microsoft ActiveX Data Objects x.x Library , 其中 x.x 是版本号, 可能会有很多个, 这里我选择的是 2.5

    第四步:打开代码窗口, 在 Form_Load 过程中输入下面的代码(具体说明在代码的注释中):

     1 Dim cn As New ADODB.Connection
     2 Dim rs As New ADODB.Recordset
     3 
     4 
     5 ' 定义数据库连接字符串变量
     6 Dim strCn As String
     7 
     8 
     9 ' 定义数据库连接参数变量
    10 Dim db_host As String
    11 Dim db_user As String
    12 Dim db_pass As String
    13 Dim db_data As String
    14 
    15 
    16 ' 定义 SQL 语句变量
    17 Dim sql As String
    18 
    19 
    20 
    21 
    22 ' 初始化数据库连接变量
    23 db_host = "localhost"
    24 db_user = "root" ‘用户名
    25 db_pass = "root" ’用户密码
    26 db_data = "radio" '数据库名
    27 
    28 
    29 ' MySQL ODBC 连接参数
    30 '+------------+---------------------+----------------------------------+
    31 '| 参数名     | 默认值               | 说明                             |
    32 '+------------+------------------------------------------------------–+
    33 '| user       | ODBC (on Windows)   | MySQL 用户名                     |
    34 '| server     | localhost           | MySQL 服务器地址                 |
    35 '| database   |                     | 默认连接数据库                   |
    36 '| option     | 0                   | 参数用以指定连接的工作方式       |
    37 '| port       | 3306                 | 连接端口                         |
    38 '| stmt       |                     | 一段声明, 可以在连接数据库后运行 |
    39 '| password   |                     | MySQL 用户密码                   |
    40 '| socket     |                     | (略)                             |
    41 '+------------+---------------------+----------------------------------+
    42 
    43 
    44 ' 详细查看官方说明
    45 ' http://dev.mysql.com/doc/refman/5.0/en/myodbc-configuration-connection-parameters.html
    46 
    47 
    48 strCn = "DRIVER={MySQL ODBC 3.51 Driver};" & _
    49          "SERVER=" & db_host & ";" & _
    50          "DATABASE=" & db_data & ";" & _
    51          "UID=" & db_user & ";PWD=" & db_pass & ";" & _
    52          "OPTION=3;stmt=SET NAMES GB2312"
    53 
    54 
    55 ' stmt=SET NAMES GB2312
    56 ' 这句是设置数据库编码方式
    57 ' 中文操作系统需要设置成 GB2312
    58 ' 这样中文才不会有问题
    59 ' 版本要求 mysql 4.1+
    60 
    61 
    62 ' 连接数据库
    63 cn.Open strCn
    64 ' 设置该属性, 使 recordcount 和 absolutepage 属性可用
    65 cn.CursorLocation = adUseClient
    66 
    67 
    68 ' 访问表 table1
    69 sql = "select nicheng,xinqing from nichengandxinqing where uid='1'"
    70 rs.Open sql, cn
    71 
    72 Dim nicheng As String
    73 Dim xinqing As String
    74 
    75 
    76 '加载用户昵称
    77 nicheng = rs.Fields(0)
    78 Label1.Caption = nicheng
    79 
    80 
    81 '加载用户心情短语
    82 xinqing = rs.Fields(1)
    83 Label2.Caption = xinqing
    84 '鼠标放在心情短语提示短语详细信息
    85 Label2.ToolTipText = xinqing
    86 
    87 '加载网络图片
    88 Image1.Picture = LoadPicture("http://hi.csdn.net/attachment/201201/5/8747069_1325751084ZiD4.jpg")
  • 相关阅读:
    10.1~10.15学习情况
    ACM-ICPC 2018 沈阳赛区网络预赛
    打卡4
    打卡3
    tab 简单的tab
    css 圆形动画
    pdf和图片之间的转换
    对list进行分组
    C# 打开所在文件夹
    读取xml文件
  • 原文地址:https://www.cnblogs.com/hubery/p/3608797.html
Copyright © 2011-2022 走看看