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

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..

Related

How to create an exe file with local database attach ? (Visual Studio C#)

I would like to create an .exe file with database .mdf file attach. I try to run the .exe file on a client computer. It produces this error:
My SQL connection code
SqlConnection con = new SqlConnection(#"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=C:\Users\zy\source\repos\Automation\Automation\Database1.mdf;Integrated Security=Truee");
Solution Explorer:
The problem indicates requested LocalDB instance requires SQL Server Express instance to run properly, and the machine where executable file started doesn't have running SQL Server Express instance.
If you want to run SQL Server Express together with application start event, make sure SQL Server LocalDB component(s) included in prerequisities by using Project -> [ProjectName] Properties -> Publish menu and look for Prerequisities section like the following:
The LocalDB option depends on which SQL Server version you're using, this will download & install SQL Server Express instance to any client machines which at the time of installation doesn't have SQL Server instance installed.
Additionaly, ensure that database files (both .mdf and .ldf) are included in Application Files section:
Finally, you may adjust the connection string to be more general by using |DataDirectory| in case the application installed in different folder, like example below:
SqlConnection con = new SqlConnection(#"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=|DataDirectory|\Automation\Database1.mdf;Integrated Security=True");
Note 1: Consider to use a resource file or app.config to include LocalDB connection string for efficiency.
Note 2: If you don't want to include SQL Server Express LocalDB instance as prerequisities, make sure you include it separately in the installation package.

How to attach database mdf file to SQL Server Express without Management Studio?

Currently I am working on a C# Windows application, and this application is using SQL Server Express.
I have installed SQL Server and the Management Studio on my PC.
I have installed SQL Server Express on the client's PC, but I don't want to install Management Studio as well.
How do I attach my .mdf and .ldf files to SQL Server Express, without having SQL Server Management Studio installed?
This is my connection string
Data Source=.\\SQLEXPRESS;Initial Catalog=dbname;Integrated Security=False;User Id=sa;Password=password;Connect Timeout=0
You should use the AttachDBFileName parameter in your connection string and point it to your MDF file. There is a special value you can put into this to refer to a local data directory so that you don't need to use a hardcoded path:
AttachDbFileName=|DataDirectory|\MyDatabase.mdf
For ASP.NET applications, the |DataDirectory| refers to the App_Data folder under your project. I'm not sure what it refers to for a windows app, but I'm guessing you could figure it out pretty easily.
Note that InitialCatalog is not necessary when using AttachDBFileName. InitialCatalog is normally used to refer to a DB that the SQL Server instance already knows about. AttachDBFileName is used to instantiate a database from a given file.

Can I connect to LocalDB MDF file on computer from a web app that is being run on a web server?

In development, i.e., when using Visual Studio and its built-in IIS Express instance, I have set up my web app to use LocalDB, with the data MDF file located in a directory on my computer but not inside the project directory, e.g., "C:\MyAppData\MyAppData.mdf", not "C:[...]\MyAppProject\App_Data\MyAppData.mdf."
This is working just fine when using Visual Studio/IIS Express, and the connection string being used looks something like this:
<add name="MyAppEntities" connectionString="metadata=res://*/MyAppModel.MyAppModel.csdl|res://*/MyAppModel.MyAppModel.ssdl|res://*/MyAppModel.MyAppModel.msl;provider=System.Data.SqlClient;provider connection string="data source=(localdb)\v11.0;attachdbfilename=C:\MyAppData\MyAppData.mdf;integrated security=True;connect timeout=30;MultipleActiveResultSets=True;App=EntityFramework"" providerName="System.Data.EntityClient" />
My question is, after I deploy the app to a development web server, is it possible that the app can connect to that same MDF file hosted on my computer? If so, how? If not, why not? Note that I am not publishing the MDF file to the development web server: I am trying to access the MDF file on my computer from the app that is running on a web server.
My assumption would be that the web app would use the connection string to look for that MDF file on whoever's computer is running the app, but apparently it is not this simple and may not even be possible?
It won't work. You need to setup, perhaps, a SQL Server Express instance in a local PC, and inside create a database similar to your MDF (or just export it outright). You can connect to this SQL Express instance changing the connectionsString to a (example) "SERVERNAME" and referencing the database. A great example is at http://www.connectionstrings.com/. SQL Server Express is free, and also fairly more robust than anything internal in VS.

Modify app.config to connect to a database file in computer with no SQL Server Management Studio installed

I created a software using .net4 which uses ORM and connects to database. So when I need to connect to a file I have to first open SQL Server Management Studio and attach the database to it and then I am able to use my software if I don't do this it throws an exception that the underlying provider failed to open.
Now I want to deploy the software to my brothers computer who doesn't have Visual Basic and SQL Server installed on his computer (but he has the .net framework) so is there a way I can modify my app.config to point to the database file instead and I don't need to attach the file to the SQL Server?
Simply I don't want to attach or detach the file to the database again and again how can the .net automatically attach it.
Indeed, install the SQL Server 2012 Express LocalDB (it is likely already installed with your Visual Studio 2012 installation) and then modify the connection string as follows:
connectionstring="data source=(LocalDB)\v11.0;attachdbfilename=c:\MyDatabaseFile.mdf;integrated security=True;"
the (LocalDB)\v11.0 part is where you point at the LocalDB "instance", and you can attach any mdf file you want. Keep in mind that this locks up the mdf file, so you cannot have it open in Management Studio and run your app.
You mentioned attaching a database, but not wanting to install SQL Server. I would take a look at the SQL Server Express LocalDB. While it is technically installed, it is not installed as a Windows Service, so it is much lighter than full SQL server or SQL Express.

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