【问题标题】:How can I convert Dataset column value to an integer如何将数据集列值转换为整数
【发布时间】:2014-07-06 13:38:57
【问题描述】:
public DataSet ds = new DataSet();
public SqlDataAdapter adapter = new SqlDataAdapter();

public int AuthenticatedUserAge(String User_name)
{
    string sql = "SELECT UserName, Age FROM tblDataProg WHERE (UserName ='" +   User_name  + "')";
    ds = GetDataSet(sql);
    int help = int.Parse(ds.Tables[0].Rows[0]["Age"].ToString());
    return help;    
}

public DataSet GetDataSet(string sql)
{
    SqlConnection connection = new SqlConnection(ConnStr());
    SqlCommand cmd = new SqlCommand();
    cmd.CommandText = sql;
    cmd.Connection = connection;
    adapter = new SqlDataAdapter(cmd);
    connection.Open();
    adapter.Fill(ds);
    connection.Close();
    return ds;
}    

public string ConnStr()
{
    return @"Data Source=.\SQLEXPRESS;AttachDbFilename='G:\עבודת גמר תכנות\v5\חדש תיקיה 1\v4\App_Data\SiteDB.mdf';Integrated Security=True;User Instance=True";
}

我需要检查从AuthenticatedUserAge 返回的值是否小于 17,它会写你好少年,否则写你好成人。

我认为的问题是这一行没有返回值:
int help = int.Parse(ds.Tables[0].Rows[0]["Age"].ToString());
它抛出这个错误:

输入的字符串格式不正确

【问题讨论】:

  • 你能告诉我们你是如何在你的代码中调用这些方法的吗?例如,您在哪里分配给username 会话变量?
  • 返回什么?如果它抛出错误,会抛出什么错误?你设置断点并调试过这个吗?
  • 它会抛出这个错误:输入字符串的格式不正确。
  • 我确实设置了断点并调试了这个
  • 它抛出这个错误:输入字符串的格式不正确@Phil Sandler

标签: c# asp.net dataset sql-server-express


【解决方案1】:

这不是真正的答案,但在评论中不会明确。

打破这条线:

int help = int.Parse(ds.Tables[0].Rows[0]["Age"].ToString());

进入:

//set a breakpoint on the next line
string temp = ds.Tables[0].Rows[0]["Age"].ToString(); 
//take one step (F10) over the break and hover over temp
//or use a watch.  What is the value of temp?
int help = int.Parse(temp);

【讨论】:

    【解决方案2】:

    数据库中该用户年龄的值必须为 NULL。
    您需要检查 NULL,并且为了安全起见,您还应该检查数据库是否没有返回任何结果。

    public int AuthenticatedUserAge(String User_name)
    {
        string sql = "SELECT UserName, Age FROM tblDataProg WHERE (UserName ='" +   User_name  + "')";
        ds = GetDataSet(sql);
        //check for no results
        if(ds.Tables.Count == 0 || ds.Tables[0].Rows.Count == 0)
        {
           return 0;
        }
        //check for null
        if(ds.Tables[0].Rows[0]["Age"] == DBNull.Value)
        {
           return 0;
        }
        int help = int.Parse(ds.Tables[0].Rows[0]["Age"].ToString());
        return help;    
    }
    

    此外,您真的不应该使用动态 SQL - 请改用参数。阅读this

    【讨论】:

      【解决方案3】:

      使用TryParse方法防止出错:

      public int AuthenticatedUserAge(String User_name)
      {
          string sql = "SELECT UserName, Age FROM tblDataProg WHERE (UserName ='" +   User_name  + "')";
          ds = GetDataSet(sql);
          int help = 0;
          int.TryParse(ds.Tables[0].Rows[0]["Age"].ToString(), out help);
          return help;    
      }
      

      使用上面的代码,如果发生错误,help 的值将保持在它的初始化值(本例中为 0)。

      【讨论】:

        【解决方案4】:

        试试这个。它会帮助你。

        int help = Convert.ToInt32(ds.Tables[0].Rows[0]["Age"]);
        

        【讨论】:

          【解决方案5】:

          可能年龄字段为 Null,您可以使用它来识别和设置默认值。

          int Help = ds.Tables[0].Rows[0]["Age"] != DBNull.Value ? Convert.ToInt16(ds.Tables[0].Rows[0]["Age"]) : 0;
          

          【讨论】:

            猜你喜欢
            • 2013-07-23
            • 1970-01-01
            • 2022-01-03
            • 1970-01-01
            • 2019-07-20
            • 1970-01-01
            • 1970-01-01
            • 2012-05-02
            • 2011-11-12
            相关资源
            最近更新 更多