View local database in Server Explorer - c#

I've created a local database in my C# Windows Application and I'm inserting data into a table in the database. My question is why can't I see the data in the table when I view the table in the server explorer? I see that there's a database in the /bin/debug folder that's getting the data but thats not the one thats available in the server explorer. Is there a setting somewhere in Visual Studio 2010 to use the project database not the debug version?

If you open the App.config you can change the data source of the connectionString to a direct link to your database file. Also, select your database in the Solution Explorer and in the Properties section change the copy to output property to "Do not copy"

Exactly i think, you have two copy of your database.one in your bin folder and another in your server explorer(with different location).when you add data into table exactly you insert them into database at your bin folder(a copy of your database).
check your address of database in your connection string,it must be the same as address of your database in server explorer.(do you use EF?)

Related

Adding new Service-based database to C# project in Visual Studio 2013

Lately i was building C# windows forms application with service-based database.
I was forced to make few changes to make it work, but it's still not working as it should.
I'am doing this at start:
1. Add Service-based database to project
2. Create Tables
3. Add Entity framework 6.1.2 Model to project
This creates seccond database file in Project and in Data Connection. Now if i want to show any data, I must create dgv and dataSet pointing at this newly created .mdf file. Also i need to change Copy to Output Directory to Copy if newer. Now I can see my data, but i only see empty table in Data Connection in Server Explorer. Also if I open it and refresh, all data in my application are gone.
Is there any tutorial, or something about How to add Service-based database and connect to this file using entity framework, wihout coppying it to output directory?
If someone need a solution for this case, there is one:
Create database in any program (could be visual studio)
Copy it to /bin/debug/
Put bin folder to your solution in Visual Studio
Select database and make Copy to Output - Do not copy
Edit connection string in entity framework to
attachdbfilename=|DataDirectory|\file.mdf;
Database should now appear in Server Explorer in VS. You can now remove all newly created databases (leave only this one you copy to bin/debug/)
Now you will see correct database in Data Connections, and your program will update this one. You can easily coppy your solution to any other location, and connections will not be messed up.
Btw, I think this is entity framework error or DataDirectory should point to Solution folder, not bin/debug. I know we can change DataDirectory pointing...

Is there a way to add an existing SQL Server database file to a project for integration tests?

I can't seem to find the steps I need to do this process though Google\StackOverflow.
I have an existing SQL Server 2012 database installed locally on my localhost instance of SQL Server.
I would like to add the .mdf/.ldf files to a test project in Visual Studio and have a way for tests to connect to that database on machines other than my own. I believe the .mdf and .ldf need to be created when the project is built.
Hi maybe you can test it like this:
Create your project.
Right click on you project Add > New Item… > Visual C# Items > Data > Service-based Database (.mdf and .ldf files will be created in your project)
Open mdf in server explorer.
Right click on .mdf inside server explorer and click "New Query".
In the "New Query" window you can paste a query generated for "schema and data" from sql-server.
Run the Query and you have all the schema and data from the original database.
There you can work as you like with the database as a localdb.
For creating schema and data query in sql-server:
Right click on your database in sql-server > Tasks >Generate Scripts.
Follow the wizard but on "Specify how scripts should be saved or published" option, go to Advanced> Types of data to script> Schema and data.
Save it on a file or save on clipboard.
Paste the entire query without the name of the database as in the point #6 in the above paragraph.
Hope it can help you.
Nelson Parra R.

C# Express Edition + SQL Server 2008 R2 Express; Database file being recreated on run

I created a standard C# console application in 2010 Express, used the wizards to create a simple sql express db file which gets placed in the project dir, used the wizards to generate an entity data model based on the newly created db, wrote a few lines to test out entity.
Problem is each time I run the application it recreates the DB file in whatever dir the exe is in overwriting itself everytime and completly ignoring the original db file that sits in the project dir.
Does anyone know what I am missing here ?
Not entirely sure about this but try it out.
Make sure your connection string points to the database file in the project directory.
Select the file in Visual Studio and Choose 'Copy to Output Directory' ->> Do Not Copy.
Hope it works.
you must be using some way to find that file or some path parameter within the database connection that points to the database file.
Now either you can have that parameter be generated by code or make the directory specific and make the database file copy at a specific location from the original location so that the only that particular database is accessed using the application.
For doing that you can add another key in the application config file or at some other place so that the database your application is accessing is in the project directory itself at all times.

MDF database inside project. Visual Studio Server Explorer not showing changes to the data

I've added a new MDF file to a .NET4.0 class library project, created a single table and a LINQ to SQL data model in order to access it. The connection string that was automatically added is:
Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\ClosedIncidents.mdf;Integrated Security=True;User Instance=True
I run some code against it that first prints out the tables records to the console, add's a record, and then prints out all records again. The idea is to just check that after VS closes the MDF file retains the changes. This is proved true as on a second run of the app it moans that it can't add the same hard coded record because it already exists.
The issue I am pondering on, is why when in Server Explorer if I view the data of the table that it shows nothing is there.
My understanding is that it attaches the server or user sql server instance directly to the mdf file within my project. Changes through the instance are written back to the source mdf, not a copy of it. Why then does the program show the record being added but not Server Explorer?
Chances are the file is being copied to the output directory, and so the IDE and the application are actually touching different files. You can confirm this by doing a clean build.

.NET including class library database (and other resources) with windows app deployment

I have a class library that attaches itself a tiny SQL Server database that resides in its Data Directory. When i'm using this class library with another windows application i see that once i compile my code, the database files get copied to the bin folder of my windows app project. However when i publish the windows app,install, and run it, i get the error 'An attempt to attach an auto-named database for file C:\Documents and Settings\User\Local Settings\Apps\2.0\Data..\DB.mdf failed.' Obviously this folder doesn't have the mdf files.
I guess this won't be a problem if i just add the database files to my windows application project. But surely there's a better way?
You could include an SQL script for creating the database into your "installation/run first time routine".
I guess that you've already stated that having a form of SQL Server is an installtion prerequisite.
For the data files I would recommend that you use a variable connection string for accessing your database. That way you can change the installation routine to include asking the user where they wish to have the data files installed and save that as part of your connection string to the app.config file.
Conversely you could also use the users selection of where to install the app to override the relative path stored for the database within your code (using the same connection string variable as mentioned above).

Categories