gpt4 book ai didi

mysql - 系统.Data.SqlClient.SqlException : Incorrect syntax near the keyword 'Table'

转载 作者:行者123 更新时间:2023-11-29 11:36:55 24 4
gpt4 key购买 nike

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

System.Data.sqlclient.sqlexception:Incorrect syntax near the keyword 'Table'

这是代码部分。

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)
{
}
}

最佳答案

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

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

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

关于mysql - 系统.Data.SqlClient.SqlException : Incorrect syntax near the keyword 'Table' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36425671/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com