日期:2014-05-17  浏览次数:20532 次

cmd.ExecuteNonQuery();返回值为-1
cmd.ExecuteNonQuery();返回值为-1。不知道是那个地方出错了!!!!求高手指点!!
  protected void Button1_Click(object sender, EventArgs e)
  {
  String str = "SELECT * FROM [Car_Park] WHERE CarLisence='" + txtBox1.Text + "'";
  SqlConnection conn = new SqlConnection("Data Source=DEEP-20120227AR;Database=Car_Park;User ID=sa;Password=sa");
   
  SqlCommand cmd = new SqlCommand(str, conn);
  conn.Open();
  if (txtBox1.Text == "")
  {
  this.Response.Write("<script language=javascript>alert('车牌号不能为空!');</script>");
  //return false;
  return;
  }
  if (Convert.ToInt32(cmd.ExecuteScalar()) <= 0)
  {
  this.Response.Write("<script language=javascript>alert('停车场无此车牌号的车辆!');</script>");
  return;
  }
  else
  {
  SqlDataReader sd = cmd.ExecuteReader();
  sd.Read();//从数据库中读取记录。
  string CarportNumber = sd["CarportNumber"].ToString();
  string ParkTime = sd["ParkTime"].ToString();
   
  txtBox2.Text = CarportNumber;
  txtBox3.Text = ParkTime;//分别获取两个变量,同时把这两个变量的值送显示。
  DateTime pt = Convert.ToDateTime(sd["ParkTime"]);
  Decimal money;
  if (sd["CarportNumber"].ToString().Substring(0, 1).Equals("A"))
  {
  money = 0.00M;
  }
  else
  {
  money = Convert.ToDecimal(((Convert.ToInt32(DateTime.Now.ToString("HH")) - Convert.ToInt32(pt.ToString("HH")))) * 1 + 2.00);//此处的时间计算也是有问题的,两天的时间差会出现负值!!!!

  }
  sd.Close();
  conn.Close();
  txtBox4.Text = DateTime.Now.ToString();
  txtBox5.Text = money.ToString();


  conn.Open();
  String Sql = "";
  Sql = "INSERT INTO Car_Park(LiKaiShiJian,Money) values(";
  Sql += "'" + txtBox4.Text + "',";
  Sql += "'" + txtBox5.Text + "')";//此处构造的SQL语句是没有问题的,只是写入不到数据库中。!!!  
  try
  {
  int rows= cmd.ExecuteNonQuery();
   
  }
  catch (Exception err)
  {
  MessageBox.Show(err.Message, "修改记录失败");
  }
  finally
  {
  conn.Close();

  }
   
  }
   
  }

------解决方案--------------------
你的这个cmd还是执行上面的查询语句 ,怎么插入数据呢? 改成下面红色部分

protected void Button1_Click(object sender, EventArgs e)
{
String str = "SELECT * FROM [Car_Park] WHERE CarLisence='" + txtBox1.Text + "'";
SqlConnection conn = new SqlConnection("Data Source=DEEP-20120227AR;Database=Car_Park;User ID=