Connect to db by a from!

Hi, I began to use C# two days ago. I've done my first application a form by which I can write information into a db. So, I would like to connect this form in two db... is it possible


Answer this question

Connect to db by a from!

  • norman_timo

    Well, what u can do is to create an extra node with the name <appSettings>

    <appSettings>
     <add key="connstring1" value="theconnectionstring" />
     <add key="connstring2" value="theotherconnectionstring" />
    </appSettings>

    Then u can retrieve all nodes within the appSettings node by the ConfigurationSettings.AppSettings property. This will return a namevalue collection that holds all the key-value information of each add node.
    For example:

    NameValueCollection nodes = ConfigurationSettings.AppSettings;
    string connstring1 = (string) nodes["connstring1"];
    string connstring2 = (string) nodes["connstring2"];

    Just like that, again this is untested so it may contain some sort of error. This is
    the way i should handle this sort of stuff. Good luck mate!


     



  • ddrakonn

    Thnak you, but I thought this solution, but the problem is that in C# string connection is declared in app.config file, in this way:

    < xml version="1.0" encoding="utf-8" >

    <configuration>

    <configSections>

    </configSections>

    <connectionStrings>

    <add name="utenti.Properties.Settings.ProvaConnectionString"

            connectionString="Data            Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\Prova.mdf;Integrated Security=True;Connect Timeout=30;User Instance=True"

    providerName="System.Data.SqlClient" />

    </connectionStrings>

    </configuration>

    so, I don't know how I can save to different variable this connection.Can you help me

    Thank you



  • Bhavish_K

    Well, even if I create two string connection the problem is How I can relate BindingNavigator to this different sources I don't understand how the BindingNavigator relates to its object.

    To better understand that I've told you, I try to explain to you the experiment I've done yesterday:

    I've added a new source as odbc object, then in the app.config I changed the db source by modifing odbc (for example first odbc was related at "Prova" db and dns was "prova",then I changed this object by relating this to another db named "Prova2"),the result was that when I run debug the form open the Prova2 db but it write new record in Prova db.

    There is some particular event that manages BindingNavigator's save button So, where I can see where save button writes its records  



  • protools

    That is possible. Not sure how you are doing it but it can be simply done with the default components that Microsoft offers within the VS.NET enviroment.

    You can also define two connectionstrings in your code, and place the connections in 2 different variables.

    Like this:

    string connString1 = "DRIVER={MySQL ODBC 3.51 Driver};SERVER=localhost;DATABASE=db1;UID=user;PASSWORD=password;OPTION=3;";

    string connString2 = "DRIVER={MySQL ODBC 3.51 Driver};SERVER=localhost;DATABASE=db2;UID=user;PASSWORD=password;OPTION=3;";

    OdbcConnection db1Connection = new OdbcConnection(connString1);

    OdbcConnection db2Connection = new OdbcConnection(connString2);

    This is just an example of an ODBC connection with a mysql database. There are much possibilities if it comes to databases. Just look on the internet for more possibilities cause there is lots to find.

    Also, when u having some problems defining the connection string then have a look on this website:

    http://www.connectionstrings.com/

    Have fun!



  • Connect to db by a from!