c#连接mysql的字符串 应该怎么写?
展开全部
1、先下载和安装MySQLDriverCS
2、在安装文件夹下面找到MySQLDriver.dll,然后将MySQLDriver.dll添加引用到项目中
3、参考代码:(个别引用非必须)
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.Odbc;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using MySQLDriverCS;
namespace mysql
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
MySQLConnection conn = null;
conn = new MySQLConnection(new MySQLConnectionString("localhost", "inv", "root", "831025").AsString);
conn.Open();
MySQLCommand commn = new MySQLCommand("set names gb2312", conn);
commn.ExecuteNonQuery();
string sql = "select * from exchange ";
MySQLDataAdapter mda = new MySQLDataAdapter(sql, conn);
DataSet ds = new DataSet();
mda.Fill(ds, "table1");
this.dataGrid1.DataSource = ds.Tables["table1"];
conn.Close();
}
}
}
2、在安装文件夹下面找到MySQLDriver.dll,然后将MySQLDriver.dll添加引用到项目中
3、参考代码:(个别引用非必须)
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.Odbc;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using MySQLDriverCS;
namespace mysql
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
MySQLConnection conn = null;
conn = new MySQLConnection(new MySQLConnectionString("localhost", "inv", "root", "831025").AsString);
conn.Open();
MySQLCommand commn = new MySQLCommand("set names gb2312", conn);
commn.ExecuteNonQuery();
string sql = "select * from exchange ";
MySQLDataAdapter mda = new MySQLDataAdapter(sql, conn);
DataSet ds = new DataSet();
mda.Fill(ds, "table1");
this.dataGrid1.DataSource = ds.Tables["table1"];
conn.Close();
}
}
}
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
public static int ExecuteSql(string SQLString)
{
using (MySqlConnection connection = new MySqlConnection(connectionString))
{
using (MySqlCommand cmd = new MySqlCommand(SQLString, connection))
{
try
{
connection.Open();
int rows = cmd.ExecuteNonQuery();
return rows;
}
catch (MySql.Data.MySqlClient.MySqlException e)
{
connection.Close();
throw e;
}
}
}
}
{
using (MySqlConnection connection = new MySqlConnection(connectionString))
{
using (MySqlCommand cmd = new MySqlCommand(SQLString, connection))
{
try
{
connection.Open();
int rows = cmd.ExecuteNonQuery();
return rows;
}
catch (MySql.Data.MySqlClient.MySqlException e)
{
connection.Close();
throw e;
}
}
}
}
更多追问追答
追问
连接数据库的字符串呢?
追答
server=127.0.0.1;database=codematic2;uid=sa;pwd=
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
这里将介绍C#数据库查询和操作方面的有关知识,包括数据库连接、数据库查询的相关代码和示例。希望本文对大家有所帮助。
一:C#数据库查询之数据库连接代码:
SqlConnectionobjSqlConnection=newSqlConnection("server=127.0.0.1;uid=sa;pwd=;database=test"); objSqlConnection.Open(); 二:数据库的添加记录代码:
inti=0; strings1="",s2=""; i=Convert.ToInt16(textBox1.Text); s1=textBox2.Text; s2=textBox3.Text; SqlConnectionobjSqlConnection=newSqlConnection("server=127.0.0.1;uid=sa;pwd=;database=test"); objSqlConnection.Open(); MessageBox.Show("数据库连接成功","好"); try { SqlCommandsqlcom=newSqlCommand("insertintoinfo(id,name,sex)values("+i+",'"+s1+"','"+s2+"')",objSqlConnection); sqlcom.ExecuteNonQuery(); MessageBox.Show("添加成功!","啊"); } catch(Exceptiona) { MessageBox.Show(a.ToString()); } MessageBox.Show("添加成功!","啊"); } 三:数据库的修改代码:
inti=0; strings1="",s2=""; s1=textBox2.Text; s2=textBox3.Text; if(textBox1.Text.Length==0) i=0; else i=Convert.ToInt32(textBox1.Text); SqlConnectionobjSqlConnection=newSqlConnection("server=127.0.0.1;uid=sa;pwd=;database=test"); objSqlConnection.Open(); MessageBox.Show("数据库连接成功","好"); try { SqlCommandsqlcom=newSqlCommand("updateinfosetname='"+s1+"',sex='"+s2+"'"+"whereid="+i,objSqlConnection); sqlcom.ExecuteNonQuery(); MessageBox.Show("修改成功!","啊"); objSqlConnection.Close(); } catch(Exceptiona) { MessageBox.Show(a.ToString()); } 四:数据库的删除代码:
inti=0; strings1="",s2=""; s1=textBox2.Text; s2=textBox3.Text; if(textBox1.Text.Length==0) i=0; else i=Convert.ToInt16(textBox1.Text); SqlConnectionobjSqlConnection=newSqlConnection("server=127.0.0.1;uid=sa;pwd=;database=test"); objSqlConnection.Open(); MessageBox.Show("数据库连接成功","好"); try { SqlCommandsqlcom=newSqlCommand("deletefrominfowhereid="+i,objSqlConnection); sqlcom.ExecuteNonQuery(); MessageBox.Show("删除成功!","啊"); objSqlConnection.Close(); } catch(Exceptiona) { MessageBox.Show(a.ToString()); } 五:数据库的查询代码:
1.类开始:
DataTabledt1=newDataTable(); SqlDataAdapterda1=newSqlDataAdapter(); 2.按钮代码:
inti=0,n=0; strings1="",s2=""; s1=textBox2.Text; s2=textBox3.Text; if(textBox1.Text.Length==0) i=0; else i=Convert.ToInt32(textBox1.Text); SqlConnectionobjSqlConnection=newSqlConnection("server=127.0.0.1;uid=sa;pwd=;database=test"); objSqlConnection.Open(); MessageBox.Show("数据库连接成功","好"); stringquery="SELECT*frominfowhereid="+i; DataSetobjDataSet=newDataSet(); SqlDataAdapterobj=newSqlDataAdapter(); obj.SelectCommand=newSqlCommand(query,objSqlConnection); obj.Fill(objDataSet,"info"); SqlCommandobjSqlCommand=newSqlCommand(query,objSqlConnection); SqlDataReaderobjSqlReader=objSqlCommand.ExecuteReader(); while(objSqlReader.Read()) { n+=1; MessageBox.Show("编号:"+objSqlReader.Getvalue(0)+"姓名:"+objSqlReader.Getvalue(1)+"性别"+objSqlReader.Getvalue(2)); } if(n==0) MessageBox.Show("数据库中没有这样的记录!"); 六:C#数据库查询代码:
inti=0; //intn=0; strings1="",s2=""; stringsql; s1=textBox2.Text; s2=textBox3.Text; if(textBox1.Text.Length==0) { i=0; } else i=Convert.ToInt32(textBox1.Text); SqlConnectionobjSqlConnection=newSqlConnection("server=127.0.0.1;uid=sa;pwd=;database=test"); objSqlConnection.Open(); MessageBox.Show("数据库连接成功","好"); stringquery="SELECT*frominfowhereid="+i; if(i==0) sql="select*frominfo"; else sql="select*frominfowhereid="+i; da1=newSqlDataAdapter(sql,objSqlConnection); dt1.Clear(); da1.Fill(dt1); dataGridView1.DataSource=dt1; C#数据库查询之数据库的封装类代码:
classDBClass { publicvoiddbclass(stringsql) { try { SqlConnectionsqlcon=newSqlConnection("server=127.0.0.1;uid=sa;pwd=;database=test"); sqlcon.Open(); SqlTransactionobjt=sqlcon.BeginTransaction();//事物开始 SqlCommandsqlcom=newSqlCommand(sql,sqlcon); sqlcom.Transaction=objt;//将Command对象设置为事物处理的对象 sqlcom.ExecuteNonQuery(); objt.Commit();//提交事物 sqlcon.Close(); } catch(Exceptiona) { MessageBox.Show(a.ToString()); } } } --db2数据库连接代码: stringstrcon="Provider=IBMDADB2;DataSource=hfzd;UserId=db2admin;Password=db2admin"; //stringsql="select*fromADMINISTRATOR.HFZD"; stringsql="deletefromADMINISTRATOR.HFZDwhereID=1"; OleDbConnectionolecon=newOleDbConnection(strcon); olecon.Open(); MessageBox.Show("数据库已连接上"); dt.Clear(); da=newOleDbDataAdapter(sql,olecon); da.Fill(dt); dataGridView1.DataSource=dt; olecon.Close();
一:C#数据库查询之数据库连接代码:
SqlConnectionobjSqlConnection=newSqlConnection("server=127.0.0.1;uid=sa;pwd=;database=test"); objSqlConnection.Open(); 二:数据库的添加记录代码:
inti=0; strings1="",s2=""; i=Convert.ToInt16(textBox1.Text); s1=textBox2.Text; s2=textBox3.Text; SqlConnectionobjSqlConnection=newSqlConnection("server=127.0.0.1;uid=sa;pwd=;database=test"); objSqlConnection.Open(); MessageBox.Show("数据库连接成功","好"); try { SqlCommandsqlcom=newSqlCommand("insertintoinfo(id,name,sex)values("+i+",'"+s1+"','"+s2+"')",objSqlConnection); sqlcom.ExecuteNonQuery(); MessageBox.Show("添加成功!","啊"); } catch(Exceptiona) { MessageBox.Show(a.ToString()); } MessageBox.Show("添加成功!","啊"); } 三:数据库的修改代码:
inti=0; strings1="",s2=""; s1=textBox2.Text; s2=textBox3.Text; if(textBox1.Text.Length==0) i=0; else i=Convert.ToInt32(textBox1.Text); SqlConnectionobjSqlConnection=newSqlConnection("server=127.0.0.1;uid=sa;pwd=;database=test"); objSqlConnection.Open(); MessageBox.Show("数据库连接成功","好"); try { SqlCommandsqlcom=newSqlCommand("updateinfosetname='"+s1+"',sex='"+s2+"'"+"whereid="+i,objSqlConnection); sqlcom.ExecuteNonQuery(); MessageBox.Show("修改成功!","啊"); objSqlConnection.Close(); } catch(Exceptiona) { MessageBox.Show(a.ToString()); } 四:数据库的删除代码:
inti=0; strings1="",s2=""; s1=textBox2.Text; s2=textBox3.Text; if(textBox1.Text.Length==0) i=0; else i=Convert.ToInt16(textBox1.Text); SqlConnectionobjSqlConnection=newSqlConnection("server=127.0.0.1;uid=sa;pwd=;database=test"); objSqlConnection.Open(); MessageBox.Show("数据库连接成功","好"); try { SqlCommandsqlcom=newSqlCommand("deletefrominfowhereid="+i,objSqlConnection); sqlcom.ExecuteNonQuery(); MessageBox.Show("删除成功!","啊"); objSqlConnection.Close(); } catch(Exceptiona) { MessageBox.Show(a.ToString()); } 五:数据库的查询代码:
1.类开始:
DataTabledt1=newDataTable(); SqlDataAdapterda1=newSqlDataAdapter(); 2.按钮代码:
inti=0,n=0; strings1="",s2=""; s1=textBox2.Text; s2=textBox3.Text; if(textBox1.Text.Length==0) i=0; else i=Convert.ToInt32(textBox1.Text); SqlConnectionobjSqlConnection=newSqlConnection("server=127.0.0.1;uid=sa;pwd=;database=test"); objSqlConnection.Open(); MessageBox.Show("数据库连接成功","好"); stringquery="SELECT*frominfowhereid="+i; DataSetobjDataSet=newDataSet(); SqlDataAdapterobj=newSqlDataAdapter(); obj.SelectCommand=newSqlCommand(query,objSqlConnection); obj.Fill(objDataSet,"info"); SqlCommandobjSqlCommand=newSqlCommand(query,objSqlConnection); SqlDataReaderobjSqlReader=objSqlCommand.ExecuteReader(); while(objSqlReader.Read()) { n+=1; MessageBox.Show("编号:"+objSqlReader.Getvalue(0)+"姓名:"+objSqlReader.Getvalue(1)+"性别"+objSqlReader.Getvalue(2)); } if(n==0) MessageBox.Show("数据库中没有这样的记录!"); 六:C#数据库查询代码:
inti=0; //intn=0; strings1="",s2=""; stringsql; s1=textBox2.Text; s2=textBox3.Text; if(textBox1.Text.Length==0) { i=0; } else i=Convert.ToInt32(textBox1.Text); SqlConnectionobjSqlConnection=newSqlConnection("server=127.0.0.1;uid=sa;pwd=;database=test"); objSqlConnection.Open(); MessageBox.Show("数据库连接成功","好"); stringquery="SELECT*frominfowhereid="+i; if(i==0) sql="select*frominfo"; else sql="select*frominfowhereid="+i; da1=newSqlDataAdapter(sql,objSqlConnection); dt1.Clear(); da1.Fill(dt1); dataGridView1.DataSource=dt1; C#数据库查询之数据库的封装类代码:
classDBClass { publicvoiddbclass(stringsql) { try { SqlConnectionsqlcon=newSqlConnection("server=127.0.0.1;uid=sa;pwd=;database=test"); sqlcon.Open(); SqlTransactionobjt=sqlcon.BeginTransaction();//事物开始 SqlCommandsqlcom=newSqlCommand(sql,sqlcon); sqlcom.Transaction=objt;//将Command对象设置为事物处理的对象 sqlcom.ExecuteNonQuery(); objt.Commit();//提交事物 sqlcon.Close(); } catch(Exceptiona) { MessageBox.Show(a.ToString()); } } } --db2数据库连接代码: stringstrcon="Provider=IBMDADB2;DataSource=hfzd;UserId=db2admin;Password=db2admin"; //stringsql="select*fromADMINISTRATOR.HFZD"; stringsql="deletefromADMINISTRATOR.HFZDwhereID=1"; OleDbConnectionolecon=newOleDbConnection(strcon); olecon.Open(); MessageBox.Show("数据库已连接上"); dt.Clear(); da=newOleDbDataAdapter(sql,olecon); da.Fill(dt); dataGridView1.DataSource=dt; olecon.Close();
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询