Jaa


How to: Submit SQL Statements to a Remote SQL Server (Programmatically)

You can submit SQL statements directly to a computer that is running Microsoft SQL Server by using the SqlCeRemoteDataAccess object.

To submit an SQL statement

  1. Initialize a SqlCeRemoteDataAccess object and set the properties for the connection.

    SqlCeRemoteDataAccess rda = new SqlCeRemoteDataAccess("https://www.adventure-works.com/sqlmobile/sqlcesa30.dll", "Data Source=MyDatabase.sdf");
    
  2. Call the SubmitSql method, passing in the SQL statement or name of a stored procedure and the connection string to the SQL Server database.

    rda.SubmitSql("sp_ValidateData", strConn);
    

Example

This sample shows how to run an SQL command on a remote computer that is running SQL Server by using the SubmitSQL method.

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

See Also

Other Resources

Using the SubmitSQL Method

Help and Information

Getting SQL Server Compact Edition Assistance