簡體   English   中英

在頁面上將 SQLDataReader 與 SQLDataSource 和控件的值一起使用

[英]Using SQLDataReader with SQLDataSource and controls' values on page

我有一個搜索引擎,它使用 ASP.NET 中的SqlDataSource控件,它從帶有參數的存儲過程中獲取數據。 我從頁面上的默認值和控件中獲取參數,因此用戶可以得到他想要的。 然后該數據顯示在GridView控件中。

現在,我必須添加一個導出按鈕,它將結果導出到 excel 文檔中,供用戶下載。 我不確定這是否是最好的方法,但我使用了微軟的算法( http://support.microsoft.com/default.aspx?scid=kb;en-us;308247 )來做到這一點。 當我嘗試獲取搜索字段的值時出現了問題。 你看,我們將使用它作為模板,因為我們有大約十幾個搜索引擎要制作,並且我們希望有一些可以動態工作的東西。

這是 aspx 頁面上的SqlDataSource控件的示例:

<asp:SqlDataSource ID="SearchDataSource" runat="server"
    ConnectionString="CONNECTIONSTRING"
    ProviderName="System.Data.SqlClient"
    SelectCommand="sp_SearchEngine_BASE" 
    SelectCommandType="StoredProcedure" onselected="SearchDataSource_Selected">
    <SelectParameters>
        <asp:ControlParameter ControlID="ctlID1" DbType="SomeType" DefaultValue="" 
            Name="spParamA" PropertyName="aProperty" />
        <asp:ControlParameter ControlID="ctlID2" DbType="SomeType" DefaultValue="" 
            Name="spParamB" PropertyName="aProperty" />
    </SelectParameters>
</asp:SqlDataSource>

這是導出代碼:

protected void exportExcel()
    {
        int i;
        String strLine = "", filePath, fileName, fileExcel;
        FileStream objFileStream;
        StreamWriter objStreamWriter;
        Random nRandom = new Random(DateTime.Now.Millisecond);
        //Dim fs As Object, myFile As Object
        SqlConnection cnn = new SqlConnection(SearchDataSource.ConnectionString);

        //Create a pseudo-random file name.
        fileExcel = "t" + nRandom.Next().ToString() + ".xls";

        //Set a virtual folder to save the file.
        //Make sure that you change the application name to match your folder.
        filePath = Server.MapPath("\\ExcelTest");
        fileName = filePath + "\\" + fileExcel;

        //Use FileStream to create the .xls file.
        objFileStream = new FileStream(fileName, FileMode.OpenOrCreate, FileAccess.Write);
        objStreamWriter = new StreamWriter(objFileStream);

        //Use a DataReader to connect to the Pubs database.
        cnn.Open();
        String sql  = SearchDataSource.SelectCommand;
        SqlCommand cmd = new SqlCommand(sql, cnn);

        //cmd.Parameters.Add(SearchDataSource.SelectParameters[0].);
        for (i = 0; i <= SearchDataSource.SelectParameters.Count - 1; i++)
        {
            // ---------------------------------------
            // ----- Here is where I am stuck... -----
            // ---------------------------------------
            //cmd.Parameters.AddWithValue(SearchDataSource.SelectParameters[i].Name, WhatDoIPutHere);
        }

        SqlDataReader dr;
        dr = cmd.ExecuteReader();

        //Enumerate the field names and records that are used to build the file.
        for(i = 0; i <= dr.FieldCount - 1; i++) {
           strLine = strLine + dr.GetName(i).ToString() + "\t";
        }

        //Write the field name information to file.
        objStreamWriter.WriteLine(strLine);

        //Reinitialize the string for data.
        strLine = "";

        //Enumerate the database that is used to populate the file.
        while (dr.Read()) {
           for(i = 0; i<= dr.FieldCount - 1; i++) {
              strLine = strLine + dr.GetValue(i) + "\t";
           }

           objStreamWriter.WriteLine(strLine);
           strLine = "";
        }

        //Clean up.
        dr.Close();
        cnn.Close();
        objStreamWriter.Close();
        objFileStream.Close();
        /*
        //Show a link to the Excel file.
        HyperLink1.Text = "Open Excel";
        HyperLink1.NavigateUrl = fileExcel;
        */
    }

由於它需要是動態的,因此導出代碼需要適用於SqlDataSource控件中使用的任何內容。 因此,如果它有更多參數、不同的連接字符串或類似的東西,它仍然需要工作。 我們不應該為不同的引擎更改代碼。

所以這里的問題是:如何獲取SqlDataSource SearchDataSource使用的控件的值作為 C# 用戶定義方法void exportExcel()鏈接到我的 aspx 頁面中的SqlDataReader dr的參數,而無需對控件的 ID 進行硬編碼?

像這樣的東西應該工作:

for(int i = 0; i < SearcDataSource.SelectParameters.Count; i++)
{
   string controlId= (ControlParameter)SearchDataSource.SelectParameters[i].ControlID;
}

你可能也想在這里做一個檢查 null:

for(int i = 0; i < SearcDataSource.SelectParameters.Count; i++)
{
   var controlParam = SearchDataSource.SelectParameters[i] as ControlParameter;
   if (controlParam == null) continue;
   string controlId= controlParam.ControlID;
}

要獲得您需要使用 Evaluate 方法評估 ControlParameter object 的值: http://msdn.microsoft.com/en-us/library/system.Z2567A5EC9705EB7AC2C984033E06parameter.evalZuate.asp

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM