c# Connecting to a local MDF DB File - c#

On my development computer I have MS SQL Server/Visual Studio 2005. My program can correctly connect to my local DB and use it. However my other computer (non-dev) does not have MS SQL Server/Visual Studio 2005 and does not connect to the DB. It spits out the following:
"An error has occurrred while establishing a connection to the server. When connecting to SQL Server 2005, this failure may be caused by the fact that under the default settings SQL Server does not allow remote connections. ..." (Error: 26).
Does this mean I have to install SQL Server 2005 on my non-dev computers? Is there any other way?
My connection string is:
"Data Source=.\SQLEXPRESS;AttachDbFilename=\""
+ Directory.GetCurrentDirectory()
+ "\DB.mdf\";Integrated Security=True;User Instance=True";

Your connection string is telling the Sql Server Native Client ADO.NET Provider to attempt to connect to a Sql Server instance named SQLEXPRESS that will manage the database stored in a file DB.mdf. Since your client computer does not have Sql Server Express installed, it's not going to find a database to connect to.
You will need to:
Install Sql Server on the client computer and deploy your database there.
Switch to Sql Server Compact Edition (SqlCE - embedded database) and re-architect your application to use the portable database file (with SqlCE) instead.
Ditch using a robust database engine and switch entirely to ADO.NET DataSets, saving/loading the contents of the DataSet to an Xml file (via WriteXml() and ReadXml()). If the amount of data you are processing is fairly limited in size, DataSets are a good approach to maintaining integrity (via a strongly typed and well-defined schema) and portability.

If you want to run it on that other pc/server you also need to have SQL Server (need to attach your .mdf to it) or SQL Server Express installed.
Another trick would be to change your connection string so that it points out to another pc/server where your database runs on.

Yes, you do need MS-SQL (Express) available on your target computers. Either a local install or a connection to a server.
It is not that difficult to include SQl Express in a Setup.exe (see PreRequisites).
An alternative is to use SQL-CE or Sqlite or (even) MS-Access. They are 'embedded' database engines so that you only need to distribute DLLs.

Related

Connect to local database on other computers

How can I use my LocalDB on other computers?
SqlConnection connection = new SqlConnection(#"Data Source=(LocalDB)\v.11.0;AttachDbFilename=[DataDirectory]\Database1.mdf;Integrated Security=True");
I tried this but it doesn't work because I get this error:
Local Database Runtime error occurred. Cannot create an automatic instance
Does someone know how to fix this, or what I can do instead?
I don't want to use SQLServer because I am making a program for an offline computer.
The fact that your app is for an offline computer is irrelevant. If you're attaching an MDF file on demand then that will only work with a SQL Server instance installed on the local machine anyway.
As suggested elsewhere, LocalDB is intended for development use only. Your users should be installing SQL Server Express and you can specify that in the connection string. If you're going to use a SQL Server data file, i.e. an MDF file, then you need a SQL Server instance to attach it to. If you don't want to have to have a SQL Server instance installed then don't use a SQL Server data file in the first place.
LocalDb is sql server and is only intended for development purposes. It isn't something you would use for distributing an application. SQL Express is an option for you, but may be overkill for what you need. You might look into SQLite as you might find it easier to embed and distribute with your application. Of course, the right answer depends on your actual requirements, etc.

How to bake in the databases from SQL Server to C#

I'm very very new to SQL Server, and I have written a program in Visual Studio using C# that uses a table from a database that I have created using SQL Server Management Studio. But if I try to run my program on another device, it either asks for a version of SQL Server to be installed or it fails to connect to the server because I used the local option to create the said database and therefore, the other device doesn't have the permission to connect to it.
I have read somewhere that I should use in-memory databases so that other devices can run this program without connecting to me or needing SQL Server to be installed? Can I store the database somewhere in the project and tell my program to seek it and load it?
TLDR; how to use the features of SQL Server like database and tables while offline and without the need to connect to any server or even the need to install SQL Server itself if possible.
And sorry for my bad English!

Can I access sdf (SQL Server Compact database file) kept at some other computer on the same network?

I have done so for SQL Server, now I was trying to do it SQL Server Compact.
I am unable to make a connection string in SQL Server Compact which has the provision for providing ip/server.
Try Replicating Data to a remote server.
http://msdn.microsoft.com/en-us/library/ms152568(v=sql.105).aspx
SQL Server Compact is just not designed to be used in a network. It's designed as a local-only cache/database store. It was a design decision made by its authors ... if you need something that works for multiple users in a network - use the real SQL Server (Express or any other edition) (by marc_s
It is also considered bad idea to expose SQL server to network - you may want to redesign your application to have service exposing limited functionality to users and calling DB (in this case you can use local SQL compact, but still consider other editions that are more appropriate to general data storage).
If SQL Compact is requirement - replicate data to local file - MSDN:Replicating Data to SQL Server Compact.
For more information on Sql Compact connection strings - Connecting C# to SQL Server Compact database

How to run desktop application with its Sql Server database on a machine that have not SQL server installed

I have already finished a desktop application using Visual Studio 2010 and SQL Server 2008.
it's working perfectly on my machine (i have a SQL Server 2008 and 2005 ).
the database is local and unchangeable,
i want this application to be run successfully on the machine that doesn't have SQL Server installed.
my Connection string is:
Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\KBank.mdf;Integrated Security=True;User Instance=True;Connection Timeout=300
I want to know the best and simplest way to make it. thanks
The simplest way would be to create a setup for your application that states SQL Server 2008 as a prerequisite. That's it.
For further help you would have to supply more information on how you use the database in your application and if you have a way to host a public sql server instance to which your application will connect.
If you're using the AttachDbFilename=|DataDirectory|\KBank.mdf;User Instance=True approach, then you have no choice but to install SQL Server Express (and no other edition!) locally on that / every machine that is supposed to use your application.
This approach is severly flawed in my opinion, and it limits your flexibility.
What I'd suggest is to use the real server approach: put your database onto a server (both on your development environment, and in production) and then you have the flexibility of having either a SQL Server instance on every user's machine (if that makes sense), or you can have a centralized server which the clients only connect to (no local database server installed).
If the database is "local and unchangeable" how about the compact edition? That way your clients dont need to install a full sql server instance
I agree with Nick. if you dont want to install SQL Server on the machine then SQL server compact edition would be one of the option. Syntax is pretty similar to SQL Server but it comes with limitations. like you can not have a stored procedures in compact edition and few more.
You might want to visit SQL CE Tutorial as a development resource.

SQL Server database or database on SQL Server

I'm writing windows application which will be used on one computer. I like to do with SQL server, retrieve data with stored procedures, and so...
My question is, what is the difference between SQL server database (in file) and standard db on SQL server, because I don't want to install SQL server on client's PC just for one app. Can be this SQL server DB used with stored procedures, or is there other way?
Thanks.
If you don't want to install a full SQL Server engine on the client machine you could use an embedded database such as SQL Server Compact or SQLite which are designed for those scenarios.
You may want to look at SQL Server Compact, SQL Server Express or something like SQLite. I don't think any of these really offers a way to run a database engine without installing something or requiring something else to be installed (e.g. you could use AttachDbFileName method with SQL Server but this relies on VS/Express to already be there).

Categories

Resources