Condividi tramite


Procedura: Invio di istruzioni SQL a un server SQL remoto (a livello di programmazione)

È possibile inviare direttamente istruzioni SQL a un computer che esegue Microsoft SQL Server utilizzando l'oggetto SqlCeRemoteDataAccess.

Per inviare un'istruzione SQL

  1. Inizializzare un oggetto SqlCeRemoteDataAccess e impostare le proprietà per la connessione.

    SqlCeRemoteDataAccess rda = new SqlCeRemoteDataAccess("https://www.adventure-works.com/sqlmobile/sqlcesa30.dll", "Data Source=MyDatabase.sdf");
    
  2. Chiamare il metodo SubmitSql, passando l'istruzione SQL o il nome di una stored procedure e la stringa di connessione al database di SQL Server.

    rda.SubmitSql("sp_ValidateData", strConn);
    

Esempio

Nell'esempio seguente viene illustrato come eseguire un comando SQL su un computer remoto che esegue SQL Server utilizzando il metodo SubmitSQL.

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

Vedere anche

Concetti

Utilizzo del metodo SubmitSQL

Guida in linea e informazioni

Assistenza su SQL Server Compact Edition