从sql Command存储值并将其与值进行比较

时间:2012-12-06 05:58:24

标签: c# asp.net sqlcommand

这是参考我之前的问题:

Search data in database

我使用以下代码执行查询。但是我遇到了从命令中存储值的问题,然后使用该结果来比较值。

这是我的代码:

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();

5 个答案:

答案 0 :(得分:1)

查看Retrieving Data Using a DataReader (ADO.NET)

,更具体地说,在

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

在任何get函数

例如

SqlDataReader.GetString Method SqlDataReader.GetInt32 Method

所以你可以试试像

这样的东西
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;
}

答案 1 :(得分:1)

您需要调用sdrDatanew.GetInt32(0)方法并使用parameterized sql语句。

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;
      }
    }
}

答案 2 :(得分:0)

您的代码看起来是正确的。在行

之前声明变量
if (sdrDatanew.HasRows)

并将值存储在您想要的位置。您可以在编写switch case的地方使用变量。

//这将存储行中第一列的值。

var row=((IDataRecord) (sdrDatanew))[0];

答案 3 :(得分:0)

如果您想获得结果,请尝试使用此代码

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

}

我无法理解你在做什么。你想得到结果吗?结果是它只是一个字符串还是字符串集合?

答案 4 :(得分:0)

请尝试以下方法:

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

或者因为它只是一列您选择以下内容:

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

                    string value = cmd.ExecuteScalar().ToString();
                }
相关问题