0

I'm deploying a wpf application and I'd like to know how is the best way to connect to a database.

For example after I install my application I need to set a connection string to connect with a SQL Server Express.

I read a thread that told it's possible to find SQL Servers installed on a machine, so after I find it, how do I connect it programmatically?

An another thing I thought is, there is a file which I can read an write a connection string as web.config in asp.net?

I know that there is an app.config file but after I deploy it I guess it became inaccessible!

Rodrigo de Farias
  • 657
  • 2
  • 8
  • 20
  • 1
    You probably should not have the database on the same machine as the client. Put the database in a central location, and when you distribute the application include the location of the database in the configuration file. – mason Dec 17 '16 at 14:44
  • But I am going to, I will install a sql at my client's machine, and I'll install the same application in other machines that would access this database, that will Be the "server"! – Rodrigo de Farias Dec 17 '16 at 14:49
  • 1
    SQL Server is best put on a dedicated server machine, not installed on a client's personal computer. – mason Dec 17 '16 at 14:51
  • You already stated that you have a way of getting sql instances of a machine, then all you need to do is display a user interface and let the user select one. In order to set your connection string, your connection would be to .\instance name Or if its a network connection ip\instancename – Niklas Dec 17 '16 at 15:21

2 Answers2

1

Nothing wrong with putting SQL Server, especially Express, on the same machine if it's a smallish application. There's also the 'localdb' feature. Anyway, this bit of code will find all instances of SQL Server on your network. You can then build a window to display the results, let the user pick a server, and construct a connectionstring from the results. Note that firewalls etc on the server can get in the way of this process - typically you get the server name returned but no other info. You can fix this by adding exceptions to the firewall.

 private  DataTable FindServers()
    {

        System.Data.Sql.SqlDataSourceEnumerator instance = System.Data.Sql.SqlDataSourceEnumerator.Instance;
        DataTable dt = instance.GetDataSources();
        return dt;

    }
peterG
  • 1,651
  • 3
  • 14
  • 23
0

These days database layer should be separated from application by DataService or MicroService. Which would be the data access layer.

If you dont need that level of separation and still thinking about application that connects to database directly, I would suggest to use configuration to store connection strings as you probably like to cover scenario where database is not located on same server as WPF application.

The configuration file for non-web application is called app.configand is exactly the same as web.config for web applications.

Problem with auto-finding installed sql server instaces is simple: what if there are two instances installed on machine where you start your app? How the app will know which to use?

madoxdev
  • 3,770
  • 1
  • 24
  • 39