111,125
社区成员
发帖
与我相关
我的任务
分享 Program NewUser=new Program("localhost","kuaso",InputUsername,InputPwd);
//建立连接
MySQLConnection conn= null;
conn = new MySQLConnection(new MySQLConnectionString(NewUser.Host,NewUser.Dbname,NewUser.Username,NewUser.Pwd).AsString);
//conn.Open();
try
{
conn.Open();
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
return;
}
finally
{
}
//执行查询
MySQLCommand com = new MySQLCommand("set names gb2312", conn);
com.ExecuteNonQuery();
MySQLCommand comm;
comm = new MySQLCommand("select * from kuaso_dh_goodlinks", conn);
// 读取
MySQLDataReader reader = comm.ExecuteReaderEx();
//显示资料
Console.Write("\n\n\n");
while (reader.Read()){
Console.WriteLine("Host={0},\n\rUser={1}",reader.GetString(0),reader.GetString(1));
}
reader.Close();
conn.Close();
//给你完整的一个代码,你一看就OK了,一至这样用哈,没得问题!
private void button1_Click(object sender, EventArgs e)
{
MySQLConnection conn = null;
conn = new MySQLConnection(new MySQLConnectionString("localhost", "dif30test", "root", "qaz").AsString);
try
{
conn.Open();
MySQLCommand com = new MySQLCommand("set names gb2312", conn);
com.ExecuteNonQuery();
MySQLDataAdapter dap = new MySQLDataAdapter("select * from dif_drug", conn);
DataSet ds = new DataSet();
dap.Fill(ds, "drugs");
dgv1.DataSource = ds.Tables[0].DefaultView;
}
finally
{
conn.Close();
}
}