共用方式為


如何:將 SQL 陳述式提交至遠端 SQL Server (以程式設計的方式)

您可以使用 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 協助