2015-11-04 36 views
-2

我正在建立一個網站使用Visual Studio 2013,我堅持讓用戶註冊,一旦提交細節存儲在數據庫中,經理可以登錄並查看所有註冊的帳戶。但我越來越視覺工作室2013獲得關鍵字'表'附近的錯誤語法

Error:System.Data.SqlClient.SqlException (0x80131904): Incorrect syntax near the keyword 'Table'. at System.Data.SqlClient.SqlConnection. 

BTW表是我存儲註冊帳戶數據的數據庫表的名稱。

任何幫助被佔用!

這是提交按鈕

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; 
using System.Configuration; 

public partial class Registration : 
{ 
    protected void Page_Load(object sender, EventArgs e) 
    { 
     if(IsPostBack) 
     { 
      try 
      { 
       SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString-reg"].ConnectionString); //connects to regstration DB// 
       conn.Open(); //to open connection to DB 
       String checkuser = "select count(*) from Table where Name= '"+ nameBox.Text +"'"; // checks wether the name entered is in the database already // 
       SqlCommand com = new SqlCommand(checkuser, conn); 
       int temp = Convert.ToInt32(com.ExecuteScalar().ToString()); 
       if (temp == 1) 
       { 
        Response.Write("User already exists"); 
       } 
       conn.Close(); //to close connection to DB after executing a query// 
      } 
      catch (SqlException Ex) 
      { 
       Response.Write("Error:" + Ex.ToString()); 
      } 
     } 
    } 

    protected void Button1_Click(object sender, EventArgs e) 
    { 
     try 
     { 
      SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString-reg"].ConnectionString); //connects to regstration DB// 
      conn.Open(); //to open connection to DB 
      String insertQuery = "insert into Table (Name, Date of birth, Yoga experience, Health issues, Email address, Telephone number, Password) values (@Name, @dob, @exp, @health, @email, @phone, @password)"; // inserts into table and declares data as variables// 
      SqlCommand com = new SqlCommand(insertQuery, conn); 
      com.Parameters.AddWithValue("@Name", nameBox.Text); 
      com.Parameters.AddWithValue("@dob", Yeardropdown.SelectedItem.ToString()); 
      com.Parameters.AddWithValue("@expe", expBox.Text); 
      com.Parameters.AddWithValue("@health", healthBox.Text); 
      com.Parameters.AddWithValue("@email", emailBox.Text); 
      com.Parameters.AddWithValue("@phone", phoneBox.Text); 
      com.Parameters.AddWithValue("@passwrd", passwrdBox.Text); 

      com.ExecuteNonQuery(); 
      Response.Redirect("Manager.aspx"); 
      Response.Write("Thank you, Your registration has been successfull!"); 
      conn.Close(); 
     } 
     catch (SqlException ex) 
     { 
      Response.Write("Error:" + ex.ToString()); 
     } 
    } 

    protected void Daydropdown_SelectedIndexChanged(object sender, EventArgs e) 
    { 

    } 
} 

回答

0

另一件事的代碼就是爲什麼你需要檢查IsPostBack

第一個TableSQL將是一個Table語法。
如果必須使用的語法名稱....

的Microsoft SQL Server
修改您的SQL查詢從table[table]

應該工作..

有你在SQL Management Studio中運行你的代碼?
我相信,如果你嘗試insertselect使用查詢

語法名稱必須的添加[]

+0

我是相當新的,但我敢肯定,因爲我已經按照沒有失誤的教程,不知道爲什麼會這樣:( –

+0

@ MoAl-azzaniGawi你試過它應該工作的補充[ ]? – Nic

0

我們不能在SQLSERVER使用表作爲表名, 改變你會打你的錯誤表名,它會起作用,不要忘記在查詢中也改變你的表名。

String insertQuery = "insert into changethistablename (Name, Date of birth, Yoga experience, Health issues, Email address, Telephone number, Password) values (@Name, @dob, @exp, @health, @email, @phone, @password)"; // inserts into table and declares data as variables// 
String checkuser = "select count(*) from changethistablename where Name= '"+ nameBox.Text +"'"; // checks wether the name entered is in the database already // 
1

兩件事情:

1)您使用表作爲表名。由於它已經是SQL中的關鍵字,所以請更改此名稱。雖然您可以使用以下命令:

Insert into [Table].... 

2)您的列名稱包含空格,所以它可能會導致一些錯誤。如果列名包含空格,請在列名周圍使用括號。

String insertQuery = "insert into Table (Name, [Date of birth], [Yoga experience], [Health issues], [Email address], [Telephone number], Password) values (@Name, @dob, @exp, @health, @email, @phone, @password)"; 
相關問題