C#連接MySQL操作詳細(xì)教程
C#如何連接MySQL進(jìn)行操作,供大家參考,具體內(nèi)容如下
1、引入MySql.Data.dll
例如小編是vs2017,創(chuàng)建工程之后,添加應(yīng)用,選擇工程,點(diǎn)擊下面的引用,右擊打開,選擇添加引用

2、如何找到自己的MySql.Data.dll,答案就是自己安裝MySQL的安裝路徑下,找到后選擇添加

3、就是直接貼代碼了,這里是查詢賬號(hào)密碼,以及插入賬號(hào)密碼(注意:更改個(gè)人的數(shù)據(jù)名字以及表密碼等)
using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;using MySql.Data.MySqlClient;//引用命名空間namespace MySQL數(shù)據(jù)庫(kù)操作{ class Program { static void Main(string[] args) { string connStr = 'Database=test007;Data Source=127.0.0.1;port=3306;User Id=root;Password=124638;'; // string connStr = 'Database=test007;datasource=127.0.0.1;port=3306;user=root;pwd=124638;'; MySqlConnection conn = new MySqlConnection(connStr); //打開鏈接 conn.Open(); #region 查詢 ////創(chuàng)建命令 //MySqlCommand cmd = new MySqlCommand('select * from user ', conn); ////執(zhí)行命令--讀取數(shù)據(jù) //MySqlDataReader reader= cmd.ExecuteReader(); ////讀取數(shù)據(jù) //while(reader.Read())//判斷是否有數(shù)據(jù)--//讀取一行記錄 //{ // string username= reader.GetString('username'); // string password = reader.GetString('password'); // Console.WriteLine(username + ':' + password); //} //reader.Close(); #endregion #region 插入數(shù)據(jù) string username = 'cwer'; string password = 'lcker'; //創(chuàng)建語句 MySqlCommand cmd = new MySqlCommand('insert into user set username=’' + username + '’' + ',password=’'+password+'’',conn); //執(zhí)行語句 cmd.ExecuteNonQuery(); conn.Close(); Console.ReadKey();//程序暫停 } }}
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持好吧啦網(wǎng)。
相關(guān)文章:
1. SQL SERVER偏移函數(shù)(LAG、LEAD、FIRST_VALUE、LAST _VALUE、NTH_VALUE)2. DB2 與 Microsoft SQL Server 2000 之間的 SQL 數(shù)據(jù)復(fù)制3. DB2 XML 全文搜索之為文本搜索做準(zhǔn)備4. 理解 DB2 中列組統(tǒng)計(jì)信息5. 細(xì)化解析:Oracle 10g ASM 的一點(diǎn)經(jīng)驗(yàn)6. Oracle 數(shù)據(jù)字典7. SQL Server使用CROSS APPLY與OUTER APPLY實(shí)現(xiàn)連接查詢8. MySQL InnoDB架構(gòu)的相關(guān)總結(jié)9. DB2建立nickname導(dǎo)致CRASH解決方法10. DB2數(shù)據(jù)庫(kù)安全性全面介紹(1)

網(wǎng)公網(wǎng)安備