- - PR -
GridViewでのSQL文の可変
1
投稿者 | 投稿内容 |
---|---|
|
投稿日時: 2007-04-04 17:11
Windowsサーバー2003
ASP.NET2.0 Visual Studio2005 aspxで記述しています。 SQL Serever2005 GridViewを使用して、グリッド表を作成しようとしています。 以下のソースで実装したい機能(ソート・ページングなど)はできたのですが、 実際には現在固定で記述してあるSQL部分を可変的にしたいのです。 前画面で、テキストボックスに値を入力して、それを検索キーとしてSQLの条件に追加し、 その作成されたSQLをGridViewで使用したいということです。 なかなか初心者なものでそれがうまくいかず、期限も迫ってしまいました。 どのようにすればSQLを可変的に設定できるのでしょうか。 申し訳ございませんが解答お願いいたします。 <asp:Content ID="Content1" ContentPlaceHolderID="cph" Runat="Server"> <asp:GridView ID="grid" runat="server" AllowPaging="True" AllowSorting="True" AutoGenerateColumns="False" CellPadding="4" DataKeyNames="title" DataSourceID="sds" ForeColor="#333333" GridLines="None" PageSize="5" ShowFooter="True" OnRowCreated="grid_RowCreated"> <FooterStyle BackColor="#990000" Font-Bold="True" ForeColor="White" /> <Columns> <asp:BoundField DataField="title" HeaderText="タイトル" HtmlEncode="False" SortExpression="title" /> <asp:BoundField DataField="contents" HeaderText="コンテンツ" HtmlEncode="False" SortExpression="contents" /> </Columns> </asp:GridView> <asp:SqlDataSource ID="sds" runat="server" ConnectionString="<%$ ConnectionStrings:reserveConnectionString %>" OldValuesParameterFormatString="original_{0}" SelectCommand="SELECT [title],[contents] FROM [listtbl]" > </asp:SqlDataSource> </asp:Content> |
|
投稿日時: 2007-04-04 18:26
回答はしますが、解答は自分で見つけて下さい。
列名は、文字列連結。ただし、入力を信じないこと。 検索の条件は、同じく列名を文字列連結、値はパラメータ。 列名は、正しい列名をリストで持っておき、突き合わせをして用いること。ユーザーの入力を信じちゃいけない。 _________________ |
1