如何通过Visual FoxPro数据库中的SQL将地址字符串分为街道和门牌号

时间:2020-08-07 11:27:53

标签: sql vb.net visual-foxpro

我们的客户使用Visual FoxPro数据库。 我需要创建一个sql字符串,在该字符串中,我必须将地址字段分为街道和门牌号。 我找到了AT和CharIndex函数,但是它们只能直接在Visual Fox Pro中使用,而不能在SQL语句中使用。

我使用vb.net通过oledb连接到VFP数据库。 它必须在sql语句中完成,不能再使用其他代码。

谢谢。

Dim con As System.Data.OleDb.OleDbConnection = Nothing
    con = New System.Data.OleDb.OleDbConnection("Provider=vfpoledb;Data Source=" & Me.lblDatabase.Text & ";Collating Sequence=machine;")
    con.Open()

    Dim sqlString As String = "SELECT Address FROM AddressTable"
    Dim cmd As OleDbCommand = New OleDbCommand(sqlString, con)
    Try
        con.Open()
    Catch ex As Exception

    End Try


    Dim myDA As OleDbDataAdapter = New OleDbDataAdapter(cmd)

    Dim myDataSet As DataSet = New DataSet()

    ' Using DataAdapter object fill data from database into DataSet object
    Try
        myDA.Fill(myDataSet, "MyTable")
    Catch ex As Exception
        MsgBox("Error " & Err.Description)
    End Try


    ' Binding DataSet to DataGridView
    Try
        DataGridView1.DataSource = myDataSet.Tables("MyTable").DefaultView
    Catch ex As Exception

    End Try


    con.Close()

1 个答案:

答案 0 :(得分:0)

您是说它在第一个空格处不断地分开。然后:

Dim constr As String = "Provider=vfpoledb;Data Source=" & Me.lblDatabase.Text
Dim sqlString As String = "SELECT Address, 
    cast(Substr(Address, 1, AT(' ', Address)) as char(100)) as StreetName,
    cast(Ltrim(Substr(Address, AT(' ', Address))) as char(100)) as AddressLineRest
    FROM AddressTable"

Dim myDataTable As DataTable = New DataTable()
Dim myDA As OleDbDataAdapter = New OleDbDataAdapter(sqlString, constr)

' Using DataAdapter object fill data from database into DataSet object

Try
    myDA.Fill(myDataTable)
    DataGridView1.DataSource = myDataTable.DefaultView
Catch ex As Exception
    MsgBox("Error " & Err.Description)
End Try

PS:您可能想使用标准的.Net MessageBox代替MsgBox。

注意:将数据转换为已知的数据类型(例如此处的Char(100))非常重要。否则,您可能会默默地丢失数据。