How to deploy a windows forms database application - c#

I am developing a windows forms application that needs to communicate with the SQL Server. I'm facing a problem when I deploy the application once the connection string is trying to connect to an invalid address.
I've already searched a lot and I found out the connection string must have the |DataDirectory| directive. Now the .mdf file is located on the directory C:\Program Files\Microsoft SQL Server\MSSQL11.MSSQLSERVER\MSSQL\DATA. Actually the connection string is:connectionString="Server=.\SQLExpress;AttachDbFilename=|DataDirectory|PDVDatabase.mdf;Database=PDVDATABASE;Trusted_Connection=Yes;"
The only way my app connects to the database is by the Server Explorer (I'm using Visual Studio 2013) where I get the static connection string of the .mdf file I set up in the app.config, but that way won't work after the deployment.
My question is: How do I do to connect my app after the deployment in order to communicate with the .mdf file? (I'm using a setup project for deployment). What's can be wrong?
Thanks.

A lot of things need to be addressed
1) You need a copy of your MDF and LDF files to be distributed with your app
2) You need to know if your user has Sql Server installed in its internal LAN or its PC
If the previous condition is true then
3.1) You need to attach your copy to the end user Sql Server Instance
3.2) You need to change your connection string to point to the end user Sql Server Instance
else
4.1) You need to distribute and install LOCALDB
4.1) You need to prepare the connection string for LOCALDB
Some links to help you in this task
To Attach and Detach a database information
For LOCALDB information
Connectionstring for LocalDb
Find Sql Server Instances Across your network

You should put the connection string into App.config file of your application. There is a section meant for that in the config file.
<connectionStrings>
<add name="Connection String name" connectionString="..."/>
</connectionStrings>
That gives you a flexibility to change the config file after during the deployment of your application to make sure that the connection string points at the database in the user environment.
Later you can fetch the connection string in your C# code from the System.Configuration.ConfigurationManager.ConnectionStrings collection.
Read on the internet on how to write different connection strings, ex here: http://www.connectionstrings.com/ and you should be able to write a correct connection string to the database.

Maybe your troubles are caused by incorrect users and permissions setup in the database. You have to be sure, that the user, that is using your application after deployment, has right to connect and work on the database

Related

Portability of an MDF database file across different Windows machines

Here's the context: I'm writing a C# application that connects to a local database file (MDF). The application's GUI enables the users to do various queries and view results in a Grid View control. The MDF file is distributed with the application and can be considered an immutable (or read-only) resource i.e. not meant to be updated or modified by the end user.
The application is currently in alpha stage and has never been deployed. The source code, along with the MDF database, is source-controlled (via Perforce), so when other developers in my team synchronize their workspace, they get a copy of the MDF database (and LDF log) along with the code. No server connection is involved, all queries are made to the local DB copy using (LocalDB)\MSSQLLocalDB and the default Windows Authentication.
Here's my problem: The MDF file cannot seem to be used anywhere but on the machine it was created on. If another developer builds my app on his computer, the connection to the local database fails with a generic SQL Server error 5171. However, the MDF file is not corrupted and is a primary DB file, so the error is elsewhere.
Here's what I tried:
Verified that SQL server version is the same on all machines, so it's not a compatibility issue
Connection string used in the application is dynamic (i.e. adapts to the location of the MDF file on the computer) and formatted as: Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=C:\MyLocation\DatabaseFile.mdf;Integrated Security=True;Connect Timeout=30
Used same Windows login on all computers, to no avail (still gives error 5171)
Verified DB options with SQL Server Management Service: Restrict Access is set to MULTI_USER
The only workaround I found is to re-generate the whole database on the machine but that's not acceptable: can't one just connect to the MDF file one gets from Perforce and be done with it? Thanks.
Sorry to answer my own question so late after it was initially posted, but here it is: the solution is to make sure the MDF file is saved in Perforce as binary, not as text.
That solved the data corruption problem right away and the MDF database finally became portable across machines/users without any further issue.

How to Run Windows Form Application with SQL Server Database on Another Computer

I'm using Visual Studio 2015 and I just created a Windows Forms application with a SQL Server database. I finished the program and tried to run the application on the other computer but it didn't run. I also tried to install .Net Framework v4.0 and SQL Server Express on that computer and also put the database in path C:\ so that the SqlConnection path on my computer to another computer will be the same.
The code is like this:
SqlConnection cn;
SqlCommand cm;
SqlDataReader dr;
string connection = #"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=C:\Database1.mdf;Integrated Security=True";
But I got an error that is the database I placed in path C:\ is not writable whatsoever. I want to know what I can do in order to run the application.
You must install SQL Express on the computer/server where you want to have the database and then attach the MDF file.
Important:
- Remember to open port 1433 in the Windows Firewall.
- If you are going to use SQL Server authentication, you must create and / or activate the user with the desired password.
- To connect you can use the IP of the computer/server, preceded by the instance of sql express. Example: YourIP\SQLExpress
Good luck!
you need to create sharing server for database and after this, you can access your DB to anywhere and any computer
Thanks
So much misunderstanding and misinformation around LocalDB.
While it was being developed, your application was using an edition of SQL Server called LocalDB. This is a low admin lightweight edition that lets you reference the MDF file directly (contrary to some comments here)
When you deploy your app you decide whether you want to stick with LocalDB or upgrade to a more scaleable edition of SQL Server.
In your case, I suggest you stick with LocalDB
There's some background here and a link to a video if you have time
LocalDB deployment on client PC
https://blogs.msdn.microsoft.com/sqlexpress/2011/07/12/introducing-localdb-an-improved-sql-express/
Now if you really want to an answer you need to post the exact error message that you see, not a vague recollection of it.
The problem might be that LocaDB is not installed, or it might be that there is some SQL Server access issue. We can't tell because you did not post the error message

remote connection string from Webmatrix 3 to Microsoft SQL Server

This seems like a really easy problem to solve, from what I can gather online, but I cannot get it to work for the life of me:
I am trying to help out my dad on his website. It is an ASP.NET based site using Microsoft SQL Server Management Server. The code was developed using Visual Studio, and is hosted with Microsoft's IIS, so, in essence, it is a completely Microsoft-based site.
So far, I've gotten a test site to essentially work on WebMatrix 3; however, I cannot figure out what I need to put into the connection strings in order to get WebMatrix's SQL system to connect to the production server.
I also tried to duplicate the databases by extracting the structure from the production databases and running the query on WebMatrix, but the program needed service pack 2 for MS SQL 2008, and that install is failing for reasons I don't know why.
Best case scenario is I connect to the production server's databases.
Currently, the connection strings have:
Data Source = (ComputerName)\SQLEXPRESS
Initial Catalog = (db)
Persist Security Info = true
User ID = (uid)
Password = (password)
ProviderName= System.Data.SqlClient
So, to clarify,
Web Matrix is installed on my desktop computer, and the website is the code itself. The production site is the website code installed and running on a server somewhere I don't know where, while the test site is a copy of the website loaded onto webmatrix.
There is one working copy of the database, and it is on the production server.
I am trying to construct a connection string within WebMatrix that will connect to the server, through the proper port, and login to the database there to retrieve the data necessary to construct the site so that I can edit the code and test it.
So my question, in particular, is how does one accomplish this when there does not seem to be a slot to indicate a port in the WebMatrix connection wizard?
Webmatrix supplies a wizard to create a connection with a SQL Server. In the Databases workspace you must select Connections > New > Sql Server Connection an input your configuration data into the form.
Anyway the resulting connection string in the Web.config file should be like this:
<connectionStrings>
<add connectionString="Server=(ComputerName)\SQLEXPRESS;Database=(db);Uid=(uid);Pwd=(password)"
name="MailingLists" providerName="System.Data.SqlClient" />
</connectionStrings>
OK, my apologies; I have figured out how this works.
In the connection wizard, you can treat the "Server" box as if it were the "Data Source" option specified in a regular connection string. Then, you can add a comma and the port at which the SQL Server is set to listen.
However, now I am dealing with the problem where the host machine is actively refusing my connection.

Can not find which database is used for MVC 4 with Entity framework

I am trying to learn MVC 4 using C# and Entity Framework code first. I have created my model, create my database and populated it with the seed data. I have a ContextInitializer class which is going to drop and create the database each time I test. When I ran it, I can see my data populated on my site, I can edit, create and delete it. But I do not really know where it is stored.
I have looked at the webconfig file and saw the connectionString as follow:
"Data Source=(LocalDb)\v11.0;Initial Catalog=aspnet-CCConsolidation-20140610143008;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|\aspnet-CCConsolidation-20140610143008.mdf"
I logged in my localdb and do not see that database at all. No error whatsoever. I tried to change the connectionstring and pointed it to my database on windows azure and still can not see the database anywhere. I do not see the mdf file in my App_Data folder either.
So my questions are 2 folds:
1) Where is my database hiding?
2) How do I deploy it to Windows Azure when time comes to do that? Will it be as easy as changing the connectionstring? I have tried that without success.
Please help!
NB: I have seen a few "similar" questions on this site but the answer pointed me to look at the webconfig which I did. I have traced to my local database but it is still nowhere to be found.
The question I was referring to is Where does ASP.NET MVC 4 stores accounts information?
Have I missed something?
Updated my connection string for Windows Azure:
"Data Source=xxxx.database.windows.net;Initial Catalog=CCConsolidation;Integrated Security=False;Database=xxxx;User ID=xxxx#yyyy;Password=xxxxxx;Connect Timeout=15;Encrypt=False;TrustServerCertificate=False;AttachDBFilename=aspnet-CCConsolidation.mdf"
Have anyone seen any issue with this connection string? I used this in the Webconfig to replace SQL express, but the database was not created.
LocalDB is a new functionality in SQL Server 2012. It's a simple command line executable (not a service) that can start and stop DB instances on demand. This has nothing to do with the App_Data of your application, and the old AttachFile.
You can start, stop, delete or create instances using the command line, for example:
SqlLocalDb create "MyInstance"
SqlLocalDb start "MyInstance"
SqlLocalDb info "MyInstance"
SqlLocalDb stop "MyInstance"
SqlLocalDb delete "MyInstance"
If you want to connect to your DB from SSMS, you need to specify the following DB:
(localdb)\MyInstance
The advantage of this DB is that you don't need to install a whole SQL Server instance, start and stop services, create logins, grant permissions, and so on.
The database files are inside your Windows user profile folder, and the process is run under your own credentials, so you don't need to give permissions to any folder either.
You can get plenty of info on LocalDB here: Getting Started with SQL Server 2012 Express LocalDB
Your database is not living on a local instance of SQL Server (e.g. localhost) but appears to be a SQL Server Express instance fired up with your application and the database is stored in the aspnet-CCConsolidation-20140610143008.mdf in the App_Data directory of your site.
You should be able to browse that datastore from within Visual Studio by double-clicking on the mdf file in your Solution Explorer

How to deploy a Windows forms project with a database connection to some other machine?

I have a small demo windows forms application, which has 2 forms.
enter a name in the first form, which will add the name to a table in the database. The back-end used is SQL Server 2008.
After successful entry in the database, the user is prompted to the second form, which is basically a demo calculator.
My question is how can I deploy this project to some other machine. Wana create a normal executable file, in the other machine.
I was able to deploy one another windows forms app, which was without a SQL database connection, by using the Setup and Deployment wizard of the VS 2008.
Also, does the other machine would have to have certain prerequisites, like presence of SQL server in it?
I googled... found some MSDN links but could not find meaningful results.
Any assistance or an external pointer to this scenario would be highly appreciated.
You can get connections string using ConfigurationManager and set connection string in app.config.
Getting connection string from app.config example:
string connStr = ConfigurationManager.ConnectionStrings["myConnStr"].ConnectionString;
After deploy you application you can change app.config manually or make in your app simple form for set connection string.
You could try adding a Setup Project to your Solution and relay on the automatic dependency detection that it brings to build an MSI setup kit for your application that would bundle any dependencies. Not sure about the SQL Server though...
Since it needs a DB for its execution, you have to provide a sql server instance for it. This can be any of these below mentioned
a dedicated SQL server on the network
SQLServer installed on any one of the machines on the network and configured correctly for accepting connections from other machines on the network
SQL Server local instance installed on the second machine if the second machine is not on the network or the program is independent of the network.
AFAIK, for deployment you can copy exe (built after proper connectionstring configuration) or you can rely on setup project

Categories