如何在vb.net中将字符串值转换为整数

时间:2016-01-23 13:36:17

标签: vb.net

enter image description here费用数据类型在Sql Server数据库中是int指南

我从表费用

获得的费用的价值
'Total Expenses B/W Dates
    sql = "select COALESCE (SUM (amount), 0) from tblexpense Where transactiondate >= @p1 and transactiondate <= @p2"
    CmdObj = New SqlCommand(sql, ConObj)
    CmdObj.Parameters.Add("@p1", SqlDbType.Date).Value = DateTimePicker1.Value.Date
    CmdObj.Parameters.Add("@p2", SqlDbType.Date).Value = DateTimePicker2.Value.Date
    CmdObj.ExecuteScalar()
    Dim sumexpense As Integer = CmdObj.ExecuteScalar
    LblExp.Text = "Rs. " & sumexpense

保存按钮以将费用的值保存到表

Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
    ConObj = New SqlConnection(ConStr)
    ConObj.Open()
    Dim sql As String = "insert into tblbalance (tcon,pcon,ecoll,pendcoll,expense,bcoll,nprofit) values(@tcon,@pcon,@ecoll,@pendcoll,@expense,@bcoll,@nprofit)"

    With CmdObj
        .Parameters.Add("@tcon", SqlDbType.Int).Value = lblTcon.Text
        .Parameters.Add("@pcon", SqlDbType.Int).Value = LblPcon.Text
        .Parameters.Add("@ecoll", SqlDbType.Int).Value = LblEColl.Text
        .Parameters.Add("@pendcoll", SqlDbType.Int).Value = LblPColl.Text
        .Parameters.Add("@expense", SqlDbType.Int).Value = LblExp.Text
        .Parameters.Add("@bcoll", SqlDbType.Int).Value = LblBcoll.Text
        .Parameters.Add("@nprofit", SqlDbType.Int).Value = LblNet.Text

    End With
    CmdObj.Connection = ConObj
    CmdObj.ExecuteNonQuery()
    MsgBox("Saved Successfully")
    ConObj.Close()
End Sub

1 个答案:

答案 0 :(得分:0)

Imports System.Text.RegularExpressions
Private Shared Function GetIntOnly(ByVal value As String) As Integer
    Dim returnVal As String = String.Empty
    Dim collection As MatchCollection = Regex.Matches(value, "\d+")
    For Each m As Match In collection
        returnVal += m.ToString()
    Next
    Return Convert.ToInt32(returnVal)
End Function

  Dim sql As String = "insert into tblbalance (tcon,pcon,ecoll,pendcoll,expense,bcoll,nprofit) values('" &
        lblTcon.Text & "','" & LblPcon.Text & "','" & GetIntOnly(LblEColl.Text) & "','" & GetIntOnly(LblPColl.Text) & "','" &
        GetIntOnly(LblExp.Text) & "','" & GetIntOnly(LblBcoll.Text) & "','" & GetIntOnly(LblNet.Text) & "')"