protected void Button1_Click(object sender, EventArgs e)
{
string SqlStr = ConfigurationManager.ConnectionStrings["conn"].ToString();
SqlConnection conn = new SqlConnection(SqlStr);
conn.Open();
string sql3 = "select 編號 from CS where 編號 = '" + TextBox1.Text.Trim() + "'";
SqlCommand cmd3 = new SqlCommand(sql3, conn);
cmd3.CommandText = sql3;
cmd3.Connection = conn;
try
{
string BH = cmd3.ExecuteScalar().ToString();
string sql2 = "update CS set 數量 = 數量+1 where 編號 = " + TextBox1.Text.Trim();
SqlCommand cmd2 = new SqlCommand(sql2, conn);
Response.Write("<script>alert('插入成功')</script>");
}
catch
{
string sql1 = "insert into CS (編號,索書號,書名,作者,數量) values (";
sql1 += "'" + TextBox1.Text.Trim() + "','" + TextBox2.Text.Trim() + "','" + TextBox3.Text.Trim() +
"','" + TextBox4.Text.Trim() + "','" + "1" + "')";
SqlCommand cmd1 = new SqlCommand(sql1, conn);
Response.Write("<script>alert('插入成功')</script>");
}
conn.Close();
}
第一條語句執行沒問題,後面兩條語句不報錯但沒有結果,我的是SQL數據庫
SqlCommand cmd = new SqlCommand(sql, conn);
你應該要在這一句後面加上 cmd.ExecuteNonQuery();
cmd.ExecuteNonQuery()才是cmd的執行,你現在只是賦值,並沒有執行cmd語句