0
新問題。 我有這個作爲我的gridview,我想要它,所以當頁面加載網格視圖充滿了數據庫信息。ASP.NET GridView更新/整體查詢結構
所以下面是gridview的代碼。以下是c#代碼。
UPDATE
<asp:GridView ID="RegistrantsView" runat="server" AllowPaging="True"
AllowSorting="True" AutoGenerateColumns="True"
CellPadding="4"
ForeColor="#333333" GridLines="None">
<AlternatingRowStyle BackColor="White" ForeColor="#284775" />
<EditRowStyle BackColor="#999999" />
<FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
<HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
<RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
<SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
<SortedAscendingCellStyle BackColor="#E9E7E2" />
<SortedAscendingHeaderStyle BackColor="#506C8C" />
<SortedDescendingCellStyle BackColor="#FFFDF8" />
<SortedDescendingHeaderStyle BackColor="#6F8DAE" />
</asp:GridView>
C#:
SqlConnection connection = new SqlConnection(ConfigurationManager.ConnectionStrings["FFL-New DataConnectionString"].ConnectionString);
protected void Page_Load(object sender, EventArgs e)
{
connection.Open();//opens connection on page load
SqlCommand selectAllCommand = new SqlCommand();
selectAllCommand.CommandText = "select * from registrants";
selectAllCommand.Connection = connection;
SqlDataAdapter sda = new SqlDataAdapter();
sda.SelectCommand = selectAllCommand;
DataTable dt = new DataTable();
sda.Fill(dt);
RegistrantsView.DataSource = dt;
RegistrantsView.DataBind();
}
我會使用參數化查詢,我明白爲什麼現在。但我現在有另一個問題。我仍然使用搜索框來抓取用戶的輸入。我仍然可以接受SQL注入嗎?我應該擺脫搜索框功能嗎? – j0hnstew
@stewbydoo - 不要擺脫搜索框功能,只是不要使用'「select * ... where myColumn =」+ searchTextBox.Text'使用** [參數](http://www.dotnetperls .com/sqlparameter)**而不是 – Blachshma
試過這個,它導致一個錯誤,其中DataSource和DataSource ID都被調用,因此它告訴我使用其中一個或另一個。 – j0hnstew