Windows Mobile v 6.5连接到SQL Server 2008 R2

时间:2016-10-20 02:57:26

标签: vb.net visual-studio-2008 windows-mobile-6.5

我在使用我的SQL Server 2008中的vb.net开发的Windows移动应用程序作为后端时遇到问题。这是我的连接字符串: 数据源= STEPH-PC \ SQL2008;初始目录= MyDB;用户ID = myusername;密码= mypassword;

它总是给我一个SQL服务器不存在或访问被拒绝的错误。有关如何解决此问题的任何帮助吗?

1 个答案:

答案 0 :(得分:0)

1-测试连接服务器与Pocket PC:

首先测试您的SQL Server CE移动代理是否连接到SQL Server CE服务器代理。为此,您必须在服务器和Pocket PC中安装SQL Server CE 3.5。在谷歌搜索如何安装SQL Server CE 3.5。在此过程中,您在服务器中创建了一个虚拟目录,并在该目录中有一个文件sqlcesa35.dll,因此要在Pocket PC浏览器中测试连接,请写:http://ipserver/virtual_directory/sqlcesa35.dll(当然ipserver必须是您的服务器IP和virtual_directory必须是您的虚拟目录名称)。这样做你必须在Pocket PC上输入以下消息: Microsoft SQL Server Compact 服务器代理

此时我必须提到,您必须始终保持互联网连接。

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中执行数据库服务器中的句子并将数据从Pocket传输到服务器:

    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

正如我上面所说:我放在这里的代码需要调试...我只从项目中提取了一些部分并放在这里。希望这会对你有所帮助!