Avatar of CyberUnDead
CyberUnDead

asked on 

DbProviderFactory Command Parameters

I am connecting to an Access MDE database but I will need to support both OLEDB and ODBC.

I have a function that returns an individual's user name against the database from the supplied individual's ID.  This was originally done in VB.NET with the ODBC provider.  I am having trouble with passing the id back into the query using the database factory.

This line does not work: cmd.Parameters.Add(id);


public static string GetUsername(string ID)
{
            System.Configuration.AppSettingsReader appReader = new System.Configuration.AppSettingsReader();
            string Provider = appReader.GetValue("Provider", typeof(string)).ToString();
            string ConnectionString = appReader.GetValue("ConnectionString", typeof(string)).ToString();
            System.Data.Common.DbProviderFactory factory = System.Data.Common.DbProviderFactories.GetFactory(Provider);
            System.Data.Common.DbConnection cnn = factory.CreateConnection();
            System.Data.Common.DbCommand cmd = factory.CreateCommand();
            System.Data.Common.DbDataReader reader;
 
            cnn.ConnectionString = ConnectionString;
            cmd.Connection = cnn;
            cmd.CommandText = "SELECT Employee.first, Employee.last FROM Employee WHERE Employee.id=?";
 
            cnn.Open();
            reader = cmd.ExecuteReader();
            if (reader.HasRows == true)
            {
                return reader[0].ToString() + (32).ToString() + reader[1].ToString();
            }
            else
            {
                return "Unknown";
            }
            cnn.Close();
            cnn.Dispose();
 
}

Open in new window

.NET Programming

Avatar of undefined
Last Comment
CyberUnDead

8/22/2022 - Mon