System.Data.dll中出现类型'System.Data.SqlClient.SqlException'的异常但未在用户代码中处理附加信息:关键字'Table'附近的语法不正确。 [英] An exception of type 'System.Data.SqlClient.SqlException' occurred in System.Data.dll but was not handled in user code Additional information: Incorrect syntax near the keyword 'Table'.

查看:131
本文介绍了System.Data.dll中出现类型'System.Data.SqlClient.SqlException'的异常但未在用户代码中处理附加信息:关键字'Table'附近的语法不正确。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

System.Data.dll中出现'System.Data.SqlClient.SqlException'类型的异常,但未在用户代码中处理



其他信息:不正确关键字'Table'附近的语法。



这里是注册按钮代码。

An exception of type 'System.Data.SqlClient.SqlException' occurred in System.Data.dll but was not handled in user code

Additional information: Incorrect syntax near the keyword 'Table'.

here is the Registration Button Code.

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();
            string checkuser = "select count(*) from Table where Username='" + tbUsername.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,@Passowrd,@Country)";
            SqlCommand com = new SqlCommand(insertQuery, conn);
            com.Parameters.AddWithValue("@Uname",tbUsername.Text);
            com.Parameters.AddWithValue("@Email",tbEmail.Text);
            com.Parameters.AddWithValue("@Password",tbPassword.Text);
            com.Parameters.AddWithValue("@Country", ddlCountry.SelectedItem.ToString());

            com.ExecuteNonQuery();
            Response.Write("Hooray!! Registration Succesful");
        
            conn.Close();
        }
        catch(Exception ex)
        {
            Response.Write("Oops, Something Went Wrong!!" + ex.ToString());
            
        }
    }
}



请帮助!!

我,ma bigner


Please help!!
I,m a bigner

推荐答案

试试这个:



Try this:

string checkuser = "select count(*) from [Table] where Username='" + tbUsername.Text + "'";
//Here "Table"  is a predefined word in SQL Sever. You should use it like [Table].
//I hope [Table] is your table name





同样在Button1_Click事件中修改:



Also modify in Button1_Click Event:

string insertQuery = "insert into [Table] (Username,Email,Password,Country) values(@Uname,@Email,@Passowrd,@Country)";





而且我认为你应该这样做:我是怎么做的编写SQL,第1部分:命名约定 [ ^ ]



- Amy


这篇关于System.Data.dll中出现类型'System.Data.SqlClient.SqlException'的异常但未在用户代码中处理附加信息:关键字'Table'附近的语法不正确。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆