
前提・実現したいこと
c# asp.netでデータベースの中から利用者の入力したキーワードを含む名前を表示したい。
ここに質問の内容を詳しく書いてください。
webアプリケーションを作っているのですが、データベースに登録した名前からユーザーが入力したキーワードを含む名前だけをDataListを使って表示しようとしています。
1.利用者がキーワードを入力して検索ボタンを押す。
2.selectでキーワードを含む名前を選別
3.select結果をDataListに表示する
という動作を目指しています。
select 名前 from 会員 where 名前 LIKE N'%@kensaku%'
でキーワードを含む名前のselectができるのは、SQLマネージャーで確かめました。
ただこのselectの結果をDataListに表示する方法が分かりません。
よろしくお願いします。
自分の開発環境(windows10, .NETFramework 4.7.2, Visual Studio 2019 ,Microsoft SQL Server 2017 (RTM) - 14.0.1000.169 )
該当のソースコード
HTML
1 2<%@ Page Title="" Language="C#" MasterPageFile="~/Site1.Master" AutoEventWireup="true" CodeBehind="index.aspx.cs" Inherits="WebApplication6.WebForm1" %> 3<asp:Content ID="Content1" ContentPlaceHolderID="head" runat="server"> 4 <link rel="stylesheet" type="text/css" href="StyleSheet5.css"> 5</asp:Content> 6<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="server"> 7 <p id="kensaku"> 8 <asp:TextBox ID="TextBox1" runat="server" style="width: 148px"></asp:TextBox> 9 <asp:Button ID="Button1" runat="server" Text="検索" /> 10 </p> 11 12 <div id="gakubuti" style="height: 650px"> 13 <div id="name"> 14 <asp:DataList ID="DataList1" runat="server" DataSourceID="SqlDataSource1" OnSelectedIndexChanged="DataList1_SelectedIndexChanged" Width="186px" ForeColor="#3399FF" RepeatColumns="15" CellPadding="10" CellSpacing="10" Font-Bold="False" Font-Italic="False" Font-Overline="False" Font-Strikeout="False" Font-Underline="False" GridLines="Horizontal" HorizontalAlign="Justify"> 15 <HeaderStyle Font-Bold="False" Font-Italic="False" Font-Overline="False" Font-Strikeout="False" Font-Underline="False" /> 16 <ItemTemplate> 17 18 <asp:Label ID="名前Label" runat="server" Text='<%# Eval("名前") %>' /> 19 20 </ItemTemplate> 21 <SeparatorStyle BorderStyle="Solid" /> 22 </asp:DataList> 23 24 <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:GraveConnectionString %>" SelectCommand="SELECT [名前] FROM [会員]"></asp:SqlDataSource> 25 </div> 26 </div> 27</asp:Content>
c#
1 2using System; 3using System.Collections.Generic; 4using System.Configuration; 5using System.Data.SqlClient; 6using System.Linq; 7using System.Web; 8using System.Web.UI; 9using System.Web.UI.WebControls; 10 11namespace WebApplication6 12{ 13 public partial class WebForm1 : System.Web.UI.Page 14 { 15 protected void Page_Load(object sender, EventArgs e) 16 { 17 18 } 19 20 protected void DataList1_SelectedIndexChanged(object sender, EventArgs e) 21 { 22 23 } 24 25 protected void Button1_Click(object sender, EventArgs e) 26 { 27 // 接続文字列の取得 28 var connectionString = 29 30 ConfigurationManager.ConnectionStrings["GraveConnectionString"].ConnectionString; 31 32 using (var connection = new SqlConnection(connectionString)) 33 34 using (var command = connection.CreateCommand()) 35 { 36 try 37 { 38 39 // データベースの接続開始 40 connection.Open(); 41 42 // SQLの準備 43 command.CommandText = @"select 名前 from 会員 where 名前 LIKE N'%@kensaku%';"; 44 command.Parameters.Add(new SqlParameter("@kensaku", TextBox1.Text)); 45 46 // SQLの実行 47 command.ExecuteNonQuery(); 48 49 50 } 51 catch (Exception exception) 52 { 53 54 throw; 55 } 56 finally 57 { 58 // データベースの接続終了 59 connection.Close(); 60 61 } 62 } 63 } 64 65 66 } 67 68}



回答1件
あなたの回答
tips
プレビュー