我是ASP.net的新手,创建了一个基本的登录页面。我在尝试编译时遇到以下错误:
“System.Data.dll中发生'System.Data.SqlClient.SqlException'类型的异常,但未在用户代码中处理
其他信息:关键字“表格”附近的语法不正确。“
以下是我的代码,有人可以解释一下我在这里做错了吗?
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Configuration;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (IsPostBack) {
SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString);
conn.Open();
string checkuser = "select count(*) from Table where username = '" + username.Text + "' ";
SqlCommand com = new SqlCommand(checkuser, conn);
int temp = Convert.ToInt32(com.ExecuteScalar().ToString());
if (temp == 1)
{
Response.Write("Username Already Exist");
}
conn.Close();
}
}
protected void Button2_Click(object sender, EventArgs e)
{
try
{
SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString);
conn.Open();
string insertQuery = "insert into Table (username, email, password) values (@username, @email, @password)";
SqlCommand com = new SqlCommand(insertQuery, conn);
com.Parameters.AddWithValue("@username", username.Text);
com.Parameters.AddWithValue("@email", emailID.Text);
com.Parameters.AddWithValue("@password", passwrd.Text);
com.ExecuteNonQuery();
Response.Redirect("default3.aspx");
Response.Write("Your registration is successful");
conn.Close();
}
catch (Exception ex)
{
Response.Write("Error:" + ex.ToString());
}
Response.Write("Your form has been submitted");
}
}
答案 0 :(得分:0)
Table
是T-SQL中的reserved word(出于相当明显的原因)。快速解决方法是明确地附上标识符:
select count(*) from [Table] where ...
更合适的解决方法是为表提供更有意义的名称。汽车公司没有为其车辆命名Car
。它很容易产生混淆。