【问题标题】:cast null value from database从数据库中转换空值
【发布时间】:2022-09-27 11:42:17
【问题描述】:

我有一个从数据库中获取数据的表单,我想将数据从 null 转换为值 0,以防数据库中没有行,我尝试了很多但没有找到解决方案

{
        con.Open();
        SqlCommand cmd = con.CreateCommand();
        cmd.CommandType = CommandType.Text;
        cmd.CommandText = \"select max(Inv_no) from Tb_agentSales\";
        cmd.ExecuteNonQuery();
        DataTable dt = new DataTable();
        SqlDataAdapter ds = new SqlDataAdapter(cmd);
        ds.Fill(dt);
                   
        if (dt.Rows.Count > 0 )
        {
            double result = (Convert.ToDouble(cmd.ExecuteScalar()) +1);
            textBox23.Text = result.ToString();
        }
     

        con.Close();

    }
  • 请澄清您的具体问题或提供其他详细信息以准确突出您的需求。正如它目前所写的那样,很难准确地说出你在问什么。

标签: integer row sql-null


【解决方案1】:
{
    con.Open();
    SqlCommand cmd = con.CreateCommand();
    cmd.CommandType = CommandType.Text;
    cmd.CommandText = "select max(Inv_no) from Tb_agentSales";
    cmd.ExecuteNonQuery();
    DataTable dt = new DataTable();
    SqlDataAdapter ds = new SqlDataAdapter(cmd);
    ds.Fill(dt);

    double result;         
    if (dt.Rows.Count > 0)
    {
        result = (Convert.ToDouble(cmd.ExecuteScalar()) + 1);
        textBox23.Text = result.ToString();
    }
    else {
        result = 0;
    }
    

    con.Close();

}

这应该有效,但请注意我尚未对其进行测试。

【讨论】:

  • 是的,它的工作,但如果数据库 Null ,将返回错误,因为它不能种姓到 int 或 double
猜你喜欢
  • 2013-01-02
  • 1970-01-01
  • 1970-01-01
  • 2017-02-16
  • 1970-01-01
  • 2014-03-19
  • 2021-06-07
  • 2020-10-22
  • 1970-01-01
相关资源
最近更新 更多