Sqldatasource将Oracle数据库与参数连接起来,代码无法正常工作

时间:2014-07-29 14:02:12

标签: c# oracle parameters sqldatasource

我需要在SQLDatasource中设置3个参数,将源绑定到gridview 如果我删除参数,并直接从设计视图中选择数据源,它会正确显示数据 我想在传递参数之后,Oracle有一些特殊的方法来处理SQL,但是一旦传递了参数,gridview就没有任何东西可以显示。我怎样才能解决这个问题?

请参阅aspx代码:

<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$        ConnectionStrings:ConnectionString %>" ProviderName="<%$ ConnectionStrings:ConnectionString.ProviderName %>" SelectCommand="SELECT d.* FROM DECODATA D where (D.FAMILY like :family) AND (D.SERIES like :series) AND (D.MODEL like :model) ORDER BY D.FAMILY, D.SERIES, D.MODEL">  
         <SelectParameters>  
            <asp:Parameter Name="family" />
            <asp:Parameter Name="series" />
            <asp:Parameter Name="model" />
        </SelectParameters>
    </asp:SqlDataSource>  
The front code is automatically created.  

代码隐藏:搜索按钮:即使我硬编码这样的参数,仍然无法正常工作     (此参数设置适用于我知道的SQL Server)

SqlDataSource1.SelectParameters["family"].DefaultValue = "CLASSIC LOW";
            SqlDataSource1.SelectParameters["series"].DefaultValue = "%";
            SqlDataSource1.SelectParameters["model"].DefaultValue = "%";
            GridView1.DataSource = SqlDataSource1;
            GridView1.DataBind();                 

1 个答案:

答案 0 :(得分:0)

好的,这是我的回答......

 try
        {
            var oraCntStr = new OracleConnectionStringBuilder
            {
                DataSource =
                    "<something to connect to your database>",
                UserID = "<login>",
                Password = "<password>"
            };

            using (OracleConnection oraCntgv = new OracleConnection(oraCntStr.ToString()))
            {
                oraCntgv.Open();
                OracleCommand oraCmd = oraCntgv.CreateCommand();
                oraCmd.Parameters.Add(new OracleParameter("<parameter's name>", OracleDbType.Varchar2));
                oraCmd.Parameters["<parameter's name>"].Value = Unum;
                oraCmd.CommandText = @"<Select sentence with your <:parameter's name>>";
                OracleDataReader oraReader = oraCmd.ExecuteReader();

                if (oraReader.HasRows)
                {
                    GridView1.DataSource = oraReader;
                    GridView1.DataBind();
                }
                oraCmd.Dispose();
                oraReader.Close();
            }

        }
         catch (Exception err)
        {
            tbxTFStr.Text = "Something goes wrong: " + err.Message;
        }

它适用于我...此解决方案使用Oracle的ODT,并且不要忘记添加

using Oracle.DataAccess.Client;

我在网络应用程序中使用了这个解决方案