OleDB参数 [英] OleDB Parameters

查看:87
本文介绍了OleDB参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个访问数据库,我有一个州名的ddl和年份的ddl.我有一个gridview,我想将状态下拉列表的值传递到where子句中.显然,如果我可以将sql与命名参数一起使用,那将是我所坚持的,并且不确定如何正确设置其格式.

I have this access db that I have a ddl for the state name and a ddl for the year. I have a gridview that I'd like to pass the value of the state drop down list into where clause. Obviously if I could use sql with the named parameters I would but this is what I'm stuck with and not sure exactly how to format it correctly.

下拉列表的名称为ddlStates.在我尝试过的参数中

the drop down list is name ddlStates. In the parameters I've tried

mycommand.Parameters.Add("@ddlStates")

这是数据集

        public DataSet GetData()
    {
        DataSet ds;
        using (OleDbConnection myConnString = new OleDbConnection())
        {
            myConnString.ConnectionString = connString;
            using (OleDbCommand myCommand = new OleDbCommand())
            {
                myCommand.CommandText = "select * from tblTest where location = ?";
                myCommand.Parameters.Add();
                myCommand.Connection = myConnString;



                using (OleDbDataAdapter da = new OleDbDataAdapter())
                {
                    da.SelectCommand = myCommand;
                    ds = new DataSet();
                    da.Fill(ds, "Grades");
                }
            }
            return ds;
        }
    }//ends get data dataset

推荐答案

1.您需要打开连接

2.您可以按如下所示添加参数

2.you can add the parameter as follows

public DataSet GetData()
{
    DataSet ds;
    using (OleDbConnection conn = new OleDbConnection(connString))
    {
        string query= "select * from tblTest where location = ?";            
        using (OleDbCommand myCommand = new OleDbCommand(query, conn))
        {

            myCommand.Parameters.AddWithValue("@ddlStates", <your value>);
            conn.Open();

            using (OleDbDataAdapter da = new OleDbDataAdapter(myCommand, conn))
            {
                ds = new DataSet();
                da.Fill(ds, "Grades");
                return ds;
            }
        }

    }
}

这篇关于OleDB参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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