简体   繁体   中英

How to use multi client supported ADO.Net parameter

I am building a ado.net wrapper that can connect with any database(supported by ADO.Net)basically from SQl, Oledb,Odbc,Oracle,Sqlite etc. I implemented the connection and all other basic things. But at a point where my queries need parameter to pass from other levels , i am stucked there. I know for that purpose we use IDataParameterCollection or IDbDataParameters etc. But do not know the way to implement.

So you guys are requested to please help me out. It would be a great help.

OR

You can say in short, i need an independent way of passing parameters that will be used in all type of clients, whether it is SqlClient, OracleClient or any other client.

Thanks!!!

(cmd is the DbCommand instance)

DbParameter p = cmd.CreateParameter();
p.Direction = ParameterDirection.Input;
p.Value = the_value;
p.ParameterName = the_param_name;
p.DbType = ...

cmd.Parameters.Add(p);

Then, queries are like select * from table where field = ? . But, be careful with the order or creating parameters.

The ? joker should be independent.

Ok. On the user side :

SqlParameter s1 = new SqlParameter();
s1.Direction = ParameterDirection.Input;
s1.ParameterName = "s1";

SqlParameter s2 = new SqlParameter();
s2.Direction = ParameterDirection.Input;
s2.ParameterName = "s2";

this.ExecuteScalar("query", new IDataParameter[] { s1, s2 });

And on the other side :

public int ExecuteScalar(string commandText, IDataParameter[] param)
      {
         IDbCommand cmd = connection.CreateCommand();
         cmd.CommandText = commandText;
         foreach (IDataParameter p in param)
            cmd.Parameters.Add(p);
      }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM