[Mono-list] Parameters & SQLite
Adam Tauno Williams
adam at morrison-ind.com
Thu Aug 31 06:45:50 EDT 2006
> It is my blog, but take a look anyway.
> http://little.xmtp.net/blog/2005/10/21/proper-data-abstraction-in-net-and-mono-applications/
> http://little.xmtp.net/blog/2005/11/22/more-proper-data-abstractions-in-net-and-mono-applications/
> The short answer is "yes" parameters are supported. There are examples
> at the above URLs.
> A short example:
> |IDbCommand idcSelect = conn.CreateCommand();
> idcSelect.CommandText = "select col from table where othercl=@someting";
> ||IDataParameter param = idcSelect.CreateParameter();
> Cmd.Parameters.Add(param);
> param.ParameterName = "@someting";
> param.DbType = DbType.String;
> param.Value = "whatever something would equal";
> The above is more geared to 1.1, since 2.0 has DbConnection and the
> ADO2.0 db provider stuff.
Excellent!
I'm trying to create a routine that automatically stores a Hashtable (a
result from an expensive RPC) into a database as a cache. Each key in
the Hashtable corresponds to a column in the table, which the key
entityName containing the name of the table. The routine compiles fine
but then just hangs when ExecuteNonQuery is called.
------
IDbCommand command = connection.CreateCommand();
int count = 0;
string columnList = "";
string valueList = "";
foreach(string key in dictionary.Keys)
{
IDataParameter parameter;
if (count > 0)
{
columnList = columnList + ", ";
valueList = valueList + ", ";
}
columnList = columnList + key;
valueList = valueList + "@" + key;
parameter = command.CreateParameter();
command.Parameters.Add(parameter);
if (dictionary[key].GetType().FullName == "System.String")
{
parameter.ParameterName = "@" + key;
parameter.DbType = DbType.String;
parameter.Value = (string)dictionary[key];
} else if (dictionary[key].GetType().FullName == "System.Int32")
{
parameter.ParameterName = "@" + key;
parameter.DbType = DbType.Int32;
parameter.Value = (int)dictionary[key];
} else if (dictionary[key].GetType().FullName == "System.DateTime")
{
parameter.ParameterName = "@" + key;
parameter.DbType = DbType.DateTime;
parameter.Value = (DateTime)dictionary[key];
} else throw new System.Exception("Unhandled type in response");
count++;
}
command.CommandText =
"INSERT INTO " + (string)dictionary["entityName"] +
" (" + columnList + ") VALUES (" + valueList + ");";
Console.WriteLine("Dictionary.StoreHash: Executing non-query");
command.ExecuteNonQuery();
Console.WriteLine("Dictionary.StoreHash: Non-query complete");
command.Dispose();
----
I'd expect an error/exception, the hang is confusing.
More information about the Mono-list
mailing list