C#向数据库中增加数据
privatevoidbutton1_Click(objectsender,EventArgse)//确定按钮{if(textBox1.Text==""||textBox...
private void button1_Click(object sender, EventArgs e)//确定按钮
{
if (textBox1.Text == "" || textBox2.Text == "" || textBox3.Text == "" || textBox4.Text == "" || textBox5.Text == ""
|| textBox6.Text == "" || textBox7.Text == "" || textBox8.Text == "" || textBox9.Text == "" || textBox10.Text == "")//数据不能为空
{
MessageBox.Show("信息有空缺,请填完整");
}
else
{
try
{
conn.Open();//打开数据库连接
SqlCommand cmd = new SqlCommand("select * from tbl_Employee_Info where empId='" + textBox1.Text.Trim() + "' ", conn);
SqlDataReader sdr = cmd.ExecuteReader();
if (sdr.Read())//验证要添加的数据是否存在
{
sdr.Close();
MessageBox.Show("员工编号已存在");
}
else//要添加的数据不存在,继续操作
{
sdr.Close();
SqlCommand scmd = new SqlCommand("insert into tbl_Employee_Info values('" + textBox1.Text.Trim() +
"','" + textBox2.Text.Trim() + "','" + textBox3.Text.Trim() + "','" + textBox4.Text.Trim() +
"','" + textBox5.Text.Trim() + "','" + textBox6.Text.Trim() + "','" + textBox7.Text.Trim() +
"','" + textBox8.Text.Trim() + "','" + textBox9.Text.Trim() + "','" + textBox10.Text.Trim() + "')", conn);
int i = scmd.ExecuteNonQuery();//新增一条记录,返回值为1
if (i == 1)
{
MessageBox.Show("添加成功");
}
conn.Close();
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
}
添加一条数据后总是会出现下面情况:
求大神指点下,问题出哪了? 展开
{
if (textBox1.Text == "" || textBox2.Text == "" || textBox3.Text == "" || textBox4.Text == "" || textBox5.Text == ""
|| textBox6.Text == "" || textBox7.Text == "" || textBox8.Text == "" || textBox9.Text == "" || textBox10.Text == "")//数据不能为空
{
MessageBox.Show("信息有空缺,请填完整");
}
else
{
try
{
conn.Open();//打开数据库连接
SqlCommand cmd = new SqlCommand("select * from tbl_Employee_Info where empId='" + textBox1.Text.Trim() + "' ", conn);
SqlDataReader sdr = cmd.ExecuteReader();
if (sdr.Read())//验证要添加的数据是否存在
{
sdr.Close();
MessageBox.Show("员工编号已存在");
}
else//要添加的数据不存在,继续操作
{
sdr.Close();
SqlCommand scmd = new SqlCommand("insert into tbl_Employee_Info values('" + textBox1.Text.Trim() +
"','" + textBox2.Text.Trim() + "','" + textBox3.Text.Trim() + "','" + textBox4.Text.Trim() +
"','" + textBox5.Text.Trim() + "','" + textBox6.Text.Trim() + "','" + textBox7.Text.Trim() +
"','" + textBox8.Text.Trim() + "','" + textBox9.Text.Trim() + "','" + textBox10.Text.Trim() + "')", conn);
int i = scmd.ExecuteNonQuery();//新增一条记录,返回值为1
if (i == 1)
{
MessageBox.Show("添加成功");
}
conn.Close();
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
}
添加一条数据后总是会出现下面情况:
求大神指点下,问题出哪了? 展开
2个回答
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询