private string MySqlCon = “server=127.0.0.1;user id=root;password=root;database=data”;
MySqlCommand cmd;
MySqlConnection con;
MySqlDataAdapter msda;
public MySQLConn()
{
con = new MySqlConnection(MySqlCon);
con.Open();//可以省略,不过建议最好写上
}
public DataTable ExecuteQuery(string sqlStr)
{//查
cmd = new MySqlCommand(sqlStr,con);
cmd.CommandType = CommandType.Text;
DataTable dt = new DataTable();
msda = new MySqlDataAdapter(cmd);
msda.Fill(dt);
con.Close();
return dt;
}
public int ExecuteUpdate(string sqlStr)
{//增删改
//MySqlCommand cmd;
//MySqlConnection con;
con = new MySqlConnection(MySqlCon);
con.Open();//可以省略,不过建议最好写上
cmd = new MySqlCommand(sqlStr, con);
cmd.CommandType = CommandType.Text;
int iud = 0;
iud = cmd.ExecuteNonQuery();
con.Close();
return iud;
}