Click here to Skip to main content
15,891,607 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
this is imran khan.i face a problem in accessing database from two database server on a single aspx page so plz. provide a solution for this problem.
example: if we have a page "Index.aspx" than we want to access database from two tables which resides on diff. database server.
so plz. provide a solution how this possible....
Posted
Updated 22-Jan-10 5:00am
v2

You need to establish two connections - one to each server.
 
Share this answer
 
Define 2 connection strings in your web.config file for the different database servers.
XML
<connectionStrings>
        <add name="connString1" connectionString=..../>
        <add name="connString2" connectionString=..../>
</connectionStrings>


Use corresponding connection string in your application like this:
String connString1 = ConfigurationManager.ConnectionStrings["connString1"].ToString();
String connString2 = ConfigurationManager.ConnectionStrings["connString2"].ToString();


Hope it helps!!!
 
Share this answer
 
Hi, you should make two connections & as well as below

sample code below
public SqlConnection con1;
public SqlConnection con2;
SqlDataAdapter Adp1;
SqlDataAdapter Adp2;
SqlCommand Cmd1 = new SqlCommand();
SqlCommand Cmd2 = new SqlCommand();

con1 = new SqlConnection("Connection1ConnectionString");
con2 = new SqlConnection("Connection2ConnectionString");

con1.Open();
con2.Open();

Cmd1.Connection = con1;

DataTable dtData1 = new DataTable();

Adp1 = new SqlDataAdapter(Cmd1);
Adp1.Fill(dtData1);

Cmd2.Connection = con2;

DataTable dtData2 = new DataTable();

Adp2 = new SqlDataAdapter(Cmd2);
Adp2.Fill(dtData2);

GridView1.DataSource = dtData1;
GridView1.Databind();
GridView2.DataSource = dtData2;
GridView2.Databind();
please let me know your feedback.
 
Share this answer
 
For Every database you would have a separate ConnectionString, and if you want to access both at a time, you need to use Two object of SqlConnection and Open them simultaneously.

:thumbsup:
 
Share this answer
 
use 2 different sql connections and there will be no problem use code of other guys have send for this question
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900