我正在尝试以实用方式创建表,我的代码生成以下语法:
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
如果有所作为,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;
}
答案 0 :(得分:7)
您没有关闭(
string x = "CREATE TABLE [test] ([ID] int NOT NULL,[Name] nvarchar(100) NOT NULL)"
答案 1 :(得分:5)
您的查询缺失)
"CREATE TABLE [test] ([ID] int NOT NULL,[Name] nvarchar(100) NOT NULL)"