Build a universal connection string - c#

I developed a win form based application which involves database. I built the database in visual studio inside that solution. My current connection string in app.config file is:
connectionString="Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=|DataDirectory|\NARA.mdf;Integrated Security=True;" providerName="System.Data.SqlClient"/>
But to check it I copied my Debug folder which included all files (including database) but it gave connection string exception on my other system. How can I make a connection string which can work on any system I take my program to.

All of your problems are caused by incompatibility between Sql Server versions.
When you install the 2012 LocalDB it has a default instance named v11.0. So, if you want to connect to this default instance and let it manage your MDF file you need a connection string with
connectionString="Data Source=(LocalDB)\v11.0;AttachDbFilename=....."/>
or you can create an instance with the name required by your connection using an administrative command prompt and typing
SqlLocalDb c MSSqlLocalDB
But this is not the end of the problems, because now you need to have an MDF file created with Sql Server 2012 version (localdb or not). And, as clear from the error message received, you have an MDF file created with Sql Server 2016.
The 2012 cannot read file created with 2016. This has always been the case with Sql Server (and logically so) because new versions of the MDF file always contains enhancements or internal changes that create this incompatibility.
The fix is simple: Install a version of your LocalDB bits that are compatible with the MDF file you distribute. (Remember that LocalDB is a developper facility and shouldn't be used in production albeit in simple scenario it can works, for example I use it to distribute a demo of my full scale application. So a customer with a poor IT department can evaluate the product without being forced to install the Sql Server Express o the full version)

Related

Connect Windows Forms application with SQL Server Express

I have created a Windows Forms desktop application using Visual Studio 2019. I also created a database using SQL Server Management Studio. I connected the database with my application with connection string
"Data Source=CYBERCELL\\SQLEXPRESS;Initial Catalog=Details;Integrated Security=True;"
The application is running without any error. I published this application using Visual Studio 2019 publish wizard and installed in another computer. But I can't understand how to use and connect the database with the application in that system.
I am looking to install SQL Server in every computer. But if I install SQL Server, the server name will be different.
If you're locally installing SQL Server Express on each individual computer, you could use
.\SQLEXPRESS
or
(local)\SQLEXPRESS
as your server/instance name - this is the same on every computer. The dot . or (local) (in parenthesis) refers to the local computer, and SQLEXPRESS is the default instance name for any SQL Server Express installation.
I once stuck with this same scenario... and this is how I solved the problem,
You don't have to install SSMS or SQL server on any of your client's pc, if you follow these steps
You have to create a localDB on your application and make sure the application will connect to that localDB.(if you don't know how to connect to local db, here => Connect to LocalDB. And make sure to create tables and procedures as same as your previous database.
Follow this answer(ticked) with code(connection string)
After this, you have to install Advanced Installer to create an installer for your application (the best choice I ever chose).
Then Follow this to create an installer where you make sure to add a prerequisite of localDB (say localdb 2019) installer of your current version.
This makes your application run on any pc and only requires localdb as a prerequisite to be installed which also comes with the installer.
Make sure to leave a message not to delete localdb on the application installed location.

Deploying LocalDB with Entity Framework - won't use existing database

I built a code-first Entity Framework application in C# and first deployed it successfully on my development machine.
Now I've redeployed the application to a non-dev machine (running the same OS - W7x64) - I also installed localdb. But it doesn't work.
When the app runs it crashes with the error
Cannot create file 'E:\Temp\WebUtility\WebDB.mdf' because it already exists. Change the file path or the file name, and retry the operation.
CREATE DATABASE failed. Some file names listed could not be created. Check related errors.
it cannot create the database because the file already exists. But I WANT the app to use the existing database, not create a new one! If I remove the existing database files, the app does start up and proceeds to create new empty database files which is no good.
And if I replace these newly created files, it's unhappy again, so clearly it doesn't believe the database it's been deployed with is valid.
My connection string is as follows:
<connectionStrings>
<add name="ModelContainer"
connectionString="Data Source=(LocalDB)\MSSQLLocalDB;attachdbfilename=E:\Temp\WebUtility\WebDB.mdf;integrated security=True;connect timeout=30;MultipleActiveResultSets=True;"
providerName="System.Data.SqlClient" />
</connectionStrings>
What am I missing here?
It turns out the version of SQL is slightly different - VS installs SQL Express 2016 SP1, I installed 2014 on the deployment box.
Note - simply uninstalling this and reinstalling the 2016 version doesn't work, you need to manually delete the keys from HKEY_CURRENT_USER\SOFTWARE\Microsoft\Microsoft SQL Server\UserInstances

how to deploy localdb database with app

I am ready to finalize a winforms add in I have created for Excel which uses Add-In Express to publish.
This add in uses a sql server localdb database with mdf file.
My primary question is how to deploy the mdf file to target machines. So far, I have manually copied the mdf file to test machine, but when testing the add in it fails to connect to the file (localdb is installed).
Error message:
Exception Source: .Net SqlClient Data Provider
Exception Type: System.Data.SqlClient.SqlException
Exception Message: Cannot open database "MyDatabase" requested by the login. The login failed.
Is it even possible to just drop the mdf onto machine and everything work? If so, is there a guide somewhere that could help. Or do I have to generate the sql scripts to create the database locally? If I do have to generate the SQL scripts, is there a way to roll this up in the Click-Once deployment approach, or I do I need a seperate process?
This is how you deploy a WinForms application with LocalDB
You have to create a localDB on your application and make sure the application will connect to that localDB.(if you don't know how to connect to local db, here => Connect to LocalDB. And make sure to create tables and procedures as same as your previous database.
Follow this answer(ticked) with code(connection string)
After this, you have to install Advanced Installer to create an installer for your application (the best choice I ever chose).
Then Follow this to create an installer where you make sure to add a prerequisite of localDB (say localdb 2019) installer of your current version.
This makes your application run on any pc and only requires localdb as a prerequisite to be installed which also comes with the installer.
Make sure to leave a message not to delete localdb on the application installed location.

How to Deploy "SQL Server Express + EF" Application

It's my first time to Deploy an Application which uses SQL Server Express Database.
I'm Using Entity Framework Model First to contact Database.
and i created a Setup wizard with Install Shield to Install the App.
These are Steps that I'v done to Install The Application in Destination Computer :
Installing MS SQL Server Express (DEST)
Installing The Program using Setup file (DEST)
Detach Database from SQL server and Copy Related .mdf ,.ldf file to the Destination Computer.
Attach database file in destination computer using SQL Server Management Studio.
I know server names and SQL name Instances are different and my program can't run correctly with the Old Connection String.
I'm Beginner at this, and I want to know what should I do in the Destination Computer to make the program run?
should I find a way to change the connection string on runtime?!
or is there any way to modify installshield project and it do this work for me? (installshield is professional edition)
could you suggest me what to do?
in my searches I saw that WiX can do this, but I find it complicated, and i don't have enough time to learn it. i need to deploy the app ASAP.
Thanks alot.
Few hints for using LocalDB in your project:
Download SQL Express LocalDB 2014 here. You can install it silently with single command like this
msiexec /i SqlLocalDB.msi /qn IACCEPTSQLLOCALDBLICENSETERMS=YES
Include your .MDF in your VS project and set in properties to Copy if newer so that it gets copied to your bin folder during build so that it is automatically included in the installer.
At your app startup (in app.cs) check, if database file exists in desired location (e.g. %PUBLIC%\YourApp\Data) (WPF Desktop Application with MDF used locally for all local users). If not, copy the .mdf file from your app install dir to your data dir.
Modify app.config so that your connection string looks like:
<add name="MyContextLocalDB" connectionString="Server=(localdb)\MSSQLLocalDB; Integrated Security=True; AttachDBFilename=|DataDirectory|\MyDatabase.mdf; Connection Timeout = 30" providerName="System.Data.SqlClient" />
Connection timeout should be increased, since LocalDB exe is launched when you first try to connect to it.
You can also use Database.CreateIfNotExists, but I have never tried it.

Add *.mdf file to C# Project

I'm using Visual Studio 2005. I create a project, not Web Project, just Windows application.
I remember that Access Database File can be added into a project. I don't need connection to server, data can be retrieved. And I want to do the same thing with SQL Database file.
I did the following steps:
Right-click on project.
Choose Add An Existing Item
Browse for *.mdf file.
DataSource Config Wizard appears and it displays this Message
An error occurred while retrieving the information from the database:
Failed to generate a user instance of SQL Server due to a failure int starting the process for the user instance. The connection will be closed.
I need help to add mdf file into my project.
To start with, and MDF file can be read only by an instance of SQL Server. If you deploy MDFs, then your application must either connect to a SQL Server provided by your end-user during setup, or it must deploy its own instance, in the later case a SQL Server Express Edition instance. See How to: Install SQL Server Express. With Visual Studio 2008 you can add a prerequisite to your own application setup MSI, see "Installing" the SQL Server 2008 Express ClickOnce Bootstrapper for Visual Studio 2008 SP1.
A second issue is that, despite the wide belief of the contrary, distributing the MDF alone without the LDF can land you into a world of pain. You can end up distributing an inconsistent MDF that needs the LDF to finish recovery and get into a consistent state.
But a more serious issue is your plan to deploy binaries (MDFs) instead of scripts for database deployment. This is doomed to fail. As soon as you'll plan to release v. 1.1 of your application you'll face the non-trivial problem of how to replace the user MDF (which now contains data added by the user) with your new MDF. This is why is much much better to deploy upgrade scripts always, and forget about the MDF in your project.
You can read from an Access file (*.mdb) in your app without any other requirements because the core Jet engine used by Access is included as part of Windows — it's built in. Sql Server is not included as part of Windows, and so you cannot use an *.mdf file in your app unless Sql Server has been installed and you have appropriate permissions for it.
It is possible to distribute either Sql Server Express Edition or Sql Server Compact Edition (recommended) with your app. Another option is SqlLite, which has a fully-managed database engine available.
An .MDF is a sql server DB, not MS Access. MS access is .MDB. You cannot read an .MDF on its own. It needs a log file (.LDF) as well. If you attach it to your local instance, it will create a new one for you. You can then connect to that DB.
To solve deployement problem (Updated version of your .mdf file and Code), you can have a utility in your application which can create .xls file of every table(Backup your database) which you used in your application. Now you can easly import those .xls file in SQL Server and create new version of .mdf file and attach same file in latest code.Now new release of your app ready to deploye..!

Categories