询问一个关于sql语句语法的问题

huangweia0 2010-07-29 09:53:56

string sqlstr = "insert into lkinfo (outtime) values('" + OutTime + "') where outtime = '" + OutTimeDb + "' and room_id = '" + Room_id + "' and LKName = '" + LkName + "'";


这句代码有什么错误吗?调试的时候总是提示where附近有语法错误。
...全文
165 19 打赏 收藏 转发到动态 举报
写回复
用AI写文章
19 条回复
切换为时间正序
请发表友善的回复…
发表回复
周煜皓 2010-07-30
  • 打赏
  • 举报
回复
插入语句 怎么会有条件呢???
a13872321228 2010-07-30
  • 打赏
  • 举报
回复
天啊,哪有这样写的,用string.format()不行吗
huangweia0 2010-07-30
  • 打赏
  • 举报
回复
我终于可以安稳的睡觉了。
huangweia0 2010-07-30
  • 打赏
  • 举报
回复
哈哈哈......终于搞定了,散分散分。
哈哈哈......终于搞定了我十来天以来都没有搞定的一个问题。
高兴呐......伟大的意大利的左后卫,介个是我的生日,这是一份最好的生日礼物。
huangweia0 2010-07-29
  • 打赏
  • 举报
回复
是的,后来我也发现不能用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);
}
}
SQL77 2010-07-29
  • 打赏
  • 举报
回复
[Quote=引用楼主 huangweia0 的回复:]
C# code

string sqlstr = "insert into lkinfo (outtime) values('" + OutTime + "') where outtime = '" + OutTimeDb + "' and room_id = '" + Room_id + "' and LKName = '" + LkName + "'";



这句代码有什么错误……
[/Quote]
SQL啥时候有这样的语法了???

你要插入一条数据还得限制到哪条,那么你这是更新

"UPDATE lkinfo SET outtime='" + OutTime + "'
where outtime = '" + OutTimeDb + "' and room_id = '" + Room_id + "' and LKName = '" + LkName + "'";
mayonglong 2010-07-29
  • 打赏
  • 举报
回复
insert into lkinfo outtime ??
huangweia0 2010-07-29
  • 打赏
  • 举报
回复
[Quote=引用 11 楼 mayonglong 的回复:]
引用 5 楼 mayonglong 的回复:
后面加where做啥呢,insert不需要~


不能误人子弟


SQL code

insert into A
a,b,c,d
select a,b,c,d
from B
where a<=b
[/Quote]

我按照这种写法往里套,


//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附近有语法错误,不知道是不是哪个地方还是不对呢?
mayonglong 2010-07-29
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 mayonglong 的回复:]
后面加where做啥呢,insert不需要~
[/Quote]

不能误人子弟


insert into A
a,b,c,d
select a,b,c,d
from B
where a<=b
wuyq11 2010-07-29
  • 打赏
  • 举报
回复
where条件
string sqlstr = "insert into lkinfo select OutTime from tb where outtime = '" + OutTimeDb + "' and room_id = '" + Room_id + "' and LKName = '" + LkName + "'";
lovexiongnan 2010-07-29
  • 打赏
  • 举报
回复
上面的说的对~~~
ocean202715 2010-07-29
  • 打赏
  • 举报
回复
后面加where做啥呢,insert不需要~
行者_ 2010-07-29
  • 打赏
  • 举报
回复
第一下没反应过来
看了语句 没发现错误
看了下面 的回复 才发现是 insert 开始还觉得是 update
pc_242 2010-07-29
  • 打赏
  • 举报
回复
上面都说了
mayonglong 2010-07-29
  • 打赏
  • 举报
回复
后面加where做啥呢,insert不需要~
bapeiaw 2010-07-29
  • 打赏
  • 举报
回复
插入语句也要条件,那这个条件用来干麻的,还要和数据比一下??
caorenlong 2010-07-29
  • 打赏
  • 举报
回复
把where 条件去掉
huangweia0 2010-07-29
  • 打赏
  • 举报
回复
也就是说后面不能跟where 条件是吧?
wuyq11 2010-07-29
  • 打赏
  • 举报
回复
string sqlstr = "insert into lkinfo(outtime) values('" + OutTime + "')";

110,545

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧