0

Visual Studio 2010 を使用しており、データベースを追加して SQLdatasource に接続しています。基本的なログインを作成しています。ユーザーにログインを入力してもらいたいのですが、ログインしようとすると、データベースを介して対話し、ログイン名が存在するかどうかを確認したいと考えています。データベースから 1 つの列だけを選択して反復処理するにはどうすればよいでしょうか。

select SQLステートメントは次のようになると思います

tblUser から userName を選択

username は列で、tblUser はテーブルです

4

1 に答える 1

1

SQL ステートメントは正しく、最後に SQLDataSource は次のようになります。

<asp:SqlDataSource
          id="SqlDataSource1"
          runat="server"
          DataSourceMode="DataReader"
          ConnectionString="Data Source=localhost;Integrated Security=SSPI;Initial Catalog=Northwind;"
          SelectCommand="SELECT userName from tblUser">
      </asp:SqlDataSource>

注: 構成ファイルにある接続文字列を使用することもできます。

 ConnectionString="<%$ ConnectionStrings:MyNorthwind%>"

また、結果をコントロールにバインドしないように聞こえるため、SQLDataSource を使用せずにこのクエリを実行することもできます。例えば:

using (SqlConnection connection = new SqlConnection(
               connectionString))
    {
        SqlCommand command = new SqlCommand(
            "SELECT userName from tblUser", connection);
        connection.Open();
        SqlDataReader reader = command.ExecuteReader();
        try
        {
            while (reader.Read())
            {
               // check if reader[0] has the name you are looking for

            }
        }
        finally
        {
            // Always call Close when done reading.
            reader.Close();
        }
    }
于 2012-04-26T01:33:39.163 に答える