我照这书敲的代码,以下代码可以连上access,但我想连mysql该怎么连,谢谢大家,初学c#真的遇到好多不懂的东西,
static void Main(){
OleDbConnection thisconnection = new OleDbConnection(@"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=d:\db1.mdb");
thisconnection.Open(); OleDbCommand thiscommand = thisconnection.CreateCommand(); thiscommand.CommandText = "select * from customer"; OleDbDataReader thisreader = thiscommand.ExecuteReader();
while (thisreader.Read())
{
Console.WriteLine("\t{0}\t{1}", thisreader["name"], thisreader["password"]);
}
thisreader.Close();
thisconnection.Close();
}
static void Main(){
OleDbConnection thisconnection = new OleDbConnection(@"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=d:\db1.mdb");
thisconnection.Open(); OleDbCommand thiscommand = thisconnection.CreateCommand(); thiscommand.CommandText = "select * from customer"; OleDbDataReader thisreader = thiscommand.ExecuteReader();
while (thisreader.Read())
{
Console.WriteLine("\t{0}\t{1}", thisreader["name"], thisreader["password"]);
}
thisreader.Close();
thisconnection.Close();
}
using System.Data.SqlClient;
然后改原先代码为下面的static void Main(){
SqlConnection thisconnection = new SqlConnection("Data Source=10.321.0.3;Initial Catalog=temp;Password=123456;User ID=sa");
thisconnection.Open(); SqlCommand thiscommand = thisconnection.CreateCommand(); thiscommand.CommandText = "select * from customer"; SqlDataReader thisreader = thiscommand.ExecuteReader();
while (thisreader.Read())
{
Console.WriteLine("\t{0}\t{1}", thisreader["name"], thisreader["password"]);
}
thisreader.Close();
thisconnection.Close();
}
MySQLConnection DBConn; = new MySQLConnection(new MySQLConnectionString("localhost","mysql","root","",3306).AsString);
//("IP","数据库名","用户名","密码",端口)根据自己的情况改
DBConn.Open();MySQLCommand DBComm; DBComm = new MySQLCommand("select * from customer",DBConn); MySQLDataReader DBReader = DBComm.ExecuteReaderEx();while (DBReader.Read())
{
Console.WriteLine("\t{0}\t{1}", DBReader["name"], DBReader["password"]);
} DBReader.Close();
DBConn.Close();
using MySQLDriverCS;
//coder BY XXXXXpublic partial class _Default : System.Web.UI.Page{
protected void ImageButton1_Click(object sender, ImageClickEventArgs e)
{ string unamein =utextb.Text.Trim();
string upwdin = ptextb.Text.Trim();
string userid=null, userpwd=null;
if ((unamein != null) && (unamein != "") && (upwdin != null) && (upwdin != ""))
{
MySQLConnection conn = null;
conn = new MySQLConnection(new MySQLConnectionString("localhost", "sbgl", "root", "mysqltest").AsString);
conn.Open(); MySQLCommand commn = new MySQLCommand("set names gb2312", conn); //字符集指定,不不然做汉字操作的时候会出问题
commn.ExecuteNonQuery(); MySQLCommand mycm = new MySQLCommand("select * from suser where uname='" + unamein + "'", conn);
MySQLDataReader msdr = mycm.ExecuteReaderEx();
///判断用户是否合法 if (msdr.Read())
{
userid = msdr["uname"].ToString();
userpwd = msdr["upwd"].ToString();
}
msdr.Close();
conn.Close();
if ((userid == unamein) && (userpwd == upwdin))
{
Session["userid"] = userid;
Server.Transfer("amin.aspx");
}
else
{
Response.Write("<script>alert('对不起,输入错误!!!');</script>");
utextb.Text = "";
ptextb.Text = "";
}
} // enif
else
{
Response.Write("<script>alert('对不起,输入错误!!');</script>");
utextb.Text = "";
ptextb.Text = "";
} // end else
}