简体   繁体   中英

Run MsAccess query in c#

I would like to execute a query stored in MsAcces database in c#. I keep getting an error in "CreateDataReader" the error message:


"string" does not contain a definition for "CreateDataReader" and was not found an extension method "CreateDataReader" receiver type "string" as the first argument.


Here is the code, could someone tell me what i'm doing wrong here?

private void button1_Click(object sender, EventArgs e)
{
    try
    {
        OleDbConnection con = new OleDbConnection(@"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=|DataDirectory|\uchet_tovarov.mdb");
        String cmd = string.Format("сумма_определ_товара");
        OleDbCommand oleCmd = new OleDbCommand(cmd);

        oleCmd.CommandText = "сумма_определ_товара";
        oleCmd.CommandType = CommandType.StoredProcedure;

        IDataReader dr = cmd.CreateDataReader(oleCmd);

        while (dr.Read())
        {
            MessageBox.Show(dr.GetInt32(0).ToString());
        }              
    }
    catch { }
}

As the error message states, cmd here is a string:

IDataReader dr = cmd.CreateDataReader(oleCmd);

Looks like you're looking for oleCmd , your command object:

IDataReader dr = oleCmd.ExecuteReader();

Look at this line:

String cmd = string.Format("сумма_определ_товара");

The class name String must start with a capital letter in both places. Try first with this one:

String cmd = String.Format("сумма_определ_товара");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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