SqlCeCommand执行C#

hay*_*sam 0 c# sql sql-server-ce

我正在尝试以实用方式创建表,我的代码生成以下语法:

string x = "CREATE TABLE [test] ([ID] int NOT NULL,[Name] nvarchar(100) NOT NULL"
Run Code Online (Sandbox Code Playgroud)

但每次我尝试运行代码时,我都会生成以下异常.

我的代码:

   SQLCommand cmd = new SQLCommand(x,conn);
   cmd.ExecuteNonQuery();
Run Code Online (Sandbox Code Playgroud)

我得到的例外:

解析查询时出错.[令牌行号= 1,令牌行偏移= 66,令牌错误= NULL]

我正在使用,SqlCeConnection如果这有所作为,并SqlCeCommand执行.

我是否错误地创建了表格?

我的后端连接和执行如下所示:

public bool ExecuteSqlCommand(string sqlStr, IList<SqlCeParameter> parameters, SqlCeConnection conn)
{
   SqlCeCommand cmd = null;
   cmd = conn.CreateCommand();

   try
   {
      if (parameters != null)
      {
         foreach (SqlCeParameter param in parameters)
         {
             cmd.Parameters.Add(param);
         }
      }

      cmd.CommandText = sqlStr;

      int affectedRows = cmd.ExecuteNonQuery();

      bool DDLCommand = sqlStr.StartsWith("Drop ", StringComparison.OrdinalIgnoreCase);
      DDLCommand = DDLCommand || sqlStr.StartsWith("Delete ", StringComparison.OrdinalIgnoreCase);
      DDLCommand = DDLCommand || sqlStr.StartsWith("Insert ", StringComparison.OrdinalIgnoreCase);
      DDLCommand = DDLCommand || sqlStr.StartsWith("Update ", StringComparison.OrdinalIgnoreCase);
      DDLCommand = DDLCommand || sqlStr.StartsWith("Create ", StringComparison.OrdinalIgnoreCase);
      DDLCommand = DDLCommand || sqlStr.StartsWith("Alter ", StringComparison.OrdinalIgnoreCase);

      if (DDLCommand || affectedRows > 0)
         return true;
      else
         return false;
   }
   catch (Exception e)
   {
       SqlCeException sqlEx = e as SqlCeException;
   }

   if (cmd != null)
       cmd.Dispose();

   return false;
}
Run Code Online (Sandbox Code Playgroud)

Cla*_*edi 7

你没有关闭你的 (

string x = "CREATE TABLE [test] ([ID] int NOT NULL,[Name] nvarchar(100) NOT NULL)"
Run Code Online (Sandbox Code Playgroud)


gza*_*axx 5

您的查询丢失了 )

"CREATE TABLE [test] ([ID] int NOT NULL,[Name] nvarchar(100) NOT NULL)"
Run Code Online (Sandbox Code Playgroud)