【问题标题】:System.Data.SqlClient.SqlException: Incorrect syntax near the keyword 'Table'System.Data.SqlClient.SqlException:关键字“表”附近的语法不正确
【发布时间】:2016-04-05 11:53:16
【问题描述】:

这是我的Registration.aspx 文件的编码。每当我点击提交时,我都会得到 ​​p>

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 Registration : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (IsPostBack)
        {
            SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["RegistrationConnectionString"].ConnectionString);
            conn.Open();
            // This is the part im getting error in.
            string checkuser = "select count(*) from Table where UserName ='" + uname.Text + "'";
            SqlCommand com = new SqlCommand(checkuser, conn);
            int temp = Convert.ToInt32(com.ExecuteScalar().ToString());
            if (temp == 1)
            {
                Response.Write("User Already Exists");
            }

            conn.Close();
        }
    }

    protected void Button1_Click(object sender, EventArgs e)
    {
        try
        {
            SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["RegistrationConnectionString"].ConnectionString);
            conn.Open();
            string insertQuery = "insert into Table (UserName,Email,Password,Country) values (@uname ,@email ,@pass ,@country)";
            SqlCommand com = new SqlCommand(insertQuery, conn);
            com.Parameters.AddWithValue("@uname", uname.Text);
            com.Parameters.AddWithValue("@email", email.Text);
            com.Parameters.AddWithValue("@pass", pwd.Text);
            com.Parameters.AddWithValue("@country", DropDownList1.SelectedItem.ToString());
            com.ExecuteNonQuery();
            Response.Redirect("Manager.aspx");
            Response.Write("Registration Successful");
            conn.Close();
        }
        catch (Exception ex)
        {
            Response.Write("Error:" + ex.ToString());
        }
    }

    protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
    {
    }
}

【问题讨论】:

  • 这个问题与jquery和visual studio无关。请注意适当地标记您的问题。
  • 你确定你的表名是Table
  • 是的@KartikeyaKhosla
  • MySql 也没有

标签: mysql asp.net vb.net


【解决方案1】:

Table 是保留字,请在这两个查询中使用括号:

"select count(*) from [Table] where UserName ='" + uname.Text + "'";

"insert into [Table] (UserName,Email,Password,Country) values (@uname ,@email ,@pass ,@country)";

【讨论】:

  • 不是你的错,但这不是 MySql (SqlConnection)
【解决方案2】:

如下更改您的查询,

string insertQuery = "insert into [Table] (`UserName`,`Email`,`Password`,`Country`) values (@uname ,@email ,@pass ,@country)";

【讨论】:

    【解决方案3】:

    表格是关键字,所以我不能在这种情况下使用它,所以请确保您的 sql 查询是正确的

    而mysql不使用

    using System.Data.SqlClient;
    

    【讨论】:

      猜你喜欢
      • 2015-10-10
      • 2021-07-24
      • 2021-11-22
      • 1970-01-01
      • 2016-01-16
      • 1970-01-01
      • 1970-01-01
      • 2021-03-31
      相关资源
      最近更新 更多