I am trying to create a table pragmatically, my code generates the following syntax:
string x = "CREATE TABLE [test] ([ID] int NOT NULL,[Name] nvarchar(100) NOT NULL"
But every time I try to run the code, I generate the following exception.
My code:
SQLCommand cmd = new SQLCommand(x,conn);
cmd.ExecuteNonQuery();
The exception I get:
There was an error parsing the query. [ Token line number = 1,Token line offset = 66,Token in error = NULL ]
I am using SqlCeConnection
if that makes a difference, and SqlCeCommand
for execution.
Am I creating the table incorrectly?
My backend connection and execution looks like this :
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;
}
You're not closing your (
string x = "CREATE TABLE [test] ([ID] int NOT NULL,[Name] nvarchar(100) NOT NULL)"