我想用空白加总列

时间:2017-06-26 22:22:19

标签: vb.net

我有一个包含销售表中支付金额的列。我想对列进行求和并将结果返回到文本框,但是当表为空或列中有一些空字段时我遇到错误。请帮帮我。我得到的错误消息是Object无法从DBNull转换为其他类型。我的代码因此而来。

void Start () {
        cent = new Vector3 (0,0,1);
    }

void OnTriggerStay2D(Collider2D col)
    {
    if(col.tag == "obstacle")
        {
        rb.velocity = Vector2.zero;
        rb.Sleep ();
        transform.Translate (playerRoom.transform.position);
        transform.RotateAround (center.transform.position, cent, speed * Time.deltaTime);
        }   
    }

1 个答案:

答案 0 :(得分:1)

Reference: SQL NULL Functions

尝试在提供的链接

中使用建议的SQL NULL函数之一

例如,如果连接是SQL Server,则更新查询以使用ISNULL,如果该字段为空,将返回默认值。

Dim sql = "SELECT SUM(ISNULL(Amt_Paid, 0)) FROM Sales WHERE Customer = 'Abraham';"
Using command = New OleDbCommand(sql, con)
    Dim total = convert.Toint32(command.ExecuteScalar) 
    Textbox1.Text = total.ToString 
End Using 

在上面的任何时候,Amt_Paid为空,它将默认为0,这样在汇总值时不会遇到任何错误。

如果使用其他数据存储,请参阅之前提供的参考链接,并查看是否提供了等效版本。