日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 运维知识 > 数据库 >内容正文

数据库

C#访问MySQL数据库的方法

發布時間:2025/1/21 数据库 28 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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数据库的方法的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。