How to access the database that comes with ASP.NET Web Application? - c#

I am using visual studio 2013.
I have done
File -> New -> Project
I then choose Template -> Visual C# -> Web -> ASP.NET Web Application
Project name is set to WebApplication1.
When I press F5 to run the website it becomes a basic working website which user can register an account.
The problem that I am facing now is that how do I access the database that store the registered users? How can I manage the users? I intend in the future to publish it into a website. I need to find a way to backup the users.
Should I need to add in my own tables should I put them in a separate database or should I store it together with the database that comes with the project?
I cannot find any information on this. I suspect the keywords used for searching might be wrong. Can someone give me some suggestions?

The default template for asp.net creates a database to localdb which is a light version of sql server and will be installed with visual studio 2013, to see the generated database, browse to the AppData folder in your project and you will find a long file which contains some of your project name, this is the generated database. Now you can attach this db file into sql server through management studio if you have one or you can change the connection string to the sql server instance that you want with the database name that you want and rerun the application and you will get the database generated into the sql server that you set where you can do anything like any other database.
EDIT
If you run and registered successfully one user in your application the database will be output there, there is a small icon in the solution Explorer in vs 2013 that shows the all files (hidden) files, as the database is not part of the solution. Try that icon and you should see the database file there.
Hope this helps.

The mdf file will appear in AppData folder once I register a user.

Related

how to publish c# database project

I have created a C# database project in Visual Studio 2010 and tried to publish as a normal project, but its not storing values in database. I have database file.mdf and connection string 'Data Source=.\SQLEXPRESS;AttachDbFilename="C:\Users\fkr\documents\visual studio 2010\Projects\Management\Management\Mgt.mdf";Integrated Security=True;User Instance=True'
I am working with database for the first time and i'm not having any
idea about how to deal with it.
Please help...
You have to replicate your database from development machine to the server.
There is an option of publishing a database in Visual studio, steps for the same are:
Right click on the database and choose publish to provide that will present a dialog window to export your current database schema to a .sql format file which you have to import on the server to replicate the same database as of your development machine.
For more info refer this video: https://www.youtube.com/watch?v=T1LUrUJ2cnc

Create setup file from C# project with SQL server database in Visual Studio 2015

I want to create a setup file from a C# project in Visual Studio 2015 that attaches a SQL server database, and I want to run that setup file on another system and run without Visual Studio and SQL server
You are have two solutions
You should develop your application with SQL express on .NET framework or after develop with SQL Server change connection string from SQL Server to SQL express and rebuild it, be sure you create initialize DbContext class to set some data for run your application at first time with base and default data. [Such as application users and rolls and groups].
You should develop your application with dependency injection pattern [Microsoft Unity, IoC] then modify or replace one interface to change your developed application database.
Then Right click on project in solution explorer and publish it and give to customer.
The customer server has to run some kind of SQL Server if you want the SQL data to be accessed somehow. One thing you can do is to deploy your C# project with Visual Studio, export all needed tables (including their data if you want that) from whatever server you are using (for example MySQL) into a .sql file and pack everything, including instructions on how to install the sql server and your application, into a zip file and ship it to your customer.
What I do with my website application is, that I include a .bat that fetches everything from my git repository, deploys my web application on an existing IIS-Server on the current computer (with 'aspnet_compiler -f -c -u -errorstack -v /web -p "source directory" -d "target directory"' and 'iisreset /noforce') and automatically imports the SQL tables to an existing MySQL Server (with mysql --host=localhost --user=user --password=password --database=database < Backup.sql).
Sorry for that general answer, but you're question really wasn't that precise.
If you want to create a setup of your project then,
Click on Build on the top of visual studio tab and then click on Publish "yourprojectname" click on it and your setup file will be ready.
Hope this helps you
Use LocalDB,
(no need to at least open sql server management studio)
When you create Setup file add prerequisite
.net framework version
sql server (2012 -2019) express localdb
Add connection string correctly!
So from what I can gather you are looking for an easy way to distribute your database schema to clients, is that correct? If so then I don't see how you could intend to run the setup without SQL Server (without VS is fine though).
Assuming my assumption is the case then I think you need to think about a solution that will handle schema upgrades as your project develops, in which case it's essential that you move to Database Projects in Visual Studio as it will handle upgrading the schema automatically, and allow you to run change scripts for the things it can't do automatically (like when you've changed a column type or removed a column etc).
Here are some links to get you started:
Working with Database Projects (MSDN)
Introducting Database Projects for Visual Studio (youtube)
Using the knowledge that CSPROJ files are just MSBUILD scripts at the end of the day you can now publish the database project during whatever your installation/upgrade system is using msbuild which is included in the .NET framework i.e. you don't need Visual Studio.
This is by far the most robust and reliable way of dealing with database deployment. I would advise staying well clear of automated tools like Redgate, they do a great job for small projects that maybe get used in an office etc but for professional software development you need more guarantees and control.
You can try this, with Octopus deploy.
Or, you can try with Code First migration
Have you already existing database, or development starts now?

How to deploy application with sql server database on clients

I have designed an accounts software for my clients. I used SQL Server 2008 database with Stored Procedures. It is developed in Visual Studio 2010, .NET Framework 3.0. I have more than 500 clients using Windows 7.
The major problem is:
Is SQL Server runtime automatically installed with .NET Framework? As MS Access database does not required Office to be installed on client.
I can not installed SQL Server 2008 on each client, it is a tough job. Also the clients are not having a good knowledge of installation process.
How to run SQL Server database on clients without installing its setup on clients? Is there any runtime files or setup?
Option 1 - Setup Project
Using Visual Studio you can create a setup project and install prerequisites that you need during installation.
The installation process is very simple and the end user can install application and prerequisites after clicking next buttons.
Here are the steps for Creating a Setup Project:
1- Create a c# Windows Forms Application
Create a C# Windows Forms Project
Add New Item and Add SQL Server Database to your application
Add a table to your application and fill some data in it
Show the data in your main form.
2- Create a Setup Project
Add new project → setup and deployment → setup project
Right Click on Setup project and Add project Output and select primary output from your main project
Right Click on Setup project and Add project Output and select content files from your main project
Right CLick on setup project and Click Properties and click Prerequisites and select SQL Server Express
Select .Net Framework
Select Windows Installer
Select radio button Download prerequisites from the same location as my application.
Right Click on Users Desktop at left pane and add new Shortcut and select application folder, primary output from SampleApplication, and click ok and the rename the short cut to what you need.
Rebuild solution.
Rebuild Setup Project
Go to Output directory of setup project and run setup.exe
It's that easy.
For more information take a look at following docs articles:
How to: Create or Add a Setup Project
How to: Install Prerequisites in Windows Installer Deployment
Walkthrough: Using a Custom Action to Create a Database at Installation
Option 2 - ClickOnce
Using Visual Studio another option is using ClickOnce publishing.
To do so, in properties of your project, in publish tab, click prerequisites button, you can select SQL Express in prerequisites. This way, you only need to set your database files to copy in output directory, and use AttachDbFileName in connection string: Data Source=.\SQLEXPRESS; AttachDbFilename=|DataDirectory|\Database.mdf; Initial Catalog=Master".
For more information take a look at the following docs article:
How to: Publish a ClickOnce Application using the Publish Wizard
LocalDB is Microsoft's current recommended solution. It allows you to connect to a database file directly, without having to install an instance of the Full SQL Server, or SqlExpress. It is fully compatible with the full version of SQL server. There are no installation requirements on the client end, as the libraries are packages along with your application when it is built.
You can read more about it here.
you can instal sql express With silent installation in your setup
this way not showing any wizard for install sql express
Read this Link

How to connect Visual Studio 2010 Express C# to SQL Server Express

Is there any real workaround to getting the Express edition of C# connected to database options other than the lightweight options allowed (Access, SQL Compact, or SQL file)? As has been noted elsewhere, it's possible in the web edition to do so. It has also been noted that you can create a project file, open it in the web edition, create a connection, and create a project with your database entities, and then switch back to C#.
However, as a workaround this is quite clunky, as it seems like if I could just get the SQL connection persisted in my C# Database Explorer, which seems to be tied to the application, not any project, then I would permanently be able to add to the database without having to constantly switch back and forth.
Has anyone worked through this problem?
I figured out a better workaround. The application settings seem to be saved in [Documents and Settings]\[User]\Application\Microsoft\VCSExpress\10.0 and VWDExpress\10.0. What I found is copying ServerExplorer\DefaultView.SEView from the VWDExpress to VCSExpress after creating the connection in WD worked perfectly. I could from there create new objects, refresh from the data, create a database from the design, etc.
yes get the .net connector:
http://dev.mysql.com/downloads/connector/net/
Use add reference to point the dll and use their functions to connect to a mysql server
Ok Just to be clear about all this...
I am on Windows 7 and am admin on my machine.
You may have to change permissions on the following folders in the usual way.
I have visual Studio c# express edition on my machine together with Visual Web developer express.
I cannot browse my local SQL server with c# express and connect to a database.
So I followed the above method.
Open an ordinary explorer window and browse to:
C:\Documents and Settings\YOURUSERNAME\Application Data\Microsoft\VCSExpress\10.0\ServerExplorer
rename DefaultView.SEView to DefaultView.SEViewORIGINAL
Open another explorer window and browse to:
C:\Documents and Settings\YOURUSERNAME\Application Data\Microsoft\VWDExpress\10.0\ServerExplorer
Copy DefaultView.SEView and paste it into the folder in the other explorer window.
You will now have 2 files in the folder
DefaultView.SEViewand
DefaultView.SEViewORIGINAL
If you open visual studio c# express 2010 you will now see your databases.
You will not be able to create a new one to that database though.
You will have to create any new connections in VWDExpress first and repeat the process.
Someone might want to check out how to just move the differences between these files and fix it that way.
Good luck
Yet another work around.
Sorerons solution only works for local databases. You will get the dreaded...'your version cannot connect ...blah blah blah'.
So I opened the project in VWD...connected using Linq to sql classes.
Voila,it worked. I had the project opened in both vde and vwde at the same time.(I was in the process of copying the project templates from my vis developer express to vis webd at the same time)

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