简体   繁体   中英

How to loop data from a table of a database from another server?

I need to get data from a table on a database on a server which I have access to. This server is on a different area with a public IP I can use. I'm not really sure what I will be needing. It's like I need to do a CRUD GET method and return table rows. Any suggestions what I can do?

EDIT: I am using Winform and C# not ASP.NET

If I understood well your question, you need to read a table from another Database server. If the servers are MS SQL, the easiest way is to add a connection between the servers, in SQL is a Linked Server. See this link https://msdn.microsoft.com/en-us/library/aa560998.aspx

When a DB Sql Server is linked with another, you will have the possibility to access all the objects of the another server like tables, procedures and views.

The think you have to make attention is with transactions blocks inside stored procedures, in the server 1, calling objects from the server 2 (remote server/server linked). Is not possible to control objects from another's servers

Another way is develop an application to read the table you need and return a class with the result.

It will depend on what you're trying to achieve as to the best solution. The two options that would be most appropriate are...

Reading data in c# to use within an application, as Greg says, put a connection string in you're app.config to the database and then use any of Entity Framework, Enterprise Library or simple .Net data library to read with relevant SQL.

If you've got two databases involved, one within you're network and are accessing the other one on a different network you could use multiple connection strings in the app.config or you might want to consider setting up a Linked Server within SQL Server Management Studio to the other one which would allow you to prefix the table appropriately in the SQL from the more local database connection. You could even use synonyms to point to the more remote table if appropriate.

If what you're asking is how to access a database through your c# application then sounds like you'll need to create a connection string in your app.config. Check out this article , it's pretty useful.

<connectionStrings>
  <clear />
  <add name="Name" 
   providerName="System.Data.ProviderName" 
   connectionString="Valid Connection String;" />
</connectionStrings>

Hope this helps!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM