简体   繁体   English

SQL Server CE查询错误(unhandles异常)。 从c#执行

[英]SQL Server CE query error (unhandles exception). Executed from c#

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

I make of this connection string in C# for a SQL Server CE database (a .sdf file). 我在C#中为SQL Server CE数据库( .sdf文件)创建了这个连接字符串。 But I am getting an error, as follows: 但我收到一个错误,如下:

There was an error parsing the query. 解析查询时出错。 [ Token line number = 1,Token line offset = 77,Token in error = End ] [令牌行号= 1,令牌行偏移= 77,令牌错误=结束]

Executed from the function: 执行功能:

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();
    }
}

End is a keyword that you cannot use in your own tables - use something like EndDate instead. End是一个您不能在自己的表中使用的关键字 - 请使用类似EndDate

Also: I would assume Start and End are dates - I would strongly urge you to use DATETIME instad of NVARCHAR(30) for storing those! 另外:我会假设StartEnd的日期-我会强烈建议您使用DATETIME的instad NVARCHAR(30)用于存储那些!

So I'd change the CREATE TBALE statement to something like this: 所以我将CREATE TBALE语句改为这样的:

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

and then your code should work just fine. 然后你的代码应该工作得很好。

The word END is a reserved keyword in SQL CE. 单词END是SQL CE中的保留关键字。 If you need to use it you should enclose in square brakets 如果您需要使用它,您应该用方形托架包围

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

END is a reserved word in Sql. END是Sql中的保留字

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

If you must use it - ie, you don't want to change the name of the row - enclose it with '[]', like this: 如果你必须使用它 - 也就是说,你不想改变行的名称 - 用'[]'括起来,如下所示:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM