Click here to Skip to main content
15,894,720 members
Please Sign up or sign in to vote.
3.00/5 (1 vote)
Hello Everyone, I want to merge data from different database located in Remote and having same structure. I don't have any idea can anyone help or suggest me.
Posted
Comments
Kornfeld Eliyahu Peter 9-Jun-14 6:16am    
You may use Google and search for 'SQL Linked Server'...
Gopal Rakhal 9-Jun-14 6:20am    
Thanks i will search for it.
SRS(The Coder) 9-Jun-14 9:16am    
Whether you want to do the same through ASP.Net or just you want to know what are the methods you can prefer for it ?

Also please clarify if you have already idea on database connectivity through .Net or you are a purely fresher to it ?
Gopal Rakhal 10-Jun-14 1:05am    
Actually I want to know methods for same and if I go for asp.net to do same how can I do.

Create Linked Server(s), construct the SELECT statements to select the data from the different data sources and then use the UNION clause between the SELECT statements to merge the output.
 
Share this answer
 
Add connection string section in your web config

XML
<connectionStrings>
    <add name="ConnectionString" connectionString="Data Source=Public ip of the server;Initial Catalog=Db name;Pooling=False;User ID=id;Password=password;" providerName="System.Data.SqlClient" />
</connectionStrings>


Now in your c# code

C#
string ConnString = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;


C#
using (SqlConnection SqlConn = new SqlConnection(ConnString))
                {
                    SqlCommand sqlCmd = new SqlCommand("Select * From table", SqlConn);
                    DataTable dtSource = new DataTable();
                    using (SqlDataAdapter da = new SqlDataAdapter(sqlCmd))
                    {
                        da.Fill(dtSource);
                    }
}


The above code gets data from a table from the data server mentioned in the connection string.

You can provide multiple connection string in your web config file and use the above code again to get the data from different databases(servers)

Happy Coding!! :)

Thanks,
Vj
 
Share this answer
 
First of all you have to know the IP for the server or you can also use the server name as well, then prepare the connection string like below :-

C#
Server=192.168.10.20;Database=myDataBase;User Id=myUsername;
Password=myPassword;


Then to fetch data from this remote database server using this connection string is as follows :-

C#
public DataSet GetData()
{
	DataSet myDataSetset=new DataSet();
	string strConnectionString = "write your connection string here";
	string strQuery = "Select * From Person";
	using (SqlConnection connection = new SqlConnection(strConnectionString))
	{
  	SqlDataAdapter dAdapter=new SqlDataAdapter();
  	dAdapter.SelectCommand=new SqlCommand(strQuery, connection);
  
  	dadapter.Fill(myDataSetset);
	}

	return myDataSetset;
}



This above method will fetch data from the remote server and return you the data in DataSet.

Hope this will be helpful and fulfils what you want.
Please let me know if any further help you need on this.
 
Share this answer
 
Comments
Gopal Rakhal 10-Jun-14 3:12am    
Is this above code is for working in LAN?Here remote is for server located not in LAN, different place say different district of state.
SRS(The Coder) 10-Jun-14 7:11am    
Then seriously you have to create a Web Service which will be hosted on that server.
Then call that web service to get the data from that server database.

OR see this will help :-
http://www.techrepublic.com/forums/questions/how-can-i-access-my-server-from-outside-the-lan/
SRS(The Coder) 10-Jun-14 7:20am    
You need a VPN connection at least to achieve this.
go through the below link I hope this will help you

http://stackoverflow.com/questions/15236961/sql-merge-to-remote-linked-server-table[^]


Thanks,
-RG
 
Share this answer
 
v2

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