Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Create an App.Config file and add the following in the appsettings:</p> <pre><code>&lt;appSettings&gt; &lt;add key="appDSN" value="data source=SERVER-NAME;initial catalog=StudentDB;integrated security=SSPI;persist security info=False;packet size=4096" /&gt; &lt;add key="appDSN2" value="data source=SERVER-NAME2;initial catalog=EmpolyeeDB;integrated security=SSPI;persist security info=False;packet size=4096" /&gt; &lt;/appSettings&gt; </code></pre> <p>The key is the name you give to your setting, the value is in this case the connectionstring</p> <p>and use the following when connecting to retrieve data from both databases:</p> <pre><code>// // In a using statement, acquire the SqlConnection as a resource. // using (SqlConnection con = new SqlConnection(ConfigurationSettings.AppSettings["appDSN1"])) { // // Open the SqlConnection. // con.Open(); // // The following code shows how you can use an SqlCommand based on the SqlConnection. // using (SqlCommand command = new SqlCommand("SELECT * FROM Students", con)) using (SqlDataReader reader = command.ExecuteReader()) { while (reader.Read()) { //read through the first database } } } // // In a using statement, acquire the SqlConnection as a resource. // using (SqlConnection con = new SqlConnection(ConfigurationSettings.AppSettings["appDSN2"])) { // // Open the SqlConnection. // con.Open(); // // The following code shows how you can use an SqlCommand based on the SqlConnection. // using (SqlCommand command = new SqlCommand("SELECT * FROM employees", con)) using (SqlDataReader reader = command.ExecuteReader()) { while (reader.Read()) { //read through the second database } } </code></pre> <p>This is the easy and old way using sqlreader. Using mahdi tahsildari way (Linq to SQL) is more of today.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload