C#学习资料关于数据库连接
立即下载
资源介绍:
C#学习资料关于数据库连接
private void button1_Click(object sender, EventArgs e)
{
try
{
String user = textBoxUser.Text;
String password = textBoxPassword.Text;
String sex;
String normal;
StringBuilder sb = new StringBuilder();
if (radioButtonBoy.Checked) { sex = "男"; }
else { sex = "女"; }
if (checkBoxNET.Checked) { sb.Append(".NET "); }
if (checkBoxC.Checked) { sb.Append("C "); }
if (checkBoxJAVA.Checked) { sb.Append("JAVA "); }
normal = sb.ToString();
string city = comboBox1.SelectedItem?.ToString() ?? "";
string love = listBox1.SelectedItem?.ToString() ?? "";
string mysqlcon = "server=localhost;database=usertable;user=root;password=123456";
string SQL = "INSERT INTO user (user, password, sex, city, normal, love) VALUES (@user, @password, @sex, @city, @normal, @love)";
using (MySqlConnection conn = new MySqlConnection(mysqlcon))
{
conn.Open();
using (MySqlCommand MC = new MySqlCommand(SQL, conn))
{
MC.Parameters.AddWithValue("@user", textBoxUser.Text);
MC.Parameters.AddWithValue("@password", textBoxPassword.Text);
MC.Parameters.AddWithValue("@sex", sex);
MC.Parameters.AddWithValue("@city", city);
MC.Parameters.AddWithValue("@normal", normal);
MC.Parameters.AddWithValue("@love", love);
MC.ExecuteNonQuery();
MessageBox.Show("添加注册成功");
}
}
Console.WriteLine("已关闭连接!");
}
catch (MySqlException ex) { }
}