110,536
社区成员
发帖
与我相关
我的任务
分享
private void btnAdd_Click(object sender, EventArgs e)
{
if (string.IsNullOrEmpty(textBox1.Text.Trim()) || string.IsNullOrEmpty(textBox2.Text.Trim()) || string.IsNullOrEmpty(textBox3.Text.Trim()))
{
MessageBox.Show("所有项都必须填写!","信息框");
return;
}
string str="Persist Security Info=False;Initial Catalog=student;Data Source=localhost;Integrated Security=SSPI;";
string str1="insert into student(studentID,studentName,studentSex)Values(@id,@name,@sex)";
try
{
conn = new SqlConnection(str);
conn.Open();//打开数据库连接
da= new SqlCommand(str1, conn);
da.Parameters.Add("@id",SqlDbType.VarChar).Value=textBox1.Text.Trim();
da.Parameters.Add("@name",SqlDbType.VarChar).Value=textBox2.Text.Trim();
da.Parameters.Add("@sex",SqlDbType.VarChar).Value=textBox3.Text.Trim();
da.ExecuteNonQuery();//执行SQL语句
textBox1.Text = "";
textBox2.Text = "";
textBox3.Text = "";
conn.Close();
}
catch (SqlException oe)
{
MessageBox.Show(oe.Message,"Error");
}
finally
{
if (conn.State == ConnectionState.Open)
conn.Close();
}
}
}