首先呢,去Mysql官網(wǎng)下載MySql.Data.dll,然后添加引用
創(chuàng)新互聯(lián)建站長(zhǎng)期為1000+客戶提供的網(wǎng)站建設(shè)服務(wù),團(tuán)隊(duì)從業(yè)經(jīng)驗(yàn)10年,關(guān)注不同地域、不同群體,并針對(duì)不同對(duì)象提供差異化的產(chǎn)品和服務(wù);打造開放共贏平臺(tái),與合作伙伴共同營(yíng)造健康的互聯(lián)網(wǎng)生態(tài)環(huán)境。為八步企業(yè)提供專業(yè)的成都網(wǎng)站建設(shè)、做網(wǎng)站,八步網(wǎng)站改版等技術(shù)服務(wù)。擁有十載豐富建站經(jīng)驗(yàn)和眾多成功案例,為您定制開發(fā)。
using MySql.Data.MySqlClient;
然后使用連接字符串的時(shí)候,使用以下代碼:
MySqlConnectionStringBuilder s = new MySqlConnectionStringBuilder();
s.Server = "192.168.0.100";
s.Port = 3306; //mysql端口號(hào)
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)
{
//執(zhí)行數(shù)據(jù)庫(kù)操作
}
myDataReader.Close();
myConnection.Close();
}
詳細(xì)出處參考:
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函數(shù)處理,作用就是給特殊字符進(jìn)行轉(zhuǎn)義,具體說明網(wǎng)上找
$sql="update?`qqaide_ini`?set?`鍵值`='"?.?mysql_real_escape_string($keynamearr[$i+1])?.?"'?WHERE?`PCHash`='"?.?mysql_real_escape_string($PCHash)?.?"'?and?`小節(jié)`='"?.?mysql_real_escape_string($notename)?.?"'?and?`鍵名`='"?.?mysql_real_escape_string($keynamearr[$i])?.?"'";
1、先下載和安裝MySQLDriverCS
2、在安裝文件夾下面找到MySQLDriver.dll,然后將MySQLDriver.dll添加引用到項(xiàng)目中
3、參考代碼:(個(gè)別引用非必須)
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();
}
}
}