1、mysql官網(wǎng)下載 .net連接器
專業(yè)成都網(wǎng)站建設公司,做排名好的好網(wǎng)站,排在同行前面,為您帶來客戶和效益!成都創(chuàng)新互聯(lián)為您提供成都網(wǎng)站建設,五站合一網(wǎng)站設計制作,服務好的網(wǎng)站設計公司,成都做網(wǎng)站、成都網(wǎng)站建設負責任的成都網(wǎng)站制作公司!
2、引用下載后的mysql.data.dll
3、程序開始加:using MySql.Data.MySqlClient;
4、連接數(shù)據(jù)庫:
private void button1_Click(object sender, EventArgs e)//登入按鈕
{
string power = comboBox1.Text.Trim();
string user = textBox1.Text.Trim();
string psd = textBox2.Text.Trim();
string ipaddress = "";
string mysqluser = "";
string mysqlpsd = "";
if (user == "")
{
MessageBox.Show("請輸入用戶名");
}
else if (psd == "")
{
MessageBox.Show("請輸入密碼");
}
else
{
try
{
try
{
string[] getconfig = File.ReadAllLines("E:/project/configure.txt", Encoding.GetEncoding("gb2312"));
ipaddress = getconfig[0].Split(':')[1];//讀取ip地址
mysqluser = getconfig[1].Split(':')[1];//讀取數(shù)據(jù)庫賬號
mysqlpsd = getconfig[2].Split(':')[1]; //讀取數(shù)據(jù)庫密碼
}
catch (Exception)
{
MessageBox.Show("配置文件丟失");
return;
}
string query = "SET names gb2312;SELECT COUNT(id) FROM fx_user WHERE name='" + user + "' AND password=MD5('" + psd + "') AND userid='" + power + "'";
MySqlConnection cn = new MySqlConnection("server=" + ipaddress + ";user id=" + mysqluser + ";Password=" + mysqlpsd + ";database=system;charset=gb2312");
cn.Open();
MySqlCommand cm = new MySqlCommand(query, cn);
MySqlDataReader read = cm.ExecuteReader(); //搜索滿足 用戶名,密碼,操作員的記錄。
//如果記錄沒有--密碼或用戶名錯誤
if (read.Read()) //如果記錄多余1條--數(shù)據(jù)錯誤,聯(lián)系管理員
{ //只有一條記錄則成功登入
int x = Int32.Parse(read[0].ToString());
if (x == 0)
{
MessageBox.Show("用戶名或密碼錯誤");
}
else if (x 1)
{
MessageBox.Show("用戶沖突,請聯(lián)系管理員");
}
else if (x == 1)
{
// MessageBox.Show("登入成功");
main mf = new main(power, ipaddress, mysqluser, mysqlpsd); //將操作員 和 IP地址傳入 主窗體
mf.Show();
this.Hide();
cn.Close();
}
}
}
catch (MySql.Data.MySqlClient.MySqlException ex)
{
switch (ex.Number)
{
case 0:
MessageBox.Show("數(shù)據(jù)庫連接失敗1");
break;
case 1045:
MessageBox.Show("數(shù)據(jù)庫密碼或用戶名錯誤");
break;
default:
MessageBox.Show("數(shù)據(jù)庫連接失敗2");
break;
}
}
}
}
1、要連接MySql數(shù)據(jù)庫必須首先下載MySql官方的連接.net的文件,文件下載地址為 ,下載ZIP免安裝版。
2、解壓縮剛才下載的mysql-connector-net-6.6.6-noinstall.zip文件,里面有幾個版本選擇,在這里我選V4,選中這幾個文件(mysql.data.dll/mysql.data.entity.dll/mysql.visulstudio.dll),然后添加到C#項目的引用中,然后就可以編寫程序進行數(shù)據(jù)庫的操作了
數(shù)據(jù)庫操作代碼 InsertMySql.cs
代碼如下:
using System;
using System.Collections;
using System.Configuration;
using MySql.Data;
using MySql.Data.MySqlClient;
using System.Data;
namespace Sql
//數(shù)據(jù)庫簡單測試
{
class InsertSql1
{
public static void Main(string[] args)
{
string constr="server=localhost;User Id=root;password=219229;Database=reg";
MySqlConnection mycon=new MySqlConnection(constr);
mycon.Open();
MySqlCommand mycmd = new MySqlCommand("insert into buyer(name,password,email) values('源碼婆','66881728','66881728@qq.com')",mycon);
if (mycmd.ExecuteNonQuery()0)
{
Console.WriteLine("數(shù)據(jù)插入成功!");
}
Console.ReadLine();
mycon.Close();
}
}
}
方法一:
使用MYSQL推出的MySQL?Connector/Net?is?an?ADO.NET?driver?for?MySQL
該組件為MYSQL為ADO.NET訪問MYSQL數(shù)據(jù)庫設計的.NET訪問組件。
安裝完成該組件后,引用命名空間MySql.Data.MySqlClient;
使用命令行編譯時:csc?/r:MySql.Data.dll?test.cs?
下載地址:???
方法二:
通過ODBC訪問MYSQL數(shù)據(jù)庫
訪問前要先下載兩個組件:odbc.net和MYSQL的ODBC驅(qū)動(MySQL?Connector/ODBC?(MyODBC)?driver)目前為3.51版
安裝完成后,即可通過ODBC訪問MYSQL數(shù)據(jù)庫
方法三:
使用CoreLab推出的MYSQL訪問組件,面向.NET
安裝完成后,引用命名空間:CoreLab.MySql;
使用命令編譯時:csc?/r:CoreLab.MySql.dll?test.cs
以下為訪問MYSQL數(shù)據(jù)庫實例
編譯指令:csc?/r:CoreLab.MySql.dll?/r:MySql.Data.dll?test.cs
using?System;
using?System.Net;
using?System.Text;
using?CoreLab.MySql;
using?System.Data.Odbc;
using?MySql.Data.MySqlClient;
class?ConnectMySql
{
public?void?Connect_CoreLab()
{
string?constr?=?"User?Id=root;Host=localhost;Database=qing;password=qing";
MySqlConnection?mycn?=?new?MySqlConnection(constr);
mycn.Open();
MySqlCommand?mycm?=?new?MySqlCommand("select?*?from?shop",mycn);
MySqlDataReader?msdr?=?mycm.ExecuteReader();
while(msdr.Read())
{
if?(msdr.HasRows)
{
Console.WriteLine(msdr.GetString(0));
}
}
msdr.Close();
mycn.Close();
}
public?void?Connect_Odbc()
{
//string?MyConString?="DSN=MySQL;UID=root;PWD=qing";?
string?MyConString?=?"DRIVER={MySQL?ODBC?3.51?Driver};"?+?
"SERVER=localhost;"?+
"DATABASE=test;"?+
"UID=root;"?+
"PASSWORD=qing;"?+
"OPTION=3";
OdbcConnection?MyConn?=?new?OdbcConnection(MyConString);
MyConn.Open();
OdbcCommand?mycm?=?new?OdbcCommand("select?*?from?hello",MyConn);
OdbcDataReader?msdr?=?mycm.ExecuteReader();
while(msdr.Read())
{
if?(msdr.HasRows)
{
Console.WriteLine(msdr.GetString(0));
}
}
msdr.Close();
MyConn.Close();
}
public?void?Connect_Net()
{
string?myConnectionString?=?"Database=test;Data?Source=localhost;User?Id=root;Password=qing";
MySqlConnection?mycn?=?new?MySqlConnection(myConnectionString);
mycn.Open();
MySqlCommand?mycm?=?new?MySqlCommand("select?*?from?hello",mycn);
MySqlDataReader?msdr?=?mycm.ExecuteReader();
while(msdr.Read())
{
if?(msdr.HasRows)
{
Console.WriteLine(msdr.GetString(0));
}
}
msdr.Close();
mycn.Close();
}
public?static?void?Main()
{
ConnectMySql?ms?=?new?ConnectMySql();
ms.Connect_CoreLab();
ms.Connect_Odbc();
Connect_Net();
}
}
1、mysql官網(wǎng)下載 .net連接器
2、引用下載后的mysql.data.dll
3、程序開始加:using MySql.Data.MySqlClient;
4、連接數(shù)據(jù)庫:
private void button1_Click(object sender, EventArgs e)//登入按鈕
{
string power = comboBox1.Text.Trim();
string user = textBox1.Text.Trim();
string psd = textBox2.Text.Trim();
string ipaddress = "";
string mysqluser = "";
string mysqlpsd = "";
if (user == "")
{
MessageBox.Show("請輸入用戶名");
}
else if (psd == "")
{
MessageBox.Show("請輸入密碼");
}
else
{
try
{
try
{
string[] getconfig = File.ReadAllLines("E:/project/configure.txt", Encoding.GetEncoding("gb2312"));
ipaddress = getconfig[0].Split(':')[1];//讀取ip地址
mysqluser = getconfig[1].Split(':')[1];//讀取數(shù)據(jù)庫賬號
mysqlpsd = getconfig[2].Split(':')[1]; //讀取數(shù)據(jù)庫密碼
}
catch (Exception)
{
MessageBox.Show("配置文件丟失");
return;
}
string query = "SET names gb2312;SELECT COUNT(id) FROM fx_user WHERE name='" + user + "' AND password=MD5('" + psd + "') AND userid='" + power + "'";
MySqlConnection cn = new MySqlConnection("server=" + ipaddress + ";user id=" + mysqluser + ";Password=" + mysqlpsd + ";database=system;charset=gb2312");
cn.Open();
MySqlCommand cm = new MySqlCommand(query, cn);
MySqlDataReader read = cm.ExecuteReader(); //搜索滿足 用戶名,密碼,操作員的記錄。
//如果記錄沒有--密碼或用戶名錯誤
if (read.Read()) //如果記錄多余1條--數(shù)據(jù)錯誤,聯(lián)系管理員
{ //只有一條記錄則成功登入
int x = Int32.Parse(read[0].ToString());
if (x == 0)
{
MessageBox.Show("用戶名或密碼錯誤");
}
else if (x 1)
{
MessageBox.Show("用戶沖突,請聯(lián)系管理員");
}
else if (x == 1)
{
// MessageBox.Show("登入成功");
main mf = new main(power, ipaddress, mysqluser, mysqlpsd); //將操作員 和 IP地址傳入 主窗體
mf.Show();
this.Hide();
cn.Close();
}
}
}
catch (MySql.Data.MySqlClient.MySqlException ex)
{
switch (ex.Number)
{
case 0:
MessageBox.Show("數(shù)據(jù)庫連接失敗1");
break;
case 1045:
MessageBox.Show("數(shù)據(jù)庫密碼或用戶名錯誤");
break;
default:
MessageBox.Show("數(shù)據(jù)庫連接失敗2");
break;
}
}
}
}