0

これは私の前の質問を参照しています:

データベース内のデータを検索

次のコードを使用してクエリを実行しました。しかし、コマンドからの値を保存し、その結果を使用して値を比較する際に問題が発生します。

これは私のコードです:

SqlDataReader sdrDatanew = null;
string strnew;
string connectionString = WebConfigurationManager.ConnectionStrings["Gen_LicConnectionString"].ConnectionString;
SqlConnection connew = new SqlConnection(connectionString);
connew.Open();
strnew = "select User_Type from User_Details where User_Type='" + ddlUserSel.SelectedItem.Value + "' AND LoginID = '" + txtUserName.Text + "' AND Password = '" + txtPassword.Text + "'";
SqlCommand sqlCommnew = new SqlCommand(strnew, connew);
sdrDatanew = sqlCommnew.ExecuteReader();
if (sdrDatanew.HasRows)
  {
     if (sdrDatanew.Read())
      {
          //Here I want to store the result from the sqlcommand in a variable
      }
  }

    switch (//Here I want to use the variable in a switch case) //<---
    {
        case 0:
            Response.Redirect("Lic_Gen.aspx");
            break;
        case 1:
            Response.Redirect("Cust_Page.aspx");
            break;
    }

    connew.Close();
4

5 に答える 5

1

DataReader を使用したデータの取得 (ADO.NET) をご覧ください

より具体的には、次の行で

Console.WriteLine("{0}\t{1}", reader.GetInt32(0),reader.GetString(1));

get 関数のいずれかで

例えば

SqlDataReader.GetString メソッドSqlDataReader.GetInt32 メソッド

あなたは次のようなものを試すことができます

int userType = 0;
if (sdrDatanew.HasRows)
{
    if (sdrDatanew.Read())
    {
        userType = sdrDatanew.GetInt32(0); //something like this
    }
}

switch (userType) //something like this
{
    case 0:
        Response.Redirect("Lic_Gen.aspx");
        break;
    case 1:
        Response.Redirect("Cust_Page.aspx");
        break;
}
于 2012-12-06T06:05:23.383 に答える
1

sdrDatanew.GetInt32(0)メソッドを呼び出してparameterizedsql ステートメントを使用する必要があります。

using(SqlConnection connew = new SqlConnection(connectionString))
 {
  strnew = @"select User_Type from User_Details where User_Type=@usertype 
             AND LoginID=@loginid AND Password = @password";
  using(SqlCommand sqlCommnew = new SqlCommand(strnew, connew))
   {
     sqlCommnew.Parameters.AddWithValue("@usertype",ddlUserSel.SelectedItem.Value);
     sqlCommnew.Parameters.AddWithValue("@loginid",txtUserName.Text);
     sqlCommnew.Parameters.AddWithValue("@password",txtPassword.Text);

     connew.Open();
     sdrDatanew = sqlCommnew.ExecuteReader();

     int userType=-1;
     if(sdrDatanew.Read())
        userType=sdrDatanew.GetInt32(0);

     switch (userType)
      {
       case 0:
           Response.Redirect("Lic_Gen.aspx");
           break;
       case 1:
           Response.Redirect("Cust_Page.aspx");
           break;
      }
    }
}
于 2012-12-06T06:07:08.870 に答える
0

あなたのコードは正しいようです。行の前に変数を宣言します

if (sdrDatanew.HasRows)

そして、必要な場所に値を保存します。switch case を記述した変数を使用できるようになります。

//これにより、行の最初の列の値が格納されます。

var row=((IDataRecord) (sdrDatanew))[0];
于 2012-12-06T06:03:57.493 に答える
0

代わりに次を試してください。

 string value = string.Empty
 if (sdrDatanew.HasRows)
 {
     while (sdrDatanew.Read())
     {
          value= results[0].ToString();
     }
 }

または、次を選択する1つの列にすぎないため:

 using (SqlCommand cmd = conn.CreateCommand())
                {
                    cmd.CommandText = "SELECT 'column' from 'table' WHERE 'column' ='criteria' ";

                    string value = cmd.ExecuteScalar().ToString();
                }
于 2012-12-06T06:18:05.210 に答える
0

結果を取得したい場合は、このコードを試してください

while (sdrDatanew.Read()) {
String userDetails = sdrDatanew("User_Details").ToString;

}

あなたが何をしているのか理解できません。結果を取得したいのですが、その結果は単なる文字列ですか、それとも文字列のコレクションですか?

于 2012-12-06T06:08:17.970 に答える