数据不在数据库表中 [英] data is not going in table of database
本文介绍了数据不在数据库表中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
string constr = @"Data Source=VISHAL;Initial Catalog=DrivingSchool;Integrated Security=True";
SqlCommand cmd = new SqlCommand();
private void btnSubmit_Click(object sender, EventArgs e)
{
using (SqlConnection cn = new SqlConnection(constr))
{
if (txtFName.Text == "" || txtLName.Text == "" || txtAge.Text == "" || cmbDate.Text == "" || cmbMonth.Text == "" || txtYear.Text == "" || cmbGender.Text == "" || txtAddress.Text == "" || txtContactno.Text == "" || cmbQualification.Text == "")
{
MessageBox.Show("Fields Cannot be empty");
}
else
{
MessageBox.Show("Employee Added Successfully");
}
try
{
cn.Open();
cmd = new SqlCommand("insert into AddEmployee values('" + txtFName.Text + "','" + txtLName.Text + "','" + txtAge.Text + "','" + cmbGender.Text + "','" + cmbDate.Text + "','" + cmbMonth.Text + "','" + txtYear.Text + "','" + txtAddress.Text + "','" + txtContactno.Text + "','" + cmbQualification.Text + "')", cn);
cmd.ExecuteNonQuery();
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
finally
{
cn.Close();
}
}
推荐答案
Hi Vishal,
您只是验证所有控件中的值,如果它不为空,则表示消息员工已成功添加,甚至没有执行查询。
试试这段代码:
Hi Vishal,
You are just validating values in all the controls, if its not empty you are giving message "Employee added successfully" without even executing the query.
Try this code:
string constr = @"Data Source=VISHAL;Initial Catalog=DrivingSchool;Integrated Security=True";
SqlCommand cmd = new SqlCommand();
private void btnSubmit_Click(object sender, EventArgs e)
{
using (SqlConnection cn = new SqlConnection(constr))
{
if (txtFName.Text == "" || txtLName.Text == "" || txtAge.Text == "" || cmbDate.Text == "" || cmbMonth.Text == "" || txtYear.Text == "" || cmbGender.Text == "" || txtAddress.Text == "" || txtContactno.Text == "" || cmbQualification.Text == "")
{
MessageBox.Show("Fields Cannot be empty");
}
else
{
try
{
cn.Open();
cmd = new SqlCommand("insert into AddEmployee values('" + txtFName.Text + "','" + txtLName.Text + "','" + txtAge.Text + "','" + cmbGender.Text + "','" + cmbDate.Text + "','" + cmbMonth.Text + "','" + txtYear.Text + "','" + txtAddress.Text + "','" + txtContactno.Text + "','" + cmbQualification.Text + "')", cn);
int isInserted = cmd.ExecuteNonQuery();
if (isInserted == 1)
{
MessageBox.Show("Employee Added Successfully");
}
else
{
MessageBox.Show("Employee insertion failed");
}
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
}
finally
{
cn.Close();
}
}
希望这会对你有所帮助。
问候,
RK
Hope this helps you a bit.
Regards,
RK
这篇关于数据不在数据库表中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文