There's no particular problem, I just wanted to ask whther I'm doing it right or wrong.
I have connection string stored in Settings.Default.ServerConnection property on which rely all DB-related objects. I also use auto-generated TableAdapters for server-side stored procedures (SQL Server 2008 R2 Express) with Connection property set to use mentioned connection property. There is a settings form where I can setup a connection to any server on the local network (using SqlClientFactory.Instance.CreateDataSourceEnumerator().GetDataSources()) and store it in app's settings.
So I was wondering if it's the right approach of doing such thing or not because when app is executed for the first time it's still able to connect to SQL server without even configuring the connection (Data Source set to (local) by default).
If I'm understanding the question... :-)
Your app is able to connect to local because that is the how its bound in its current environment (say it was a remote IP, it wouldn't work).
Storing connection string information in the config file (e.g. app.config) is a great idea, because it can be changed on the fly without a rebuild.
Related
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!
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;
}
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?
I have a windows form application (developed in VS Express 2013) that connects through an instance of SQL Server 2012 to a database. Both the application and database are on my local system; I just needed a GUI to more easily interface with this very large database that stores my research data. When I initially compile and deploy the application, it works fine and has no connection problems with the database. However, if I then attach the database in SQL Server Management Studio (which I sometimes want to do) I get an error the next time I try to use the application - "Cannot open database ..... requested by the login, the login failed". I get this error even if I take the database offline and detach it before quitting SSMS. And just to be clear - I'm not making any changes to the database in SSMS, I'm just looking at the data. The connection string used by VS is Data Source (LocalDB)\v11.0;AttachDbFilename=|DataDirectory|\CollectionMetricsDatabase.mdf;Integrated Security=True.
Any idea what the problem is?
Maybe the instance of SQL Server 2012 has a max number of concurrent connections and you exceed that with your two connections at once (SSMS connection and application's connection). Check the settings in SSMS.
Another thing to try - Based on your connection string, you are using Integrated Security, which in my experience means the application connects using Windows credentials. That can be fragile, because it means that whatever 'user' runs the application (if hosted in IIS, this will be the App Pool user) is the one whose Windows credentials are used. I would suggest creating a SQL login user (in SSMS) for the database, with db_owner access, and then changing the application's connection to be username/password based instead. You may need to enable Mixed Authentication for the SQL Server instance in SSMS if it doesn't already allow it.
You need to connect in SSMS with the localDB connection string. Attaching it will prevent it from working with localDB.
In SSMS, create a connection to (LocalDB)\v11.0. You should see your DB there already.
The problem is that you are connecting to the database file directly not through SQL Server.
Only one user can have a lock on the file at the time.
You need to recreate a connection string for your app.
That is why you were needing to detach the database.
Since this is only a personal project you could live with things as they are, at least you know the server is only running when you need it.
create connection msdn
here is the problem I am facing now. I have created an application that uses local database (this was created by Add -> New Item -> Local Database. Afterwards I have added tables under this .sdf database.
Then I have connected to this database using Add -> New Item -> ADO.NET Entity Data Model.
Everything works like a charm, unless I was asked to move this database to a place, where multiple people could access this database and work with it.
Therefore, as I have no previous experience with databases, I have treated this .sdf file as any other file (let's say Excel workbook) and I thought that I could simply take already existing database, copy it on server computer (e.g. \Server001\Database\Database1.sdf) and simply change connection string under app.config.
However the problem is that this does not work. As I didn't know how to change connection string, I created new application, where I have tried to connect to this database located on a server computer; however I received the following error:
SQL Server Compact does not support opening database files on a network share.
I already have fully functioning program, but I have no idea how to make it work with multiple users. I have tied to google for solution, but all I could find is how to create local database, not how to make it accessible by placing it in server computer.
Could you guys please help me? If you need more details, please let me know!
P.S. This is WPF application, .NET 4.5, created using Visual Studio 2012 Professional.
Thank you!
The error message pretty much sums up the problem: SQL Server Compact does not support opening database files on a network share.
SQL Server Compact (aka "local database") is to be consumed by a local application; even if it was a web app serving many requests, the application itself is local.
If you want to have multiple remote connections (i.e. centralized DB, distributed app), you should look at using an instance of SQL Server (any SKU would probably work, even SQL Server Express). Those will use MDF files instead of SDF files, so you might want to refer to Convert .sdf database to .mdf database. You'll probably also need to set up a user identity for your connection string, so check out this link on CREATE USER and Difference between a User and a Login in SQL Server to understand how that can be configured.
I have developed a winform application in C#.net and using SQL Server 2008.
My application inserts and updates values into database.
Is it possible to install the application on a another system which doesn't have sql server on it?
Imagine using sql azure. Cloud based sql server. The database is never on the same physically computer. It's all down to the connection string.
With an on premise database you need to make sure the database allows external connections, maybe opening up firewall etc. then make sure the connection string is set correctly on the application to talk to external database.
You can even configure to change the connection string as required pointing at different databases depending on the individual requirements.
Scott
Yes, you can. You can access the DB remotely by referring remote DB server in connection string.
It is possible, if you are looking to run the application on machine that is running on the same Domain as the SQL Server and has privileges to the SQL Server.
I'm trying to develop multi client in C# with SQL Server 2008 data Base, after made setup file and install it to client I cannot access DataBase in server. I used LINQ to Connect DataBase and have App.config xml file in my project which ConnectionString Declare there and I after installing the application I change that XML file's ConnectionString to right way(Server name and sql instance name), but this change doesn't have any impact, whats the problem maybe have my solution?
If any one has experience about developing multi client application share with me.
and also How can I change scope in project properties from Application to User, It's may be solve my problem
Thank you
In this project we use LINQ to connect Database to server, and we can get right Database address dynamically from User Interface and use it in constructor of DataContext:
public DataClasses1DataContext(string connection)
And then it don't care in embed connection string, its use only that connectionString in their constructor.
If the error mentions "provider: Named Pipes Provider", then your application is trying to connect via named pipes to your SQL Server, is that what you want? You can prefix the server name with tcp: to force TCP/IP usage instead i.e. Server=tcp:ServerName if you wish to use TCP.
If you want to use named pipes it is possible that support for it is turned off on the server, I believe it is disabled by default. You would need to enable it via the SQL Server configuration manager program on the server, in the network configuration, protocols section.
hmmm, hard to say without the connection strings or error message, but some ideas....
When you change the app.config, restart app to verify changes.
Try pinging the server from the where the app is being installed.
Check firewall settings. shared network.
Use your new connection string on your development machine on the same network if local.