我总是会出现这样的错误,不知道哪里出了问题?在关键字 "user "附近有语法错误
异常详细信息:System.Data.SqlClient.SqlException:在关键字 "user "附近有语法错误
private void DBOpen()
{
strConnectionString = System.Configuration.ConfigurationSettings.AppSettings[ "ConnectionString "];
conn = new SqlConnection(strConnectionString);
conn.Open();
}
public void DataGridBind()
{
string strsql= "select user_id,username,sex,hometown,mobilephone,xueli,dep_name,zhiwu from user,dep where user.dep_id=dep.dep_id ";
ds = new DataSet();
DBOpen();
da =new SqlDataAdapter(strsql,conn);
da.Fill(ds, "User "); //出错!!!
DataView dv = ds.Tables[0].DefaultView;
show_person.DataSource = dv;
show_person.DataBind();
conn.Close();
}
Web.config中代码:
<appSettings>
<!--
设置数据库连接字符串配置
-->
<add key= "ConnectionString " value= "server=MY-TOMATO;database=manage;uid=administrator;pwd=123 " />
</appSettings>
------解决方案--------------------------------------------------------
from user ===> from [user]
user 是关键字,为了避免歧要加[]
------解决方案--------------------------------------------------------
user是SQL Server的关键字,类似这样的东东要加[]
string strsql= "select user_id,username,sex,hometown,mobilephone,xueli,dep_name,zhiwu from user,dep where user.dep_id=dep.dep_id ";
=============================================
string strsql= "select user_id,username,sex,hometown,mobilephone,xueli,dep_name,zhiwu from [user],dep where [user].dep_id=dep.dep_id ";
------解决方案--------------------------------------------------------
[user_id] and [user] 加就可以了