如何将 SQL 语句提交到远程 SQL 服务器(以编程方式)

通过使用 SqlCeRemoteDataAccess 对象,您可以将 SQL 语句直接提交到运行 Microsoft SQL Server 的计算机。

提交 SQL 语句

  1. 初始化 SqlCeRemoteDataAccess 对象,并设置连接的属性。

    SqlCeRemoteDataAccess rda = new SqlCeRemoteDataAccess("https://www.adventure-works.com/sqlmobile/sqlcesa30.dll", "Data Source=MyDatabase.sdf");
    
  2. 调用 SubmitSql 方法,将 SQL 语句(或存储过程名称)和连接字符串传递到 SQL Server 数据库。

    rda.SubmitSql("sp_ValidateData", strConn);
    

示例

此示例说明如何使用 SubmitSQL 方法在运行 SQL Server 的远程计算机上运行 SQL 命令。

string rdaOleDbConnectString = @"Provider = SQLOLEDB;Data Source=MySqlServer;
    Initial Catalog=AdventureWorks; User Id=username;
    Password = <password>";

        // Initialize RDA Object
        //
        SqlCeRemoteDataAccess rda = null;

        try
        {
            // Try the SubmitSql Operation
            //
            rda = new SqlCeRemoteDataAccess();

            rda.InternetLogin = "MyLogin";
            rda.InternetPassword = "<password>";
            rda.InternetUrl = "https://www.adventure-works.com/sqlmobile/sqlcesa30.dll";
            rda.LocalConnectionString = "Data Source=MyDatabase.sdf";

            rda.SubmitSql("CREATE TABLE MyRemoteTable (colA int)", rdaOleDbConnectString);
        }
        catch (SqlCeException)
        {
            // Handle errors here
            //
        }
        finally
        {
            //Dispose of the RDA object
            //
            rda.Dispose();
        }
Dim rdaOleDbConnectString As String = @"Provider = SQLOLEDB;Data Source=MySqlServer;" & _
    "Initial Catalog=AdventureWorks; " & _
    "User Id=username; Password = <password>"
        ' Initialize RDA Object
        '
        Dim rda As SqlCeRemoteDataAccess = Nothing

        Try
            ' Try the SubmitSql Operation
            '
            rda = New SqlCeRemoteDataAccess()

            rda.InternetLogin = "MyLogin"
            rda.InternetPassword = "<password>"
            rda.InternetUrl = "https://www.adventure-works.com/sqlmobile/sqlcesa30.dll"
            rda.LocalConnectionString = "Data Source=MyDatabase.sdf"

            rda.SubmitSql("CREATE TABLE MyRemoteTable (colA int)", rdaOleDbConnectString)
        Catch
            ' Handle errors here
            '
        Finally
            'Dispose of the RDA object
            '
            rda.Dispose()
        End Try

请参阅

概念

使用 SubmitSQL 方法

帮助和信息

获取 SQL Server Compact Edition 帮助