发生异常。关键字 'Table' 附近的语法不正确
An exception occurred .. Incorrect syntax near the keyword 'Table'
public partial class Login : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
string connect_str = @"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=C:\Users\Chamod\Documents\Chamod.mdf;Integrated Security=True";
SqlCommand cmd = new SqlCommand();
string ms_sql_str = "SELECT * FROM Table ";
cmd.CommandText = ms_sql_str;
SqlConnection connection = new SqlConnection(connect_str);
cmd.Connection = connection;
connection.Open();
SqlDataReader dr = cmd.ExecuteReader();
if(dr.HasRows)
{
Session["Admin"] = t1.Text.Trim();
Response.Redirect("First.aspx");
}
else
{
// spnmsg.Visible = true;
}
connection.Close();
}
}
我正在为酒店创建登录系统。当我要登录系统时,
VS 2015 指向 SqlDataReader dr = cmd.ExecuteReader();
行。
意思是
"An unhandled exception occurred during the execution of the current
web request."
我有很多错误,我不知道如何完成代码。请给我一个解决方案。
TABLE
是一个 reserved 关键字,因此您应该更改它的名称,或者您应该将它括在方括号中,例如 [Table]
:
string ms_sql_str = "SELECT * FROM [Table] ";
但是在 MySQL 中我认为你应该使用反引号 (``
):
string ms_sql_str = "SELECT * FROM `Table` ";
public partial class Login : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
string connect_str = @"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=C:\Users\Chamod\Documents\Chamod.mdf;Integrated Security=True";
SqlCommand cmd = new SqlCommand();
string ms_sql_str = "SELECT * FROM Table ";
cmd.CommandText = ms_sql_str;
SqlConnection connection = new SqlConnection(connect_str);
cmd.Connection = connection;
connection.Open();
SqlDataReader dr = cmd.ExecuteReader();
if(dr.HasRows)
{
Session["Admin"] = t1.Text.Trim();
Response.Redirect("First.aspx");
}
else
{
// spnmsg.Visible = true;
}
connection.Close();
}
}
我正在为酒店创建登录系统。当我要登录系统时,
VS 2015 指向 SqlDataReader dr = cmd.ExecuteReader();
行。
意思是
"An unhandled exception occurred during the execution of the current web request."
我有很多错误,我不知道如何完成代码。请给我一个解决方案。
TABLE
是一个 reserved 关键字,因此您应该更改它的名称,或者您应该将它括在方括号中,例如 [Table]
:
string ms_sql_str = "SELECT * FROM [Table] ";
但是在 MySQL 中我认为你应该使用反引号 (``
):
string ms_sql_str = "SELECT * FROM `Table` ";