C#访问MySQL数据库的方法
(1)首先需要下載C#訪問MySQL數據庫的ADO.NET驅動程序
下載地址為:
http://dev.mysql.com/downloads/connector/net/6.0.html
我下載的版本為:?mysql-connector-net-6.3.8.msi
下載地址如下url:
http://dev.mysql.com/downloads/mirror.php?id=405442
或者去我的CSDN資源中下載:
http://download.csdn.net/detail/guomutian911/8304975
(2)安裝mysql-connector-net
然后直接在Windows操作系統安裝?mysql-connector-net-6.3.8.msi
默認是安裝在C盤:
C:\Program Files\MySQL\MySQL Connector Net6.3.8\Assemblies
v2.0
v4.0
安裝完后我選擇的是v2.0版本的
然后在應用工程中引用組件MySQL.Data.dll,具體方法如下圖所示:
圖1為msi安裝后的目錄,需要將紅色的dll加入工程引用中
按圖示步驟加入引用,當如數字4處,出MySqlData則表明引用dll成功,即已經加載了mysql驅動
(3)程序中數據庫操作代碼如下:
//using System; //using System.Collections.Generic; //using System.ComponentModel; //using System.Data; //using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; using System.Data.SqlClient; using System.Collections.Generic; using System.Text; using MySql.Data.MySqlClient; using System.Data; using System.Data.Common; using System; using System.Collections.Generic; using System.Text;using MySql.Data.MySqlClient; using System.Data; using System.Data.Common; namespace keshe {public partial class Form1 : Form{public Form1(){InitializeComponent();}private void button1_Click(object sender, EventArgs e){}private void button2_Click(object sender, EventArgs e){}private void label1_Click(object sender, EventArgs e){}private void Form1_Load(object sender, EventArgs e){//switch (power)//{// case "0":ToolStripStatuesIt//}}private void button1_Click_1(object sender, EventArgs e){int flag = 1;if (txtName.Text == "" || txtPwd.Text == ""){MessageBox.Show("信息不完整!", "提示");return;}//if (txtName.Text != "1" && txtPwd.Text != "1")//{// MessageBox.Show("用戶名或密碼不正確!", "提示");//}//else//{// new Form2().Show();// this.Hide();//}try{MySqlClientFactory factory = MySqlClientFactory.Instance;DbConnection conn = factory.CreateConnection();conn.ConnectionString = string.Format("server={0};user id={1}; password={2}; database={3}; port={4}; pooling=false","localhost", "root", "root", "mydata", 3306);conn.Open();DbDataAdapter da = factory.CreateDataAdapter();da.SelectCommand = conn.CreateCommand();da.SelectCommand.CommandText = "select * from tb_user";//da.DeleteCommand = conn.CreateCommand();//da.DeleteCommand.CommandText = "delete from t12345 where id = @id";//DbParameter param = factory.CreateParameter();//param.ParameterName = "@id";//param.DbType = DbType.Int32;//param.SourceColumn = "id";//param.SourceVersion = DataRowVersion.Current;//da.DeleteCommand.Parameters.Add(param);//da.DeleteCommand.UpdatedRowSource = UpdateRowSource.None;DataTable dt = new DataTable("tb_user");da.Fill(dt);//MessageBox.Show("ok", "okk");int index = 0;foreach (DataRow o in dt.Rows){if (o["UserName"].Equals(txtName.Text) && o["UserPwd"].Equals(txtPwd.Text)){MessageBox.Show("用戶為:" + txtName.Text, "登陸用戶判斷");// Console.WriteLine(String.Format("index={0}, to delete id = 4, col2 = {1}", index, o["col2"]));//break;new Form2().Show();this.Hide();flag = 0;}//else//{// MessageBox.Show("非法登陸");//}index++;}if (flag == 1){MessageBox.Show("非法登陸");}//dt.Rows[index].Delete();//da.Update(dt);//dt.AcceptChanges();//da.Dispose();//conn.Close();}catch (Exception ex){//Console.WriteLine(ex.Source + " "// + ex.Message + " "// + ex.StackTrace);} }private void button2_Click_1(object sender, EventArgs e){txtName.Clear();txtPwd.Clear();}} }(4)簡單運行效果:
首先將用戶輸入與數據庫查詢比對,如果有則顯示用戶名,并提示登錄成功
111用戶名在數據庫不存在
********************************************************************************************
?
總結
以上是生活随笔為你收集整理的C#访问MySQL数据库的方法的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Fleury (弗罗莱) 算法通俗解释
- 下一篇: 【已解决】Navicat 远程连接 Li