2013-05-09 40 views
1
string query = "Create table " + name + 
       " (Manpower_Name NVARCHAR(50), Instance INT, Start NVARCHAR(30), End NVARCHAR(30));"; 

我在C#中爲SQL Server CE數據庫(.sdf文件)製作此連接字符串。但是出現如下錯誤:SQL Server CE查詢錯誤(unhandles exception)。從c執行#

解析查詢時出錯。 [令牌行號= 1,令牌行偏移= 77,令牌在錯誤=完]

從功能執行時:

public void create_proj_table(string name) 
{ 
    string query = "Create table " + name + " (Manpower_Name NVARCHAR(50), Instance INT, Start NVARCHAR(30), End NVARCHAR(30));"; 

    //open connection 
    if (this.OpenConnection() == true) 
    { 
     //create command and assign the query and connection from the constructor 
     SqlCeCommand cmd = new SqlCeCommand(query, connection); 

     //Execute command 
     cmd.ExecuteNonQuery(); 

     //close connection 
     this.CloseConnection(); 
    } 
} 
+0

你能告訴我如何你執行這個查詢嗎? – 2013-05-09 15:20:44

+0

嗨,我已添加編輯以顯示它是如何執行的。 – 2013-05-09 15:23:20

+0

謝謝marc_s的編輯。我會更加小心。 – 2013-05-09 15:28:52

回答

1

END是SQL中reserved word

string query = "Create table " + name + " (Manpower_Name NVARCHAR(50), Instance INT, Start NVARCHAR(30), End NVARCHAR(30));"; 

如果你必須使用它 - 即你不想改變該行的名稱 - 用「[]」括起來,就像這樣:

string query = "Create table " + name + " (Manpower_Name NVARCHAR(50), Instance INT, Start NVARCHAR(30), [End] NVARCHAR(30));"; 
+0

謝謝Brian!有效!! – 2013-05-09 15:45:00

+0

非常歡迎您,並感謝您的投票。 – Brian 2013-05-09 16:01:13

2

End是,你不能在自己的表使用一個關鍵字 - 改爲使用類似EndDate的東西。

另外:我會假設StartEnd是日期 - 我強烈建議您使用DATETIME instab NVARCHAR(30)存儲這些!

所以我的CREATE TBALE聲明更改爲類似這樣:

string query = "CREATE TABLE " + name + 
       " (Manpower_Name NVARCHAR(50), Instance INT, StartDate DATETIME, EndDate DATETIME);"; 

,然後你的代碼應該只是罰款。

+1

+1分析START和END字段的真實含義 – Steve 2013-05-09 16:02:26

1

END字是SQL CE中的保留關鍵字。如果你需要使用它,你應該在廣場brakets附上

string query = "Create table " + name + " (Manpower_Name NVARCHAR(50), " + 
       "Instance INT, Start NVARCHAR(30), [End] NVARCHAR(30));";