locked
Passing connection string from client to server RRS feed

  • Question

  •  

    Could someone please show me how to pass the database connection string for the remote database from the client application to the server?

     

    Thanks,

     

    David Carroll

    • Moved by Max Wang_1983 Friday, April 22, 2011 7:31 PM forum consolidation (From:SyncFx - Microsoft Sync Framework Database Providers [ReadOnly])
    Wednesday, April 16, 2008 2:49 PM

All replies

  • Hello David,

     

    Not sure you want but give my test try.

     

    SqlConnection conn = new SqlConnection(string.Format("server={0}; Integrated Security = false; User Id={1}; password='{2}'; ", sqlServerName, saLogin, saPassword));

    conn.Open();

    .............

     

    DbServerSyncProvider serverProvider = new DbServerSyncProvider();

    serverProvider.Connection = conn;

    .............

     

     

    Thanks,
    Leo

    Wednesday, April 16, 2008 11:36 PM
    Answerer
  •  

    if you are using Ntier, I think you can passing the info via a seperated web method ( where you can update the server provider ). but I would sugges to consider the security needs for you sysntem before take the approach.

     

    thanks

    Yunwen

    Saturday, April 19, 2008 11:18 PM
    Moderator
  • Yunwen,

     

    I will give it a try. What i actually trying to do is pass several parameters and create a connection string based on the parameters. There are multiple databases on the server that have the same schemas. I want to be able to set the database to syncronize with from the client.

     

    Thanks,

     

    David

     

    Monday, April 21, 2008 1:33 PM
  • To clarify what I am trying to do i have pasted code from the server and client. The code was created using Hermann Rösch's instructions for n-tier with WCF and C# I got from http://heroesch.blogspot.com/2008/01/microsoft-sql-server-compact-edition-35.html. I have modified it to send a parameter and i can read the parameter at the server by accessing

    syncSession.SynParameters in GetServerInfo(SyncSession syncSession). I would like to be able to access them when the NorthWindCacheServerSyncProvider is created but i cannot figure out how to make the parameters visible there.

     

    Thanks,

     

    David Carroll

     

    //client code

    private void syncButton_Click(object sender, EventArgs e)

    {

    Cursor.Current = Cursors.WaitCursor;

    // Call SyncAgent.Synchronize() to initiate the synchronization process.

    NorthWindCacheSyncAgent syncAgent = new NorthWindCacheSyncAgent();

    try

    {

    syncAgent.Configuration.SyncParameters.Add(new Microsoft.Synchronization.Data.SyncParameter("@DatabaseName", "Northwind"));

    Microsoft.Synchronization.Data.SyncStatistics syncStats = syncAgent.Synchronize();

    this.shippersTableAdapter.Fill(this.northwindDataSet.Shippers);

    this.employeesTableAdapter.Fill(this.northwindDataSet.Employees);

    this.customersTableAdapter.Fill(this.northwindDataSet.Customers);

    Cursor.Current = Cursors.Default;

    }

    catch (Exception ex)

    {

    Cursor.Current = Cursors.Default;

    MessageBox.Show("Error: " + ex.Message);

    }

    }

     

     

     

    // WCF SyncService code 

    namespace WcfSyncService {

    using System;

    using System.Data;

    using System.Collections.ObjectModel;

    using System.ServiceModel;

    using Microsoft.Synchronization.Data;

    using Microsoft.Synchronization.Data.Server;

    using System.IO;

    public partial class Service1 : object, INorthWindCacheSyncContract {

    private NorthWindCacheServerSyncProvider _serverSyncProvider;

    public Service1() {

    // I want to pass the parameter for the database name here and set the connection string based upon it.

    this._serverSyncProvider = new NorthWindCacheServerSyncProvider(DatabaseName);

    }

    Wednesday, April 23, 2008 7:37 PM
  •  

    To clarify what I am trying to do i have pasted code from the server and client. The code was created using Hermann Rösch's instructions for n-tier with WCF and C# I got from http://heroesch.blogspot.com/2008/01/microsoft-sql-server-compact-edition-35.html. I have modified it to send a parameter and i can read the parameter at the server by accessing

    syncSession.SynParameters in GetServerInfo(SyncSession syncSession). I would like to be able to access them when the NorthWindCacheServerSyncProvider is created but i cannot figure out how to make the parameters visible there.

     

    Thanks,

     

    David Carroll

     

    //client code

    private void syncButton_Click(object sender, EventArgs e)

    {

    Cursor.Current = Cursors.WaitCursor;

    // Call SyncAgent.Synchronize() to initiate the synchronization process.

    NorthWindCacheSyncAgent syncAgent = new NorthWindCacheSyncAgent();

    try

    {

    syncAgent.Configuration.SyncParameters.Add(new Microsoft.Synchronization.Data.SyncParameter("@DatabaseName", "Northwind"));

    Microsoft.Synchronization.Data.SyncStatistics syncStats = syncAgent.Synchronize();

    this.shippersTableAdapter.Fill(this.northwindDataSet.Shippers);

    this.employeesTableAdapter.Fill(this.northwindDataSet.Employees);

    this.customersTableAdapter.Fill(this.northwindDataSet.Customers);

    Cursor.Current = Cursors.Default;

    }

    catch (Exception ex)

    {

    Cursor.Current = Cursors.Default;

    MessageBox.Show("Error: " + ex.Message);

    }

    }

     

     

     

    // WCF SyncService code 

    namespace WcfSyncService {

    using System;

    using System.Data;

    using System.Collections.ObjectModel;

    using System.ServiceModel;

    using Microsoft.Synchronization.Data;

    using Microsoft.Synchronization.Data.Server;

    using System.IO;

    public partial class Service1 : object, INorthWindCacheSyncContract {

    private NorthWindCacheServerSyncProvider _serverSyncProvider;

    public Service1() {

    // I want to pass the parameter for the database name here and set the connection string based upon it.

    this._serverSyncProvider = new NorthWindCacheServerSyncProvider(DatabaseName);

    }

    Friday, April 25, 2008 1:02 PM
  • Hi,

    I want to do the same but i dont know how.
    I tried to add a webmethod to pass the database name, and declare a string DataBaseName in my Service class, and give this DataBaseName the content of the webmethod parameter but it doesn t work. I tried to put all the code of the Service() function in this webmethod but it dont work aswell ...

    If you find something let me know

    Regards

    Sam
    Thursday, May 29, 2008 2:42 AM