C# C语言中的三层数据库
代码中没有错误,但数据库中没有插入数据 下面是每层的代码 用户层:C# C语言中的三层数据库,c#,C#,代码中没有错误,但数据库中没有插入数据 下面是每层的代码 用户层: private void btnSave_Click(object sender, EventArgs e) { EmpProps p = new EmpProps(); p.Code1 = Convert.ToInt32(tfId.Text); p.Name1 = tfName.Text; p.Cell1 = tfCell.Text; p.
private void btnSave_Click(object sender, EventArgs e)
{
EmpProps p = new EmpProps();
p.Code1 = Convert.ToInt32(tfId.Text);
p.Name1 = tfName.Text;
p.Cell1 = tfCell.Text;
p.Adrs1 = tfAdrs.Text;
p.Dept1 = cmbDept.Text;
EmpBll eb = new EmpBll();
bool b =eb.InsertEmpBll(p);
if(b)
{ MessageBox.Show("Saved successfully");
}
else
{ MessageBox.Show("Error Ocurred");
}
}
逻辑层:
public class EmpBll
{
public bool InsertEmpBll(EmpProps p)
{
EmpDal empdal = new EmpDal();
bool b =empdal.InsrtEmpDal(p);
if (b)
return true;
else
return false;
}
}
数据访问层:
public class EmpDal
{
public bool InsrtEmpDal(EmpProps p)
{
SqlConnection conn = new SqlConnection("Data Source=DASTGIRKHAN\\SQLEXPRESS;Initial Catalog=MultilayerManagementSystem;Integrated Security=True;Pooling=False");
SqlCommand cmd = new SqlCommand("Insert INTO EmployeeRecord Values(" + p.Code1.GetType() + ",'" +p.Name1 + "','" + p.Cell1 + "','" +p.Adrs1 + "','" + p.Dept1 + "')", conn);
conn.Open();
int c= cmd.ExecuteNonQuery();
conn.Close();
if (c > 0)
return true;
else
return false;
}
}
您是否有发布示例时从代码中删除的try-catch?没有,我没有应用try-catch,但没有发生异常…您的程序也存在其他问题。您确实应该在可处置对象(如SqlConnection和SqlCommand)上使用语句,还应该使用而不是字符串添加来传递到查询中。如果你不知道,你能告诉我如果我的部门叫哈克斯会发生什么吗;下拉表EmployeeRecord;-?我无法理解。如果可能,请更改给定的代码…谢谢