如何从位于Remote的不同数据库中获取数据 [英] How to fetch data from different database located in Remote

查看:118
本文介绍了如何从位于Remote的不同数据库中获取数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Hello Everyone,我想合并位于Remote的不同数据库中的数据并具有相同的结构。我没有任何想法可以帮助或建议我。

解决方案

创建链接服务器,构造SELECT语句以从不同数据中选择数据source然后在SELECT语句之间使用UNION子句来合并输出。


在你的web配置中添加连接字符串部分



 <   connectionStrings   >  
< add 名称 = ConnectionString connectionString = 数据源=服务器的公共IP;初始Cata log = Db名称; Pooling = False;用户ID = id;密码=密码; providerName = System.Data.SqlClient / >
< / connectionStrings >





现在用你的c#代码



  string  ConnString = ConfigurationManager.ConnectionStrings [  ConnectionString]。ConnectionString; 





 使用(SqlConnection SqlConn =  new  SqlConnection(ConnString))
{
S. qlCommand sqlCmd = new SqlCommand( Select * From table ,SqlConn);
DataTable dtSource = new DataTable();
使用(SqlDataAdapter da = new SqlDataAdapter(sqlCmd))
{
da.Fill(dtSource);
}
}





以上代码从连接字符串中提到的数据服务器的表中获取数据。



您可以在Web配置文件中提供多个连接字符串,并再次使用上面的代码从不同的数据库(服务器)获取数据



快乐编码!! :)



谢谢,

Vj


首先你必须知道IP服务器或您也可以使用服务器名称,然后准备如下连接字符串: -



服务器=  192  168  10  20 ; Database = myDataBase; User Id = myUsername; 
密码= myPassword;





然后使用此连接字符串从此远程数据库服务器获取数据如下: - < br $>


  public  DataSet GetData()
{
DataSet myDataSetset = new DataSet();
string strConnectionString = 编写连接字符串这里;
string strQuery = 选择*来自人;
使用(SqlConnection connection = new SqlConnection(strConnectionString))
{
SqlDataAdapter dAdapter = new SqlDataAdapter();
dAdapter.SelectCommand = new SqlCommand(strQuery,connection);

dadapter.Fill(myDataSetset);
}

return myDataSetset;
}







以上方法将从远程服务器获取数据并返回给你数据集中的数据。



希望这会有所帮助并满足您的需求。

如果您需要任何进一步的帮助,请告诉我们此


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.

解决方案

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.


Add connection string section in your web config

<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

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



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


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 :-

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 :-

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.


这篇关于如何从位于Remote的不同数据库中获取数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆