我正在尝试务实地创建一个表,我的代码生成以下语法:
string x = "CREATE TABLE [test] ([ID] int NOT NULL,[Name] nvarchar(100) NOT NULL"
但是每次我尝试运行代码时,都会生成以下异常。
我的代码:
SQLCommand cmd = new SQLCommand(x,conn);
cmd.ExecuteNonQuery();
我得到的例外:
解析查询时出错。[令牌行号= 1,令牌行偏移量= 66,错误令牌= NULL]
我正在使用SqlCeConnection
if 这会有所作为,并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;
}