重庆分公司,新征程启航
为企业提供网站建设、域名注册、服务器等服务
首先呢,去Mysql官网下载MySql.Data.dll,然后添加引用
创新互联建站长期为1000+客户提供的网站建设服务,团队从业经验10年,关注不同地域、不同群体,并针对不同对象提供差异化的产品和服务;打造开放共赢平台,与合作伙伴共同营造健康的互联网生态环境。为八步企业提供专业的成都网站建设、做网站,八步网站改版等技术服务。拥有十载丰富建站经验和众多成功案例,为您定制开发。
using MySql.Data.MySqlClient;
然后使用连接字符串的时候,使用以下代码:
MySqlConnectionStringBuilder s = new MySqlConnectionStringBuilder();
s.Server = "192.168.0.100";
s.Port = 3306; //mysql端口号
s.Database = "dbName";
s.UserID = "root";
s.Password = "root";
s.CharacterSet = "latin1";
MySqlConnection mcon = new MySqlConnection(s.ConnectionString);
其他的也要用mysql的,比如MySqlCommand等等。有疑问可以追问~
string query = "select * from guestbook";
MySqlConnection myConnection = new MySqlConnection("server=localhostone;user id=rootone;password= *****;database=hiwperone");
MySqlCommand myCommand=new MySqlCommand(query,myConnection);
myConnection.Open();
myCommand.ExecuteNonQuery();
MySqlDataReader myDataReader = myCommand.ExecuteReader();
while (myDataReader.Read()==true)
{
//执行数据库操作
}
myDataReader.Close();
myConnection.Close();
}
详细出处参考:
String mysqlStr = "Database=test;Data Source=127.0.0.1;User Id=root;Password=root;pooling=false;CharSet=utf8;port=3306";
MySqlConnection mysql = new MySqlConnection(mysqlStr);
或者
string MyConString = "DRIVER={MySQL ODBC 3.51 Driver};" +
"SERVER=localhost;" +
"DATABASE=inv;" +
"UID=root;" +
"PASSWORD=root;" +
"OPTION=3";
OdbcConnection MyConnection = newOdbcConnection(MyConString);
在sql中给所有字段值加上mysql_real_escape_string函数处理,作用就是给特殊字符进行转义,具体说明网上找
$sql="update `qqaide_ini` set `键值`='" . mysql_real_escape_string($keynamearr[$i+1]) . "' WHERE `PCHash`='" . mysql_real_escape_string($PCHash) . "' and `小节`='" . mysql_real_escape_string($notename) . "' and `键名`='" . mysql_real_escape_string($keynamearr[$i]) . "'";
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();
}
}
}