private void button1_Click(object sender, EventArgs e)
{
string name = this.textBox1.Text;
string gender = this.cb_sex.Text;
string birthday = this.dateTimePicker1.Text;
string area = this.cb_area.Text;
//非空验证
if (name=="")
{
MessageBox.Show("歌手名称不能为空");
this.textBox1.Focus();
return;
}
//创建Connection 对象
SqlConnection conn = new SqlConnection(DBHelper.ConnStr);
//打开Connection对象
conn.Open();
//定义新增歌手的Sql语句
string sqlStr = string.Format("insert into Singer(SingerName,SingerType,Birthday,Area)values('{0}','{1}','{2}','{3}')",name,gender,birthday,area);
SqlCommand cmd = new SqlCommand(sqlStr,conn);
int rows = cmd.ExecuteNonQuery();
//关闭连接
conn.Close();
//根据返回的受影响行数判断添加是否成功
if (rows>0)
{
MessageBox.Show("歌星添加成功!");
}
else
{
MessageBox.Show("歌星添加失败!");
}
}