简体   繁体   English

如何使用多客户端支持的ADO.Net参数

[英]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. 我正在构建一个ado.net包装器,可以连接任何数据库(由ADO.Net支持),基本上来自SQl,Oledb,Odbc,Oracle,Sqlite等。我实现了连接和所有其他基本的东西。 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. 我知道为此我们使用IDataParameterCollection或IDbDataParameters等。但不知道实现的方式。

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. 简而言之,我需要一种独立的方式来传递将在所有类型的客户端中使用的参数,无论是SqlClient,OracleClient还是任何其他客户端。

Thanks!!! 谢谢!!!

(cmd is the DbCommand instance) (cmd是DbCommand实例)

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 = ? 然后,查询就像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);
      }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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