c# - SQL Table as parameter with IDBCommand -


i experimenting how allow web application support multiple databases, sql server , mysql.

i've setup sql helper class take care of connecting database , query execution. i'm using idbcommand, idbconnection, etc. i've tested class console application , works fine queries without parameters.

but when add in parameters, that's problem starts.

i wish use parameters table names because current table names might change , pain rename tables in queries.

i've found article author shared methods adding parameters idbcommand. can't find link article here's sample of i've gotten article (thanks author btw! :d) able confirm parameters added because listed under idbcommand using quickwatch getting "must declare table variable "@mytable"." error.

    internal static int addinputparameter<t>(this idbcommand cmd,     string name, t value)     {         var p = cmd.createparameter();         p.parametername = name;         p.value = value;         return cmd.parameters.add(p);     } 

any idea on doing wrong?

edit:
in response bew's reply, able using both sqlcommand , mysqlcommand.

edit2:
looks i've remembered wrong. used string.format() table names in past project.

the short answer cannot pass table parameter. instead, query want determine table @ runtime must assembled string before used in parametrized query. longer answer requires understanding happens when call cmd.executereader parametrized dynamic sql statement. in scenario, .net classes build call using sp_executesql. example:

declare @sql nvarchar(max); set @sql = 'select count(*) sys.objects object_id > @somenum'; exec sp_executesql @sql, n'@somenum int', 10000; 

now suppose try insert parameter our table sql statement. resulting statement be:

declare @sql nvarchar(max); set @sql = 'select count(*) @sometable object_id > @somenum'; exec sp_executesql @sql, n'@sometable nvarchar(256),@somenum int', 'sys.objects', 10000; 

this result in error along lines of must declare table variable "@sometable". sql server doesn't know @sometable parameter , not table variable. thus, object references (tables, views, procedures etc.) not can parametrize; criteria can parametrized.


Comments

Popular posts from this blog

assembly - 8086 TASM: Illegal Indexing Mode -

Java, LWJGL, OpenGL 1.1, decoding BufferedImage to Bytebuffer and binding to OpenGL across classes -

javascript - addthis share facebook and google+ url -