的值'我有一個搜索引擎,它在ASP.NET中使用SqlDataSource
控件,該控件從具有參數的存儲過程獲取數據。我從頁面上的默認值和控件中獲取參數,以便用戶可以獲得他想要的內容。該數據然後顯示在GridView
控件中。在SQLDataSource中使用SQLDataReader並控制'
現在,我不得不添加一個導出按鈕,它將導出結果到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()
的SqlDataReader dr
參數控件的值鏈接到我的aspx頁面沒有硬編碼控件的ID的?
我認爲你錯過了一部分問題:我怎樣才能得到它的價值? – JMichelB
要獲取需要使用Evaluate方法評估ControlParameter對象的值,請執行以下操作:http://msdn.microsoft.com/zh-cn/library/system.web.ui.webcontrols.controlparameter.evaluate.aspx – Josh
我可以'找到一種方法來使用'評估'! MSDN並沒有給出一個例子,VS也不能幫助你......你更新了鏈接,你爲什麼不更新你的例子呢? – JMichelB