SQL数据库连接、创建、访问数据库、可自定义命令执行并获取反馈的结果
public void allcommand(string command)
{
MySqlConnection conn = new MySqlConnection(MYSQL);
MySqlCommand cmd = new MySqlCommand(command, conn);
conn.Open();
MySqlDataAdapter adap = new MySqlDataAdapter(cmd);
DataSet ds = new DataSet();
adap.Fill(ds);
dataGridView1.DataSource = ds.Tables[0].DefaultView;
MySqlDataReader sdr = cmd.ExecuteReader();
while (sdr.Read())
{
MessageBox.Show(sdr[0].ToString());
}
//cmd.ExecuteNonQuery();
cmd.Dispose();
conn.Close();
}
private void button1_Click(object sender, EventArgs e)
{
string mysqlcommandtxt = "CREATE DATABASE " textBox1.Text;
allcommand(mysqlcommandtxt);
textBox2.Text = "CREATE DATABASE " textBox1.Text "\r\n";
textBox1.Clear();
textBox1.Select();
}
private void button2_Click(object sender, EventArgs e)
{
//string mysqlcommandtxt = "CREATE TABLE Test3" "(" textBox1.Text ")";
//textBox2.Text = mysqlcommandtxt "\r\n";
allcommand(textBox1.Text);
textBox2.Text = textBox1.Text "\r\n";
textBox1.Clear();
textBox1.Select();
}
private void button3_Click(object sender, EventArgs e)
{
//string mysqlcommandtxt = "ALTER TABLE Test3 ADD " textBox1.Text;
//textBox2.Text = mysqlcommandtxt "\r\n";
allcommand(textBox1.Text);
textBox2.Text = textBox1.Text "\r\n";
textBox1.Clear();
textBox1.Select();
}
private void Form1_Load(object sender, EventArgs e)
{
textBox1.Select();
MYSQL =ConfigurationManager.AppSettings["mysql"];
}
评论