JAVA日报

简单.net增删改查(添加,mysql数据库连接)

string xh = textBox1.Text;
string name= textBox3.Text;
string age= textBox4.Text;
string phone= textBox6.Text;
string area=textBox5.Text;
getCom("insert into information(xh,name,age,phone,area) values('"+xh+"','"+name+"','"+age+"','"+phone+"','"+area+"')");
MessageBox.Show("添加成功");
show();

 

public MySqlConnection getConn()
{
string MyConStr = "server=localhost;user id=root;password=123;database=text3";
MySqlConnection conn = new MySqlConnection(MyConStr);
conn.Open();
if (conn.State == ConnectionState.Open)
{
Console.WriteLine("Connection Opened Successfully ");
}
return conn;
}//获取连接

public void getCom(string str)
{
MySqlConnection conn = getConn();
MySqlCommand mysqlcom = new MySqlCommand(str, conn);
mysqlcom.ExecuteNonQuery();
mysqlcom.Dispose();
conn.Close();
conn.Dispose();
}//执行sql语句
public MySqlDataReader getReader(string str)
{
MySqlConnection conn = getConn();
MySqlCommand mysqlcom = new MySqlCommand(str, conn);
MySqlDataReader mysqlread = mysqlcom.ExecuteReader(CommandBehavior.CloseConnection);
return mysqlread;
}/返回MySqlDataReader对象  用于信息遍历

posted @ 2021-11-29 20:18  我的未来姓栗山  阅读(18)  评论(0编辑  收藏  举报