string sqlstr = "insert into lkinfo (outtime) values('" + OutTime + "') where outtime = '" + OutTimeDb + "' and room_id = '" + Room_id + "' and LKName = '" + LkName + "'";
这句代码有什么错误吗?调试的时候总是提示where附近有语法错误。

解决方案 »

  1.   

     string sqlstr = "insert into lkinfo(outtime) values('" + OutTime + "')";
      

  2.   

    也就是说后面不能跟where 条件是吧?
      

  3.   

    后面加where做啥呢,insert不需要~
      

  4.   

    第一下没反应过来
    看了语句 没发现错误  
    看了下面 的回复 才发现是 insert  开始还觉得是 update
      

  5.   

    后面加where做啥呢,insert不需要~
      

  6.   

    where条件
    string sqlstr = "insert into lkinfo  select OutTime from tb  where outtime = '" + OutTimeDb + "' and room_id = '" + Room_id + "' and LKName = '" + LkName + "'";
      

  7.   


    不能误人子弟
    insert into A
     a,b,c,d
    select a,b,c,d
    from B
    where a<=b
      

  8.   


    我按照这种写法往里套,
    //insert into A a,b,c,d select a,b,c,d from B where a<=b
    string sqlstr = "insert into lkinfo outtime select outtime from lkinfo where outtime = '" + OutTimeDb + "' and room_id = '" + Room_id + "' and LKName = '" + LkName + "'";结果提示outtime附近有语法错误,不知道是不是哪个地方还是不对呢?
      

  9.   

    insert into lkinfo outtime ??
      

  10.   

    SQL啥时候有这样的语法了???你要插入一条数据还得限制到哪条,那么你这是更新"UPDATE lkinfo SET outtime='" + OutTime + "'
     where outtime = '" + OutTimeDb + "' and room_id = '" + Room_id + "' and LKName = '" + LkName + "'";
      

  11.   

    是的,后来我也发现不能用insert,而只能用update,但是现在的问题是,看上去没错的语句就是插入不进去数据。
    代码如下:        private void button1_Click(object sender, EventArgs e)
            {
                comboBox1.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
                if (comboBox1.Text == "")
                {
                    MessageBox.Show("请选择需要退房的房号,否则无法退房!", "警告!", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }            try
                {
                    string Constr = "server=(local);Integrated Security=true;database=userdb";
                    SqlConnection SqlCon = new SqlConnection(Constr);
                    string Room_id = comboBox1.SelectedItem.ToString();
                    string OutTime = textBox1.Text;
                    string LkName = textBox2.Text;
                    string OutTimeDb = null;                if (OutTime == "")
                    {
                        MessageBox.Show("请输入退房时间,否则无法退房!", "警告!", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    }
                    else if (LkName == "")
                    {
                        MessageBox.Show("请输入登记入住的旅客姓名,否则无法退房!", "警告!", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    }                SqlDataReader dr;
                    SqlCon.Open();
                    SqlCommand cmd = new SqlCommand("select * from lkinfo where room_id = '"
                        + Room_id + "'and LKName = '" + LkName + "' and outtime = '" + OutTimeDb + "'", SqlCon);
                    dr = cmd.ExecuteReader(System.Data.CommandBehavior.CloseConnection);
                    //Room_id = null;
                    OutTimeDb = null;
                    //LkName = null;                if (dr.Read())
                    {
                        Room_id = dr["room_id"].ToString();
                        OutTimeDb = dr["outtime"].ToString();
                        LkName = dr["LKName"].ToString();
                    }
                    SqlCon.Close();                if (LkName != null && OutTimeDb != null && Room_id != null)
                    {
                        MessageBox.Show("您要退的房间本身就是空房间,无法退房!", "警告!", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    }                else
                    {
                        string sqlstr = "update lkinfo set outtime = '" + OutTime + "' where  LKName = '" + LkName + "' and room_id = '" + Room_id + "' and  outtime = '" + OutTimeDb + "'";
                        SqlCon.Open();
                        SqlCommand cmd1 = SqlCon.CreateCommand();
                        cmd1.CommandText = sqlstr;
                        int result = cmd1.ExecuteNonQuery();
                        MessageBox.Show("退房成功!", "提示!", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        comboBox1.Text = "";
                        textBox1.Text = "";
                        this.Close();
                    }
                }            catch (Exception ty)
                {
                    MessageBox.Show(ty.Message);
                }
            }
      

  12.   

    天啊,哪有这样写的,用string.format()不行吗