簡體   English   中英

Windows Mobile v 6.5連接到SQL Server 2008 R2

[英]Windows Mobile v 6.5 connecting to SQL Server 2008 R2

我在使用SQL Server 2008作為后端使用vb.net開發的Windows Mobile應用程序連接時遇到問題。 這是我的連接字符串:Data Source = STEPH-PC \\ SQL2008; Initial Catalog = MyDB; 用戶ID = myusername; 密碼= mypassword;

它總是給我一個錯誤,指出SQL Server不存在或訪問被拒絕。 對如何解決此問題有幫助嗎?

1-測試連接服務器與Pocket PC:

首先,請測試您的SQL Server CE移動代理是否連接到SQL Server CE Server代理。 為此,您必須在服務器和Pocket PC中安裝SQL Server CE 3.5。 在Google中搜索如何安裝SQL Server CE 3.5。 在此過程中,您在Server中創建一個虛擬目錄,並且在該目錄中有一個文件sqlcesa35.dll,因此要在Pocket PC瀏覽器中測試連接,請輸入: http://ipserver/virtual_directory/sqlcesa35.dll (當然ipserver必須是您的服務器IP,virtual_directory必須是您的虛擬目錄名稱)。 為此,您必須在Pocket PC中獲得以下消息: Microsoft SQL Server Compact Server代理

在這一點上,我必須提到,您始終必須具有Internet連接。

2-從服務器檢索文件 (這是示例代碼,未經調試,我僅從其他項目中提取了一部分並將其放在此處)。

Function get_companies(ByVal sSucursal As String, ByVal cn_Interface As System.Data.SqlServerCe.SqlCeConnection, _
    ByVal cmd_Interface As System.Data.SqlServerCe.SqlCeCommand, ByVal dr_Interface As System.Data.SqlServerCe.SqlCeDataReader) As Boolean

    get_companies = False

    Dim _strRemoteConnect As String
    Dim _strLocalConnect As String
    Dim _strInternetURL As String = sInternetURL
    'The last variable sInternetURL is something like: http://ip_server/virtual_directory/sqlcesa35.dll

    _strRemoteConnect = "Provider=SQLOLEDB;Data Source=" & sIPSQLServer & ";Initial Catalog=" & sBDSQLServer & ";User Id=" & sUserSQLServer & ";Password=" & sClaveSQLServer
    'sIPSQLServer is the server ip where is running SQL Server
    'sBDSQLServer is your DataBase server.
    _strLocalConnect = "Data Source=" & sPath & "\" & sDataBase_Interface & "; Password=" & sPassword 
    'sPath is your directory in your Pocket PC, begings with \
    'sDataBase_Interface is the database in my Pocket PC, an .sdf file

    Dim rda As System.Data.SqlServerCe.SqlCeRemoteDataAccess = New System.Data.SqlServerCe.SqlCeRemoteDataAccess
    rda.InternetLogin = sUserInternet 'a valid user in your domain
    rda.InternetPassword = sClaveInternet
    rda.InternetUrl = _strInternetURL
    rda.LocalConnectionString = _strLocalConnect

    Do While True
        Try
            'In server database there is a table: Monedas
            rda.Pull("_Monedas", "Select Moneda, Descripcion, Abreviada From Monedas Where Sucursal = '" & sSucursal & "'", _
                                        _strRemoteConnect, System.Data.SqlServerCe.RdaTrackOption.TrackingOff)
            Exit Do
        Catch exc As System.Data.SqlServerCe.SqlCeException
            ShowErrorSqlServerCE(exc)
        Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try
    Loop      

    'read the data received, just testing, may be this code not to be here because you process the data outside the function.
    Try
        cmd_Interface.CommandText = "Select Moneda, Descripcion, Abreviada From _Monedas"
        dr_Interface = cmd_Interface.ExecuteReader()

        Do While dr_Interface.Read()
            messagebox.show("Moneda = " & dr_interface("Moneda") & " - " & dr_interface("Descripcion") & " - " & dr_interface("Abreviada"), _
            "Currencies Received", MessageBoxButtons.OK, MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button1)
        Loop
        get_companies = true
    Catch exc As System.Data.SqlServerCe.SqlCeException
        ShowErrorSqlServerCE(exc)
    Catch ex As Exception
        MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Asterisk, MessageBoxDefaultButton.Button1)       
    Finally
        'If cn_Interface.State <> ConnectionState.Closed Then
        '    cmd_Interface.Dispose()
        '    cn_Interface.Close()
        '    cn_Interface.Dispose()
        'End If
    End Try
End Function

3-從Pocket PC中執行Database Server中的一個句子並將數據從Pocket傳輸到Server:

    Function EnviarClientesNuevos(ByVal sSucursal As String, ByVal sZona As String, ByVal sRuta As String) As Boolean
    Dim sLineas As String
    Dim nRegs As Integer

    Dim cn_Interface As System.Data.SqlServerCe.SqlCeConnection
    Dim cmd_Interface As System.Data.SqlServerCe.SqlCeCommand

    EnviarClientesNuevos = False

    Dim _strRemoteConnect As String
    Dim _strLocalConnect As String
    Dim _strInternetURL As String = sInternetURL

    _strRemoteConnect = "Provider=SQLOLEDB;Data Source=" & sIPSQLServer & ";Initial Catalog=" & sBDSQLServer & ";User Id=" & sUserSQLServer & ";Password=" & sClaveSQLServer
    _strLocalConnect = "Data Source=" & sPath & "\" & sDataBase_Interface & "; Password=" & sPassword

    Dim rda As System.Data.SqlServerCe.SqlCeRemoteDataAccess = New System.Data.SqlServerCe.SqlCeRemoteDataAccess
    rda.InternetLogin = sUserInternet
    rda.InternetPassword = sClaveInternet
    rda.InternetUrl = _strInternetURL
    rda.LocalConnectionString = _strLocalConnect

    Do While True
        If DropTableE("_NEW_CLIENTES_XX") Then
            Try
                '_NEW_CLIENTES_XX is a table in your SQL Server (The server, not the Pocket)
                rda.Pull("_NEW_CLIENTES_XX", "Select Id, Sucursal, Zona, CodCli, Nombre, Direccion, Ruc, Clase, Ruta " & _
                "FROM _NEW_CLIENTES_XX WHERE Sucursal = ''", _strRemoteConnect, SqlServerCe.RdaTrackOption.TrackingOn)
                'In where clause I compare to '' because I only need the structure
                Exit Do
            Catch exc As System.Data.SqlServerCe.SqlCeException
                ShowErrorSqlServerCE(exc)
                Exit Function
            Catch ex As Exception
                MessageBox.Show(ex.Message)
                Exit Function
            End Try
        End If
    Loop

    Try
        '--
        cn_Interface = New System.Data.SqlServerCe.SqlCeConnection
        cn_Interface.ConnectionString = "Data Source=" & sPath & "\" & sDataBase_Interface & ";Password=" & sPassword
        cn_Interface.Open()

        cmd_Interface = cn_Interface.CreateCommand()
        cmd_Interface.CommandType = CommandType.Text
        '--

        'here I have an open connection to another database in my Mobile Device. Here I have to mention that I work with 2 databases in my mobile device:
        'One database for getting the data from server, I only use it when I get data from server and when I send data to server
        'and other database which is my main database, the database that is used all the day storing customers transactions.
        'here I already have open (outside this function) the connection to this second database, but the sentence are the same above,
        'something like this:
        'Try
        '    cn = New System.Data.SqlServerCe.SqlCeConnection
        '    cn.ConnectionString = "Data Source=" & sPath & "\" & sDataBase_Ppal & ";Password=" & sPassword
        '    cn.Open()

        '    cmd = cn.CreateCommand()
        '    cmd.CommandType = CommandType.Text

        'Read the data from my main Pocket PC database          
        cmd.CommandText = "SELECT CodCli, Nombre, Direccion, Ruc, Clase From CLIENTES WHERE CLASE IN ('N', 'M')"
        dr = cmd.ExecuteReader()
        Do While dr.Read()
            'Insert the data in the table structure that I get above.
            'remember that this table is in the database that only is used when transferring data, its a temporal database.
            cmd_Interface.CommandText = "INSERT INTO _NEW_CLIENTES_XX (Sucursal, Zona, CodCli, Nombre, Direccion, Ruc, Clase, Ruta) " & _
                              "VALUES('" & sSucursal & "', '" & sZona & "', " & dr("CodCli") & ", '" & _
                              dr("Nombre") & "', '" & dr("Direccion") & "', '" & dr("Ruc") & "', '" & _
                              dr("Clase") & "', '" & sRuta & "')"

            nRegs = cmd_Interface.ExecuteNonQuery()
        Loop
        dr.Close() : dr.Dispose()           
    Catch exc As System.Data.SqlServerCe.SqlCeException
        ShowErrorSqlServerCE(exc)
        Exit Function
    Catch ex As Exception
        MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Asterisk, MessageBoxDefaultButton.Button1)
        Exit Function
    Finally
        Try
            dr.Close()
            dr.Dispose()
        Catch ex As Exception
        End Try

        'If cn.State <> ConnectionState.Closed Then
        '    cmd.Dispose()
        '    cn.Close()
        '    cn.Dispose()
        'End If

        If cn_Interface.State <> ConnectionState.Closed Then
            cmd_Interface.Dispose()
            cn_Interface.Close()
            cn_Interface.Dispose()
        End If
    End Try

    Do While True
        Try
            'I excecute a sentence in the Server. I delete the data in _NEW_CLIENTES_XX which is a work table in SQL server.
            rda.SubmitSql("Delete From _NEW_CLIENTES_XX Where Sucursal = '" & sSucursal & "' AND Zona = '" & sZona & "' And Ruta = '" & sRuta & "'", _strRemoteConnect)

            'I send the data to server
            rda.Push("_NEW_CLIENTES_XX", _strRemoteConnect, System.Data.SqlServerCe.RdaBatchOption.BatchingOn)

            EnviarClientesNuevos = True

            Exit Do
        Catch exc As System.Data.SqlServerCe.SqlCeException
            ShowErrorSqlServerCE(exc)
            Exit Function
        Catch ex As Exception
            MessageBox.Show(ex.Message)
            Exit Function
        End Try
    Loop        
End Function

一般性:

Function DropTableP(ByVal sTabla As String) As Boolean
    'Dim cn_Interface As System.Data.SqlServerCe.SqlCeConnection
    'Dim cmd_Interface As System.Data.SqlServerCe.SqlCeCommand
    'Dim dr_Interface As System.Data.SqlServerCe.SqlCeDataReader
    Dim nRegs As Integer

    Try
        'cn_Interface = New System.Data.SqlServerCe.SqlCeConnection("Data Source=" & sPath & "\" & sDataBase_Ppal & "; Password=" & sPassword)
        'cn_Interface.Open()

        'cmd_Interface = cn_Interface.CreateCommand()
        'cmd_Interface.CommandType = CommandType.Text
        cmd.CommandText = "Select TABLE_NAME From INFORMATION_SCHEMA.TABLES Where TABLE_NAME = '" & sTabla & "'"
        dr = cmd.ExecuteReader()
        If dr.Read() Then
            dr.Close()
            dr.Dispose()

            cmd.CommandText = "DROP TABLE " & sTabla
            nRegs = cmd.ExecuteNonQuery()
            DropTableP = True
        Else
            dr.Close()
            dr.Dispose()
            DropTableP = True
        End If
    Catch exc As System.Data.SqlServerCe.SqlCeException
        ShowErrorSqlServerCE(exc)
    Catch ex As Exception
        MessageBox.Show(ex.Message)
    Finally
        'If cn_Interface.State <> ConnectionState.Closed Then
        '    cn_Interface.Close()
        '    cn_Interface.Dispose()
        'End If
    End Try
End Function

Sub ShowErrorSqlServerCE(ByVal exc As System.Data.SqlServerCe.SqlCeException)
    Dim bld As New System.Text.StringBuilder
    Dim err As System.Data.SqlServerCe.SqlCeError
    Dim errorCollection As System.Data.SqlServerCe.SqlCeErrorCollection = exc.Errors
    Dim errPar As String
    Dim numPar As Integer

    ' Loop through all of the errors.
    For Each err In errorCollection
        bld.Append(ControlChars.Cr & " Error Code: " & err.HResult.ToString("X"))
        bld.Append(ControlChars.Cr & " Message   : " & err.Message)
        bld.Append(ControlChars.Cr & " Minor Err.: " & err.NativeError)
        bld.Append(ControlChars.Cr & " Source    : " & err.Source)

        ' Loop through all of the numeric parameters for this specific error.
        For Each numPar In err.NumericErrorParameters
            If numPar <> 0 Then
                bld.Append(ControlChars.Cr & " Num. Par. : " & numPar.ToString())
            End If
        Next numPar

        ' Loop through all of the error parameters for this specific error.
        For Each errPar In err.ErrorParameters
            If errPar <> [String].Empty Then
                bld.Append(ControlChars.Cr & " Err. Par. : " & errPar)
            End If
        Next errPar

        ' Finally, display this error.
        MessageBox.Show(bld.ToString(), "SQL Server CE")

        ' Empty the string so that it can be used again.
        bld.Remove(0, bld.Length)
    Next err
End Sub

就像我上面說的:我放置在這里的代碼需要調試...我只從我的項目中提取了一部分並將其放置在這里。 希望這個能對您有所幫助!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM