English 中文(简体)
利用KalDataReader, 并控制KallSource上的价值观
原标题:Using SQLDataReader with SQLDataSource and controls values on page

我有一个搜索引擎,在伙伴关系中使用<代码>SqlDataSource控制。 信息网从具有参数的储存程序中获取数据。 我从默认价值和网页控制中得到参数,这样用户就可以获得他想要的东西。 然后在<代码>GridView控制中显示这一数据。

现在,我必须增加一个出口县,它将在一份供用户下载的Excel文件中输出结果。 I m并不确定它是否是最佳方法,但我使用了微软体的算法()。 当我试图获得搜索场的价值时,问题就会出现。 你们看到,我们把这个模板作为模板,因为我们有大约十几个搜索引擎,我们希望有能动发挥作用的东西。

Here s an exemple of the SqlDataSource control on the aspx page :

<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() + "	";
        }

        //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) + "	";
           }

           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;
        */
    }

由于出口代码需要动态,因此需要在的管制中使用whatever。 因此,如果它有更多的参数、不同的联系或像这种联系一样,它仍然需要工作。 我们不得不改变为不同发动机的代码。

因此,这里的问题是:How ,我可以获得<代码>所用控制值。 缩略语 SqlDataReader dr in the C#user-下限方法 Without exportExcel ( associated with my aspx page without硬编码 the control IDs?

最佳回答
问题回答

暂无回答




相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Transaction handling with TransactionScope

I am implementing Transaction using TransactionScope with the help this MSDN article http://msdn.microsoft.com/en-us/library/system.transactions.transactionscope.aspx I just want to confirm that is ...

System.Web.Mvc.Controller Initialize

i have the following base controller... public class BaseController : Controller { protected override void Initialize(System.Web.Routing.RequestContext requestContext) { if (...

Microsoft.Contracts namespace

For what it is necessary Microsoft.Contracts namespace in asp.net? I mean, in what cases I could write using Microsoft.Contracts;?

Separator line in ASP.NET

I d like to add a simple separator line in an aspx web form. Does anyone know how? It sounds easy enough, but still I can t manage to find how to do it.. 10x!

热门标签