本文介绍了dbcommand.parameters中缺少addwithvalue的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在构建一个同时处理MSsql和Sqlite的sqlwrapper,到目前为止,使用通用的dbconnection,dataset和dataadapter都没有问题,但是使用dbcommand参数。add是唯一的选择。我想像sqlcommand和sqlitecommand一样使用parameter.addwithvalue,但是我不确定如何实现它。

I am building an sqlwrapper to handle both MSsql and Sqlite, I have no issues using the generic dbconnection, dataset and dataadapter so far, but with dbcommand parameters.add is the only option. I'd like to use parameter.addwithvalue like sqlcommand and sqlitecommand have, but I am unsure how to implement it.

例如,这是我的sqlitefactory:

For example here is my sqlitefactory:

public class SqlLiteFactory : SqlFactory
{
    public override DbConnection CreateConnection()
    {
        return new SQLiteConnection(ConfigurationManager.ConnectionStrings["ClientDBConnectionString"].ConnectionString);
        //return new SQLiteConnection("Data Source=Resources\\qaqc.sqlite;Version=3");
    }

    public override DataAdapter CreateAdapter(string command, DbConnection connection)
    {
        return new SQLiteDataAdapter(command, connection as SQLiteConnection);
    }

    public override DbCommand CreateCommand()
    {
        return new SQLiteCommand();
    }
}

这是我的实现:

DbConnection c = SqlHandler.Instance.CreateConnection();

DbCommand cmd = SqlHandler.Instance.CreateCommand();

cmd.Connection = c;
cmd.CommandText = "SELECT column FROM table WHERE syncstatus = 0 and UserName = @user GROUP BY column;";
cmd.Parameters.Add("@user");
cmd.Parameters["@user"].Value = SystemInformation.UserName;
DbDataReader dr = cmd.ExecuteReader();

在更具体的sqlcommand和sqlitecommand中,有一种方法.addwithvalue()就是我想要的

in the more specific sqlcommand and sqlitecommand there is a method .addwithvalue() which is what I'd like to have in the parent dbcommand class.

推荐答案

使用扩展方法解决了该问题

Solved it with and extension method

static class MyExtensions
    {
        public static void AddParameterWithValue(this DbCommand command, string parameterName, object parameterValue)
        {
            var parameter = command.CreateParameter();
            parameter.ParameterName = parameterName;
            parameter.Value = parameterValue;
            command.Parameters.Add(parameter);
        }
    }

来自

这篇关于dbcommand.parameters中缺少addwithvalue的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-22 23:29