where条件 string sqlstr = "insert into lkinfo select OutTime from tb where outtime = '" + OutTimeDb + "' and room_id = '" + Room_id + "' and LKName = '" + LkName + "'";
不能误人子弟 insert into A a,b,c,d select a,b,c,d from B where a<=b
我按照这种写法往里套, //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附近有语法错误,不知道是不是哪个地方还是不对呢?
insert into lkinfo outtime ??
SQL啥时候有这样的语法了???你要插入一条数据还得限制到哪条,那么你这是更新"UPDATE lkinfo SET outtime='" + OutTime + "' where outtime = '" + OutTimeDb + "' and room_id = '" + Room_id + "' and LKName = '" + LkName + "'";
看了语句 没发现错误
看了下面 的回复 才发现是 insert 开始还觉得是 update
string sqlstr = "insert into lkinfo select OutTime from tb where outtime = '" + OutTimeDb + "' and room_id = '" + Room_id + "' and LKName = '" + LkName + "'";
不能误人子弟
insert into A
a,b,c,d
select a,b,c,d
from B
where a<=b
我按照这种写法往里套,
//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附近有语法错误,不知道是不是哪个地方还是不对呢?
where outtime = '" + OutTimeDb + "' and room_id = '" + Room_id + "' and LKName = '" + LkName + "'";
代码如下: 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);
}
}