做了一个后台界面,往数据库里添加数据。用到两个表,一个表是Software,主键是SoftwareID,另一个是子表SoftwareDetail,主键是DetailID,外键是SoftwareID。
图片只添加物理路径,而且图片已经存在Software文件夹里。
参考别人做的,代码是这样的:
protected void Addbtn_Click(object sender, EventArgs e)
{
string Softwareimgname = this.SoftwareImg.PostedFile.FileName;
string Softwareimg = Softwareimgname.Substring(Softwareimgname.LastIndexOf("\\") + 1);
string Softwarename = SoftwareName.Text.ToString();
string Softwareintro = softwareIntro.Text.ToString();
string Softwaredetail = softwareDetail.Text.ToString();
try
{
AddSoftware(Softwarename, Softwareimg, Softwareintro, Softwaredetail);
Response.Write("<script>alert('" + "已成功添加软件信息!" + "');</script>");
Response.Write("<script>window.location='SoftwareList.aspx';</script>");
}
catch (Exception ex)
{
Response.Redirect("ErrorPage.aspx?ErrorMsg=" + ex.Message + "&ErrorUrl=" + Request.Url.ToString());
}
}
public int AddSoftware(string sSoftwarename, string sSoftwareimg, string sSoftwareintro, string sSoftwaredetail)
{
Response.Write("<script>alert('" + "已成功添加软件信息!" + "');</script>");
SqlConnection conn = db.CreateConnection();
string cmdtext1 = "insert into Software (SoftwareName,SoftwareImg,SoftwareIntro) values ('" + sSoftwarename + "','" + "~\\Images\\Software\\" + sSoftwareimg + "','" + sSoftwareintro + "')";
SqlCommand myCommand1 = new SqlCommand(cmdtext1, conn);
string cmdtext2 = "insert into SoftwareDetail (SoftwareName,SoftwareImg,SoftwareDetail) values ('" + sSoftwarename + "','" + "~\\Images\\Software\\" + sSoftwareimg + "','" + sSoftwaredetail + "')";
SqlCommand myCommand2 = new SqlCommand(cmdtext2, conn);
int nResult = -1;
int nResult1 = -1;
try
{
conn.Open();
nResult = myCommand1.ExecuteNonQuery();
nResult1 = myCommand2.ExecuteNonQuery();
}
catch (SqlException ex)
{
throw new Exception(ex.Message, ex);
}
finally
{
conn.Close();
}
return nResult;
不报错,但是点了添加后,不提示,也无法添加进去,页面只是刷新了一下。
求教各位高手,这究竟是哪里的问题。。。