Adding parameters to IDbCommand

前端 未结 6 642
被撕碎了的回忆
被撕碎了的回忆 2020-12-30 18:43

I am creating a small helper function to return a DataTable. I would like to work across all providers that ADO.Net supports, so I thought about ma

相关标签:
6条回答
  • 2020-12-30 19:06

    I know it's not what you're asking, but I have a much simpler and more robust solution to offer.

    The Microsoft Patterns and Practices library includes a Data Access Application block that is incredibly powerful and easy to use. A sample for executing a stored procedure and returning a dataset is shown below from our actual code:

     object[] ParameterValues = new object[] {"1",DateTime.Now, 12, "Completed", txtNotes.Text};
     Database db = DatabaseFactory.CreateDatabase("ConnectionStringName");
     DataSet ds =  = db.ExecuteDataSet("StoredProcName", ParameterValues);
    

    It doesn't matter if the Connection is OleDb, ODBC, etc. The ConnectionStringName in the first line of code is just the name of the Consternating as defined in the .config file. You pass in a Connection String name, stored proc name, and an array of objects, which make up the parameters. This is just one of the many sweet functions available.

    You'll get everything you're trying to build and then some.

    The official site is here: http://msdn.microsoft.com/en-us/library/ff648951.aspx

    To save you some searching, the Data classes documentation are found here: http://msdn.microsoft.com/en-us/library/microsoft.practices.enterpriselibrary.data(PandP.50).aspx

    (and it's free from Microsoft, and updated regularly.)

    0 讨论(0)
  • 2020-12-30 19:09

    Your Parameters parameter needs to be of type IDataParameter[] and, given the error text, the concrete implementation needs be a SqlParameter[] type.

    If you wish to keep your signature, you'll need a factory to derive the necessary concrete implementation.

    0 讨论(0)
  • 2020-12-30 19:13

    This answer is intended for slightly more specific purpose than what you're doing, but building on @Dismissile's answer, I used a Dictionary to supply the parameter name and value to a foreach loop in my personal project.

    using( IDbCommand dbCommand = dbConnection.CreateCommand() )
    {
        dbCommand.CommandText = Properties.Settings.Default.UpdateCommand;
        Dictionary<string,object> values = new Dictionary<string,object>()
        {
            {"@param1",this.Property1},
            {"@param2",this.Property2},
            // ...
        };
        foreach( var item in values )
        {
            var p = dbCommand.CreateParameter();
            p.ParameterName = item.Key;
            p.Value = item.Value;
            dbCommand.Parameters.Add(p);
        }
    }
    
    0 讨论(0)
  • 2020-12-30 19:17

    You could add the code of the accepted answer to an extension method:

    public static class DbCommandExtensionMethods
    {
        public static void AddParameter (this IDbCommand command, string name, object value)
        {
            var parameter = command.CreateParameter();
            parameter.ParameterName = name;
            parameter.Value = value;
            command.Parameters.Add(parameter);
        }
    }
    
    0 讨论(0)
  • 2020-12-30 19:21

    Add using System.Data.SqlClient; and cmd.Parameters.Add(new SqlParameter("@parameterName", value));

    0 讨论(0)
  • 2020-12-30 19:31

    I believe IDbCommand has a CreateParameter() method:

    var parameter = command.CreateParameter();
    parameter.ParameterName = "@SomeName";
    parameter.Value = 1;
    
    command.Parameters.Add(parameter);
    
    0 讨论(0)
提交回复
热议问题