dbcommand.parameters中缺少addwithvalue [英] Missing addwithvalue from dbcommand.parameters

查看:269
本文介绍了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);
        }
    }

来自 http://social.msdn.microsoft .com / Forums / zh-CN / d56a4710-3fd1-4039-a0d9-c4c6bd1cd22e / dbcommand-parameters-collection-missing-addwithvalue-method

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

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆