C#学习资料关于数据库连接asalleyZIPC#.zip 1.28MB 立即下载资源文件列表:ZIP C#.zip 大约有6个文件 C#/ C#/MySQL Connector NET 8.0.33.1.zip 552.6KB C#/usertable.sql 1.46KB C#/WinFormsAppMain.zip 776.42KB C#/断开式.txt C#/非断开.txt 1.71KB 资源介绍: 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) { } }