连接数据库mysql采用什么技术
1个回答
2015-11-28
展开全部
c#连接MySql数据库的方法
一、用MySQLDriverCS连接MySQL数据库。
先下载和安装MySQLDriverCS,在安装文件夹下面找到MySQLDriver.dll,然后将MySQLDriver.dll添加引用到项目中。
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using MySQLDriverCS;
namespace jxkh
{
public partial class frmLogin : Form
{
public frmLogin()
{
InitializeComponent();
}
private void btnLogin_Click(object sender, EventArgs e)
{
MySQLConnectionString tConnStr = new MySQLConnectionString("10.14.55.46", "performance", "administrator", "1234567@byd", 3306);
MySQLConnection tConn = new MySQLConnection(tConnStr.AsString);
try
{
tConn.Open(); //打开连接
MySQLCommand cmd4 = new MySQLCommand("set names gb2312", tConn);
cmd4.ExecuteNonQuery();
string tCmd = "select ID,Name,PassWord from managers"; //命令语句
MySQLCommand cmd = new MySQLCommand(tCmd,tConn); //在定义的tConn对象上执行查询命令
MySQLDataReader tReader = cmd.ExecuteReaderEx();
if(tReader.Read()) // 一次读一条记录
{
if(tReader["Name"].ToString()==textBox1.Text&&tReader["PassWord"].ToString()==textBox2.Text)
{
frmJxkh myJxkh = new frmJxkh();
myJxkh.Show();
}
}
tConn.Close();//重要!要及时关闭
tReader.Close();
}
catch
{
tConn.Close();
}
}
}
}
二、通过ODBC访问mysql数据库:
1. 安装Microsoft ODBC.net;
2. 安装MDAC 2.7或者更高版本;
3. 安装MySQL的ODBC驱动程序;
4. 管理工具 -> 数据源ODBC –>配置DSN…;
5. 解决方案管理中添加引用 Microsoft.Data.Odbc.dll(1.0.3300);
6. 代码中增加引用 using Microsoft.Data.Odbc;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Linq; //vs2005好像没有这个命名空间,在c#2008下测试自动生成的
using System.Text;
using System.Windows.Forms;
using Microsoft.Data.Odbc;
namespace mysql
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
string MyConString = "DRIVER={MySQL ODBC 3.51 Driver};" +
"SERVER=localhost;" +
"DATABASE=inv;" +
"UID=root;" +
"PASSWORD=831025;" +
"OPTION=3";
OdbcConnection MyConnection = new OdbcConnection(MyConString);
MyConnection.Open();
Console.WriteLine(""n success, connected successfully !"n");
string query = "insert into test values( 'hello', 'lucas', 'liu')";
OdbcCommand cmd = new OdbcCommand(query, MyConnection);
//处理异常:插入重复记录有异常
try{
cmd.ExecuteNonQuery();
}
catch(Exception ex){
Console.WriteLine("record duplicate.");
}finally{
cmd.Dispose();
}
//***********************用read方法读数据到textbox**********************
string tmp1 = null;
string tmp2 = null;
string tmp3 = null;
query = "select * from test ";
OdbcCommand cmd2 = new OdbcCommand(query, MyConnection);
OdbcDataReader reader = cmd2.ExecuteReader();
while (reader.Read())
{
tmp1 = reader[0].ToString();
tmp2 = reader[1].ToString();
tmp3 = reader[2].ToString();
}
this.textBox1.Text = tmp1 + " " + tmp2 + " " + tmp3;
*/
//************************用datagridview控件显示数据表**************************
string MyConString = "DRIVER={MySQL ODBC 3.51 Driver};" +
"SERVER=localhost;" +
"DATABASE=inv;" +
"UID=root;" +
"PASSWORD=831025;" +
"OPTION=3";
OdbcConnection MyConnection = new OdbcConnection(MyConString);
OdbcDataAdapter oda = new OdbcDataAdapter("select * from customer ", MyConnection);
DataSet ds = new DataSet();
oda.Fill(ds, "employee");
this.dataGridView1.DataSource = ds.Tables["employee"];
*/
MyConnection.Close();
}
}
}
一、用MySQLDriverCS连接MySQL数据库。
先下载和安装MySQLDriverCS,在安装文件夹下面找到MySQLDriver.dll,然后将MySQLDriver.dll添加引用到项目中。
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using MySQLDriverCS;
namespace jxkh
{
public partial class frmLogin : Form
{
public frmLogin()
{
InitializeComponent();
}
private void btnLogin_Click(object sender, EventArgs e)
{
MySQLConnectionString tConnStr = new MySQLConnectionString("10.14.55.46", "performance", "administrator", "1234567@byd", 3306);
MySQLConnection tConn = new MySQLConnection(tConnStr.AsString);
try
{
tConn.Open(); //打开连接
MySQLCommand cmd4 = new MySQLCommand("set names gb2312", tConn);
cmd4.ExecuteNonQuery();
string tCmd = "select ID,Name,PassWord from managers"; //命令语句
MySQLCommand cmd = new MySQLCommand(tCmd,tConn); //在定义的tConn对象上执行查询命令
MySQLDataReader tReader = cmd.ExecuteReaderEx();
if(tReader.Read()) // 一次读一条记录
{
if(tReader["Name"].ToString()==textBox1.Text&&tReader["PassWord"].ToString()==textBox2.Text)
{
frmJxkh myJxkh = new frmJxkh();
myJxkh.Show();
}
}
tConn.Close();//重要!要及时关闭
tReader.Close();
}
catch
{
tConn.Close();
}
}
}
}
二、通过ODBC访问mysql数据库:
1. 安装Microsoft ODBC.net;
2. 安装MDAC 2.7或者更高版本;
3. 安装MySQL的ODBC驱动程序;
4. 管理工具 -> 数据源ODBC –>配置DSN…;
5. 解决方案管理中添加引用 Microsoft.Data.Odbc.dll(1.0.3300);
6. 代码中增加引用 using Microsoft.Data.Odbc;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Linq; //vs2005好像没有这个命名空间,在c#2008下测试自动生成的
using System.Text;
using System.Windows.Forms;
using Microsoft.Data.Odbc;
namespace mysql
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
string MyConString = "DRIVER={MySQL ODBC 3.51 Driver};" +
"SERVER=localhost;" +
"DATABASE=inv;" +
"UID=root;" +
"PASSWORD=831025;" +
"OPTION=3";
OdbcConnection MyConnection = new OdbcConnection(MyConString);
MyConnection.Open();
Console.WriteLine(""n success, connected successfully !"n");
string query = "insert into test values( 'hello', 'lucas', 'liu')";
OdbcCommand cmd = new OdbcCommand(query, MyConnection);
//处理异常:插入重复记录有异常
try{
cmd.ExecuteNonQuery();
}
catch(Exception ex){
Console.WriteLine("record duplicate.");
}finally{
cmd.Dispose();
}
//***********************用read方法读数据到textbox**********************
string tmp1 = null;
string tmp2 = null;
string tmp3 = null;
query = "select * from test ";
OdbcCommand cmd2 = new OdbcCommand(query, MyConnection);
OdbcDataReader reader = cmd2.ExecuteReader();
while (reader.Read())
{
tmp1 = reader[0].ToString();
tmp2 = reader[1].ToString();
tmp3 = reader[2].ToString();
}
this.textBox1.Text = tmp1 + " " + tmp2 + " " + tmp3;
*/
//************************用datagridview控件显示数据表**************************
string MyConString = "DRIVER={MySQL ODBC 3.51 Driver};" +
"SERVER=localhost;" +
"DATABASE=inv;" +
"UID=root;" +
"PASSWORD=831025;" +
"OPTION=3";
OdbcConnection MyConnection = new OdbcConnection(MyConString);
OdbcDataAdapter oda = new OdbcDataAdapter("select * from customer ", MyConnection);
DataSet ds = new DataSet();
oda.Fill(ds, "employee");
this.dataGridView1.DataSource = ds.Tables["employee"];
*/
MyConnection.Close();
}
}
}
今至电子科技有限公司
2024-08-23 广告
2024-08-23 广告
数据库备份是确保数据安全与业务连续性的关键环节。我们上海今至电子科技有限公司高度重视数据保护,定期执行全面的数据库备份策略。这包括使用先进工具和技术,对关键业务数据进行自动化备份,并存储在安全可靠的外部存储介质或云端。通过定期验证备份的完整...
点击进入详情页
本回答由今至电子科技有限公司提供
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询