2、引用下载后的mysql.data.dll
3、程序开始加:using MySql.Data.MySqlClient
4、连接数据库:
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]//读取数据库账号
mysqlpsd = getconfig[2].Split(':')[1]//读取数据库密码
}
catch (Exception)
{
MessageBox.Show("配置文件丢失")
return
}
string query = "SET names gb2312SELECT 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=systemcharset=gb2312")
cn.Open()
MySqlCommand cm = new MySqlCommand(query, cn)
MySqlDataReader read = cm.ExecuteReader() //搜索满足 用户名,密码, *** 作员的记录。
//如果记录没有-->密码或用户名错误
if (read.Read())//如果记录多余1条-->数据错误,联系管理员
{ //只有一条记录则成功登入
int x = Int32.Parse(read[0].ToString())
if (x == 0)
{
MessageBox.Show("用户名或密码错误")
}
else if (x >1)
{
MessageBox.Show("用户冲突,请联系管理员")
}
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("数据库连接失败1")
break
case 1045:
MessageBox.Show("数据库密码或用户名错误")
break
default:
MessageBox.Show("数据库连接失败2")
break
}
}
}
}
方法一:
使用MYSQL推出的MySQL Connector/Net is an ADO.NET driver for MySQL
该组件为MYSQL为ADO.NET访问MYSQL数据库设计的.NET访问组件。
安装完成该组件后,引用命名空间MySql.Data.MySqlClient
使用命令行编译时:csc /r:MySql.Data.dll test.cs
下载地址: http://dev.mysql.com/downloads/connector/net/1.0.html
方法二:
通过ODBC访问MYSQL数据库
访问前要先下载两个组件:odbc.net和MYSQL的ODBC驱动(MySQL Connector/ODBC (MyODBC) driver)目前为3.51版
安装完成后,即可通过ODBC访问MYSQL数据库
方法三:
使用CoreLab推出的MYSQL访问组件,面向.NET
安装完成后,引用命名空间:CoreLab.MySql
使用命令编译时:csc /r:CoreLab.MySql.dll test.cs
以下为访问MYSQL数据库实例
编译指令:csc /r:CoreLab.MySql.dll /r:MySql.Data.dll test.cs
using Systemusing 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=rootHost=localhostDatabase=qingpassword=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=MySQLUID=rootPWD=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=testData Source=localhostUser Id=rootPassword=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()
}
}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)