我在向数据库添加数据时,想判断数据库中记录是否存在,但总是出错,哪位高手帮忙看一下:
//SqlCommand myCmd = new SqlCommand("select Pnumber,Pname from Product where Pnumber='" + txtnumber.Text + "'and Pname='" + txtname.Text + "'", myCon);
//myCon.Open();
//SqlDataReader dr = myCmd.ExecuteReader();
insertCmd.Parameters.Add("@Pnumber", SqlDbType.NVarChar, 20);
insertCmd.Parameters.Add("@Pdate", SqlDbType.NVarChar, 20);
insertCmd.Parameters.Add("@Pname", SqlDbType.NVarChar, 20);
insertCmd.Parameters.Add("@Pamount", SqlDbType.Int, 20);
insertCmd.Parameters.Add("@Psort", SqlDbType.NVarChar, 20);
insertCmd.Parameters.Add("@Pprice", SqlDbType.Float, 20);
insertCmd.Parameters.Add("@Psuppliers", SqlDbType.NVarChar, 20);
insertCmd.Parameters.Add("@Premarks", SqlDbType.NVarChar, 20);
insertCmd.Parameters.Add("@Poperator", SqlDbType.NVarChar, 20);
insertCmd.Parameters.Add("@Pmaker", SqlDbType.NVarChar, 20);
insertCmd.Parameters["@Pdate"].Value = txtdate.Text.ToString();
insertCmd.Parameters["@Pnumber"].Value = txtnumber.Text;
insertCmd.Parameters["@Pname"].Value = txtname.Text;
insertCmd.Parameters["@Pamount"].Value = txtamount.Text;
insertCmd.Parameters["@Psort"].Value = "";
insertCmd.Parameters["@Pprice"].Value = txtprice.Text;
insertCmd.Parameters["@Psuppliers"].Value = txtsuppliers.Text;
insertCmd.Parameters["@Premarks"].Value = txtremarks.Text;
insertCmd.Parameters["@Poperator"].Value = txtoperator.Text;
insertCmd.Parameters["@Pmaker"].Value = txtmaker.Text;
try
{
if (dr.Read())
{
Response.Write("<script> alert('该成品已存在!');location='addproduct.aspx'</script>");
}
else
{
insertCmd.Connection.Open();
insertCmd.ExecuteNonQuery();
Response.Write("<script> alert('添加成功!');location='addproduct.aspx'</script>");
insertCmd.Connection.Close();
}
}
catch
{
Response.Write("<script> alert('添加失败,请重新添加!');location='addproduct.aspx'</script>");
}