Connection string C# Entity Framework SQL Server Express - c#

I am using C# WPF EF6.0 and SQL Server Express for final program.
I have created a setup file with Advanced installer that creates database via script and SQL Server Express in
C:\Program Files\Microsoft SQL Server\MSSQL11.SQLEXPRESS\MSSQL\DATA
and I have changed my string connection from
<add name="MyDBEntities"
connectionString="metadata=res://*/MyDBModel.csdl|res://*/MyDBModel.ssdl|res://*/MyDBModel.msl;provider=System.Data.SqlClient;provider connection string="data source=(LocalDB)\v11.0;attachdbfilename=|DataDirectory|\MyDB.mdf;integrated security=True;connect timeout=30;MultipleActiveResultSets=True;App=EntityFramework""
providerName="System.Data.EntityClient" />
To this
<add name="MyDBEntities"
connectionString="metadata=res://*/MyDBModel.csdl|
res://*/MyDBModel.ssdl|
res://*/MyDBModel.msl;
provider=System.Data.SqlClient;
provider connection string="
Data source=.\SQLExpress;
integrated security=True;
MultipleActiveResultSets=True;
Initial Catalog=MyDB;
App=EntityFramework""
providerName="System.Data.EntityClient" />
I have installed .net Framework 4.5 and SQL Server 2012 sp1 v11.0.300 that I have used in Visual Studio 2013
When I install my program, everything works, but when I run my program it stopped working at first launch. I think the connection string has a problem.
If I click debug in Visual Studio, I see this error
An unhandled exception of type 'System.InvalidOperationException' occurred in EntityFramework.dll.Additional information: No connection string named 'MyDBEntities' could be found in the application config file.

The reason is that you have not attached the database files to the local database engine. try to use the appropriate SQL Server methods which are available. Your application (after installation) can not find the database in the local DB engine.

Finally I couldn't create a setup file with Advanced Installer.
But I created it by Install Shield.
My problem was insert or delete or edit from database.
I installed my program in other drive and my problem solved.
The reason was a joke that program files in C is readonly.

Related

how to run an application with database in c# on another computer

hi please if you can help me with this problem
I have a application with database in C# and only run on the computers where I have installed Visual Studio on the other computers the app run but not find the database. The database is Microsoft SQL Server Database File (SqlClient)
in app.config i have this settings
<connectionStrings>
add name ="db" connectionString="Data Source= > >(LocalDB)\MSSQLLocalDB;AttachDbFilename=C:\util\service.mdf;Integrated Security=True;Connect Timeout=30"/>
</connectionStrings>
the path of the database in the other computer is correct set
You cannot use the .mdf file without a SQL Server engine. It works on your Visual Studio machines, because it comes with SQL Server Express.
LocalDB cannot be accessed remotely, as stated here
If you are going to use the same database, you probably should setup SQL Server Express on your server then update the connection string on the other machines, otherwise you will need to install LocalDB on each machine.
You don't need to install visual studio on all the machines, when you build your application (in debug or release mode) you only need to copy the files in the related bin folder to the target machine. (if I understood that bit)
The config files contains the connection string to the database, in your case a local file (but will be using SQL Express) Look at creating your database in SQL Server Express and pointing to that in development. On the target machines you can then install SQL Express and use that. The connection string will be different to what you have now, look for some examples..

How to deploy C# windows application (setup file) with database and how to manage connection string on client machine

I am creating C# Windows Application in Visual Studio 2015.
Question:
How to deploy C# Windows Application (setup file) with SQL Server database with database connection string.
I create common connection string in app.config :
<connectionStrings>
<add name="conString"
providerName="System.Data.sqlclient"
connectionString="Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=
'C:\Users\DEVELOPERS\Documents\Visual Studio 2015\Projects\Provision1\
Provision1\provision1.mdf';Integrated Security=True"/>
</connectionStrings>
Now connection string path location shows my machine.
Then after installation .exe how to connect database and how to manage connection string on client machine.
The easiest way you can do is:
Save your connection string in app.config is absolutly common
Compile your Project with Release settings
Take your .exe file from /bin/release/.. Folder
The Database should be connected via the connection string. If the database is not connected via network you need to deploy it as well.
Make sure your clients have .Net Framework installed
If you definitive need a setup.exe which installs your software, you can make a new project which is of type setup-programm. Watch this link for description.
If you don't want your users to see the connectionstring you can use a ConnectionStringBuilder. Save just a part of your connection string to app.config and build the rest via Quellcode. Watch this MSDN-Article.

Unable to connect to LocalDB during webhosting [duplicate]

This question already has an answer here:
appharbor connection string
(1 answer)
Closed 7 years ago.
I am using asp.net identity with EF data first approach. Everything is working fine on localhost. I have hosted my website on appharbor. All of my controllers actions and webapi is working fine except AccountController. When a user tries to signup it gives the Internal server error. The details of error is:
Note: My database queries are working fine. It means there is no issue in connection string.
And to enable remote connections SQL Server Network Configuration does not has the option Protocols for SQLEXPRESS
How can I tackle this issue?
Update:
My connection strings in web.config are:
<connectionStrings>
<add name="DefaultConnection" connectionString="Data Source=(LocalDb)\v11.0;AttachDbFilename=|DataDirectory|\aspnet-Inspinia_MVC5_SeedProject-20141215093835.mdf;Initial Catalog=aspnet-Inspinia_MVC5_SeedProject-20141215093835;Integrated Security=True" providerName="System.Data.SqlClient" />
<add name="Entities" connectionString="metadata=res://*/Models.Model1.csdl|res://*/Models.Model1.ssdl|res://*/Models.Model1.msl;provider=System.Data.SqlClient;provider connection string="Server=b1ad1f3e-1ab5-4169-93f8-a5ae006f3e4e.sqlserver.sequelizer.com;Database=8a5ae006f3e4e;User ID=namucclqps;Password=54nWBkCfURyMzYfhbu8NUQb6PEZUsWcoN;"" providerName="System.Data.EntityClient" />
</connectionStrings>
Update 2:
I installed SQL Server Management Studio. Then I hosted my website on appharbor and installed add-on SQL Server. Then using following info I connected to Server.
Then added tables in SQL Server Management Studio and changed connection string in web.config and published website.
LocalDB is only a thing inside Visual Studio. There's no way to even get it on a production server. You need to install some version of SQL Server and then create your database and tables there.
After that, leave the original connection string inside Web.config alone, and instead, add a transform in Web.Release.config that changes the connection string to the one for your production SQL Server instance of your database. Finally, when publishing your site, select the "Release" configuration. The Web.config will then be transformed during publishing and will reference the production database once the site is deployed.

SQL Server Database not working due to server version issue on testing machine

My development PC has SQL Server 2008. I have a database which is created and running on SQL Server 2008. Instance name of SQL Server 2008 is SQLEXPRESS that is default one. My connection string is something like:
<add name="DB1Entities"
connectionString="metadata=res://*/Model1.csdl|res://*/Model1.ssdl|res://*/Model1.msl;provider=System.Data.SqlClient;provider connection string="data source=.\SQLEXPRESS;attachdbfilename=|DataDirectory|\DB1.mdf;integrated security=True;user instance=True;multipleactiveresultsets=True;App=EntityFramework""
providerName="System.Data.EntityClient" />
Working nicely on my development PC. But as I tried to run on another PC it's not working, but instead showing this error :
System.Data.EntityException: The underlying provider failed on Open. ---> System.Data.SqlClient.SqlException: The database '..\DB1.MDF' cannot be opened because it is version 655. This server supports version 612 and earlier. A downgrade path is not supported.
First my test PC has only SQL Server 2005 (instance name: SQLEXPRESS). Due to this error I have installed SQL Server 2008 (instance Name: MSSQLSERVER). I have tried many solutions from internet. I have also tried by changing instance mame in connection string. But still same error is there. I think I may need to swipe instance name of SQL Server 2005 and 2008. But don't know how to do that.
Any solution please?
According to your error message and this chart you have an MDF file that is version 655 (SqlServer 2008) and you are trying to use it in a machine where is installed Sql Server 2005 that supports till version 612.
Now the bad news. You cannot use that file version in that system. There is no tool that can do a "conversion" of the two files and Sql Server is notoriusly know to not support this scenario
Your options are limited. You can upgrade your destination system to the latest version of Sql Server Express and try to load your file in that renewed system. Usually there is no problem when a newest version of Sql Server tries to read a previous version (not before Sql Server 2005 though).
Another possibility is to script your current Sql Server database and then run the script on the destination machine

What should my connection string be?

I have written a C# program in Visual Studio 2010. I use database for my program by add service-based Database in VS2010. I make setup by add setup item for program. In my computer the program installed successfully and run very good. but in another machine after installing program when I want to work with database of program, 1 exception happened:"the program can't find sql....." and some errors like this.
Connection string is: (in single line)
"Data Source =.\\SQLEXPRESS;
AttachDbFilename=|DataDirectory|\\database.mdf;
Integrated Security=True;
User Instance=True"
Also I included database.mdf file into setup files, but this problem has not been sloven.
what's my connection string must be? Is this exception for that string?
The data source \SQLEXPRESS shows me that you're using a SQL Server Express Edition installed to your machine. When you distribute the program along with the database file, the SQL Server Express Edition must be installed on the target machine. Try using an embedded database like SQLlite if this is not desired, otherwise include SQL Server Express Edition within your installer.

Categories

Resources