2014-05-12 51 views
0

我有一個SQL查詢,它根據'位置'從表中檢索兩列。如何在HTML中以表格格式顯示SQL查詢數據?

下表:

content_id  content_title 
234   Fink, James 
90    Merylou, Jane 
45    Marcy, Kim 
112   Bower, John 
34    Alset, Mike 

通過以下查詢生成:

DECLARE @strLocation varchar(200) 
SET @strLocation = 'Ridge' 

SELECT [content_id], [content_title] 
FROM [content] 
WHERE [folder_id] = '188' 
AND (content_html LIKE '%'[email protected]+'%') 

查詢眺望content_html柱並查找該變量。

我的HTML看起來像這樣:

<input type=text size=50 id="txtPhysByLoc" runat="server" /><input type=button value="Go" /> 
<br /> 
<div> 
    <table border=0> 
     <span id="writeTable"></span> 
    </table> 
</div> 

我的C#代碼隱藏到目前爲止是這樣的:

protected void Page_Load(object sender, EventArgs e) 
{  
    string cString = "Provider=sqloledb;Data Source=myServer;Initial Catalog=Dbase;User Id=efv;[email protected];"; 
    SqlConnection Conn = new SqlConnection(ConnectionString); 
    Conn.Open(); 
} 

如何使用查詢我的C#代碼一起生成一個表?或者使用另一個ASP.net控件來顯示錶格數據?

+2

簡單(如果你已經建立這一點。)會是:使用'GridView',把它放在ASPX頁面中,在代碼中執行你的命令,在DataTable中獲得一個結果集,然後bin將其添加到GridView。 – Habib

+0

我可以只使用一個SQL命令,並用';'分隔每行嗎? – SearchForKnowledge

+1

簡答:是的。如果您要執行的唯一命令是有問題的命令,那麼您不需要多個語句。相反,你應該從你的Code後面傳遞參數,在後面的代碼中構造SQL命令,然後得到結果。 – Habib

回答

1
<asp:SqlDataSource runat="server" ID="sdsContent" ConnectionString="<%$ connectionStrings.connectionString %>" SelectCommandType="text" SelectCommand="your query here"> 
    </asp:SqlDataSource> 
    <asp:Repeater runat="server" ID="rptContent" DataSourceID="sdsContent"> 
    <ItemTemplate> 
    <%# Eval("content_title").ToString() %> 
    <br/> 
    <div style="clear:both;"></div> 
    </ItemTemplate> 
    </asp:Repeater> 

這應該寫類似:

 Fink, James 
    Merylou, Jane 
    Marcy, Kim 
    Bower, John 
    Alset, Mike 

 <%$ connectionStrings.connectionString %> 

部分選擇從webconfig的康恩財產

+0

作爲連接字符串,是否可以傳遞:'Provider = sqloledb; Data Source = myServer; Initial Catalog = DBase; User Id = efv; Password = st @ tl;'? – SearchForKnowledge

+0

yes但不安全, –

+0

'<%#Eval(」content_title「 %>

' – SearchForKnowledge

相關問題