请大家看一下这个程序啊
SqlConnection mycon = new SqlConnection();
mycon.ConnectionString = "server=COMPUTER;Integrated Security=True;database=Message ";
mycon.Open();
string sql, username,title,content;
username=tbxusername.Text.Trim ();
title=tbxmsitle.Text.Trim ();
content=tbxmcontent.Text.Trim ();
if(title!= " "||username != " "||content != " ")
{
sql= "insert into Message(MUname,MsTitle,MsContent)values( "+username+ ", "+title+ ", "+content+ ") ";
SqlCommand sqlcmd=new SqlCommand (sql,mycon);
sqlcmd.ExecuteNonQuery();
mycon .Close ();
Label4.Text= "发表留言成功 ";
}
else
{
Label4 .Text= "请输入完整信息 ";
}
运行的结果是
错误的位置在sqlcmd.ExecuteNonQuery();
在此上下文中不允许使用 'sfjosf '。此处只允许使用常量、表达式或变量。不允许使用列名。
------解决方案--------------------sql= "insert into [Message]([MUname],[MsTitle],[MsContent]) values( ' "+username+ " ', ' "+title+ " ', ' "+content+ " ') ";
------解决方案--------------------sql= "insert into Message(MUname,MsTitle,MsContent)values( ' "+username+ " ', ' "+title+ " ', ' "+content+ " ') ";