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

在任何获取功能

例如

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

如果你想得到结果试试这个代码

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

}

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

于 2012-12-06T06:08:17.970 回答
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();
                }
于 2012-12-06T06:18:05.210 回答