博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
两种方法连接MySql数据库
阅读量:7088 次
发布时间:2019-06-28

本文共 4654 字,大约阅读时间需要 15 分钟。

1、用MySQLDriverCS连接MySQL数据库先下载和安装MySQLDriverCS,在安装文件夹下面找到MySQLDriver.dll,然后将MySQLDriver.dll添加引用到项目中。    using System;      using System.Collections.Generic;      using System.ComponentModel;      using System.Data;      using System.Data.Odbc;      using System.Drawing;      using System.Linq;      using System.Text;      using System.Windows.Forms;      using MySQLDriverCS;      namespace mysql      {          public partial class Form1 : Form          {              public Form1()              {                  InitializeComponent();              }              private void Form1_Load(object sender, EventArgs e)              {                             MySQLConnection conn = null;                  conn = new MySQLConnection(new MySQLConnectionString("localhost", "inv", "root", "831025").AsString);                  conn.Open();                                MySQLCommand commn = new MySQLCommand("set names gb2312", conn);                  commn.ExecuteNonQuery();                  string sql = "select * from exchange ";                  MySQLDataAdapter mda = new MySQLDataAdapter(sql, conn);                  DataSet ds = new DataSet();                  mda.Fill(ds, "table1");                  this.dataGrid1.DataSource = ds.Tables["table1"];                  conn.Close();              }          }      }  复制代码2、通过ODBC访问mysql数据库:1.安装Microsoft ODBC.net:我安装的是mysql-connector-odbc-3.51.22-win32.msi2.安装MDAC 2.7或者更高版本:我安装的是mdac_typ.exe 2.7简体中文版3.安装MySQL的ODBC驱动程序:我安装的是 odbc_net.msi4.管理工具 -> 数据源ODBC –>配置DSN…5.解决方案管理中添加引用 Microsoft.Data.Odbc.dll(1.0.3300)6.代码中增加引用 using Microsoft.Data.Odbc;    using System;      using System.Collections.Generic;      using System.ComponentModel;      using System.Drawing;      using System.Linq;   //vs2005好像没有这个命名空间,在c#2008下测试自动生成的      using System.Text;      using System.Windows.Forms;      using Microsoft.Data.Odbc;      namespace mysql      {          public partial class Form1 : Form          {              public Form1()              {                  InitializeComponent();              }                    private void Form1_Load(object sender, EventArgs e)              {                  string MyConString = "DRIVER={MySQL ODBC 3.51 Driver};" +                                       "SERVER=localhost;" +                                       "DATABASE=inv;" +                                       "UID=root;" +                                       "PASSWORD=831025;" +                                       "OPTION=3";                  OdbcConnection MyConnection = new OdbcConnection(MyConString);                  MyConnection.Open();                  Console.WriteLine(""n success, connected successfully !"n");                  string query = "insert into test values( 'hello', 'lucas', 'liu')";                  OdbcCommand cmd = new OdbcCommand(query, MyConnection);                  //处理异常:插入重复记录有异常      try{        cmd.ExecuteNonQuery();      }      catch(Exception ex){                       Console.WriteLine("record duplicate.");      }finally{                       cmd.Dispose();      }      //***********************用read方法读数据到textbox**********************                  string tmp1 = null;                  string tmp2 = null;                  string tmp3 = null;                  query = "select * from test ";                  OdbcCommand cmd2 = new OdbcCommand(query, MyConnection);                  OdbcDataReader reader = cmd2.ExecuteReader();                  while (reader.Read())                  {                      tmp1 = reader[0].ToString();                      tmp2 = reader[1].ToString();                      tmp3 = reader[2].ToString();                  }                  this.textBox1.Text = tmp1 + " " + tmp2 + " " + tmp3;                  */      //************************用datagridview控件显示数据表**************************      string MyConString = "DRIVER={MySQL ODBC 3.51 Driver};" +                                       "SERVER=localhost;" +                                       "DATABASE=inv;" +                                       "UID=root;" +                                       "PASSWORD=831025;" +                                       "OPTION=3";                OdbcConnection MyConnection = new OdbcConnection(MyConString);      OdbcDataAdapter oda = new OdbcDataAdapter("select * from customer ", MyConnection);      DataSet ds = new DataSet();                oda.Fill(ds, "employee");                this.dataGridView1.DataSource = ds.Tables["employee"];      */                 MyConnection.Close();              }          }  复制代码* 版权声明:转载时请以超链接形式标明文章原始出处和作者信息* 本文来自:Unity3D 教程手册* 本文链接:http://www.unity蛮牛.com/6880.html

 

你可能感兴趣的文章
android编译错误,aapx.exe执行异常
查看>>
linux nginx 开机重启
查看>>
IPTABLES中SNAT和MASQUERADE的区别
查看>>
nali: 给dig,traceroute等命令的输出的ip附加上地理信息
查看>>
十个酷毙的Linux单行命令!
查看>>
vmware workstation 连接管理ESXI中的虚拟服务器
查看>>
Nginx+Keepalived搭建高可用负载平衡WEB 集群
查看>>
Objective -C-3
查看>>
EJB MEMCACHED --漫天飞舞的对象与对对象序列化分布式系统的分析
查看>>
两种运行时 -- 写于2009-05-19
查看>>
我的友情链接
查看>>
关于ANDROID事件-Android框架工程师的回复
查看>>
Jenkins API Token
查看>>
ubuntu安装经典的Gnome桌面
查看>>
awk使用详解(三)变量、数字表达式、赋值运算符、BGGIN、END、Built-in 变量
查看>>
linux下如何查看某软件是否已安装
查看>>
Nginx 反向代理、负载均衡、页面缓存、URL重写及读写分离详解
查看>>
大流量在宽带通信产品生产测试中的作用
查看>>
Javascript监听粘贴事件
查看>>
SQL008存储过程总结
查看>>