Issue while importing updated database on Visual Studio - c#

I'm doing an application on Visual Studio using an SQL Server database, the application is almost complete but I did a few changes on the database (added and deleted some rows) but when I'm trying to do the Schema Comparison to update the database on Visual Studio it doesn't let me choose a project as the target.
According to what I've found, it should show the project I'm working on, but instead I have this:

try to use directly database comparison and you will get the difference anyways

Don't you need to do it the other way round? So if you want to compare changes with the database so you can update the database (your target schema) you should be clicking 'Database' and doing it that way. That will show you the differences, which you can then choose to propagate to the database.
See here for more info.

Related

Update typed dataset doesn't affect database [duplicate]

I am creating a library application in Visual Studio 2010 Professional and Access 2010. I bind the Access database to Visual Studio. When I fill out the fields and click submit I can see the new record in the DataGridView, but when I close the application the new record is not saved and I have to input the record again.
Can somebody help me to know why when I input the new record through the application the record is not saved in the database?
It is a very common situation. You have your database file (the MDB or ACCDB file) listed between your project items. If you click on this file and look at the properties window you will see a property called Copy to the output directory. If this property is set to Copy Always then every time you start a debug session the database file listed in your project items is copied by VS in the output directory (usually BIN\DEBUG). Of course this copy doesn't contain the records inserted in your last debug session and you think that your previous insert has failed.
Setting this property to Copy If Newer, the mentioned behavior will happen only if you change the database schema manually.
Setting this property to Copy Never, will let you manually copy the database file.

Visual Studio is trying to connect SQL database while loading a solution. Gives error: Cannot open database requested by the login

I've got a Visual Studio solution on which I had been playing with few C# projects related to Entity Framework coding.
I was creating some test databases to connect to and learn db first and code first approach. The problem is now every time I try to open the solution containing those projects, for some weird reason Visual Studio, or the solution or some project in it tries to connect to those databases. It gives an error as I've deleted those databases:
Cannot open database "Company" requested by the login. The login
failed. Login failed for user "administrator".
Here is the error message snapshot:
I get this error one by one for several databases. But I'm wondering why would VS try to connect to some database just while loading the solution? In case anyone else has faced this situation or know the reason behind it then that will be really great.
Note: The tags in the question I've put is only due to the environment I was working into. I'm trying to reach to a point to figure out if my problem is specifically related to those areas.
TL;DR; Delete the inactive/invalid SQL Server connections marked with a red cross in the Server explorer window of Visual Studio to get rid of the issue. SQL Server connections become invalid/stale whenever you delete the corresponding database from SQL Server but Visual Studio doesn't know about it and keeps trying unnecessarily.
Details: As correctly suggested by #stuartd, the Server Explorer in Visual Studio was messing up. Posting the answer in case it helps anyone else as well.
I realized that every time I was adding an ADO.NET Entity Data Model file for Entity Framework into my project as shown below it was adding a SQL connection node in Server Explorer window as shown below.
The connection was corresponding to the DB I chose in the ADO.NET entity data model addition wizard. I see that there is red cross against few of them since I've deleted the databases from SQL Server instance. Whenever you load a solution Visual Studio will always try to refresh the connection of all the databases currently found in Server Explorer. In fact in the case of DBs which have a red mark against them (as they are deleted) I'm sure VS must be employing the retry logic as well while trying to make a connection with the database. All this takes and wastes time as far as the loading time of a solution is concerned.
How to resolve the error: I deleted the inactive/unused connections marked with a red cross to get rid of the issue. It will be great if someone knows a way to mark the connections as inactive i.e. the connections live in the Server Explorer but VS shouldn't try connecting them every time until I ask it to refresh or may be it can refresh when I expand its node.
I also observed that Visual Studio somehow maintains these server explorer connections on a per solution file basis, may be in *.suo files. I had to delete those stale connections after opening every solution which were facing this error related to DB connection issue while getting loaded in Visual Studio. All solutions facing this issue somehow had at least one project related to entity framework :).

Does a simple solution exist for programmatically detecting add/edit/deletes locally and updating visual studio online accordingly?

I have code that generates SQL scripts that will run nightly. I want to check this into source control each night, so I get a history of changes to tables etc. as well as picking up new tables and when tables are deleted.
I have a team project created in Visual Studio Online.
From looking online it looks like there's no reliable way of automatically picking up changes locally and committing them to VSO. I'd have to create something that compares what I have locally to what is in VSO, which to me seems error-prone.
If I use the command line utility it looks like I have to tell it what is added and deleted (i can't just check everything out, then add/edit/delete my local files, then commit).
I've also looked into the Team Foundation Server class, but that's obsolete.
TL;DR: Is there anything I can to do easily sync local changes (add/edit/delete) to VSO, without having to tell it what's been changed?
Why not just check in the changed from your workspace?
If you have a Local Workspace that includes the folder that you generate the SQL into you can just call tf.exe checkin to get all of the changed into TFS.
+Daniel is right.

WPF + SQL Server Compact Debug and Deploy

I have a WPF project set up to use a local SQL Server Compact database through an ADO.NET Entity Data Model in Visual Studio Express 2012 for Desktop. The project works great, on first run I can load all of the data, manipulate it as I please and come back later with the changed data still in place.
I noticed while doing a little restructuring to the schema that the data visible to VS was only the very first bits of data that I entered manually when creating the database and the next time I compiled all of the data I had added since was gone!
After some digging, I came to the conclusion that the compiled version of the app was using the SDF file sent to the bin/Debug folder by the file's Content:Copy If Newer build action. This means that there could be as many as 4 different copies of the database to be worried about: project folder, debug folder, release folder, and the deployed copy on the end user's PC.
I would like to have a single copy of the database on my dev machine that is accessed by both debug and release compiled versions and the database explorer in VS that is installed on the end user's PC by ClickOnce. I suppose I could change the connection string to an absolute path during development and hope I can remember to change it back to relative before I publish for deployment.
Finally, I foresee the need to release updates for this application as well and am worried that such an update would erase the end user's data if improperly done. If possible, I would like to be able to only update the schema of the end user's database without touching the data itself whenever I release an update. If this is not possible that is acceptable and I'll just have to make sure I put every structure I can think of into the database before my first deploy.
In summary my questions are the following:
How to share a single sql compact database between VS, debug, and release?
How to handle local database during application deployment and updates, with the optional ability to update the database schema without erasing the data?
I have a similar application and I keep the database file completely separate. Because you may also need to do updates that you don't want the user database overwritten. I have a process that checks the database schema before the EF connection takes place. So when my users install this application it requires they download the database file from my webserver and puts it in a specific location on their computer.

Create Databases in a C# application

What is the best way to create databases on C# application, I mean by a click of a button, it will create a database with a different name but the same model?
Currently, I script TSQL onto a C# application, so when I click a button, a new database will be created with the name I defined, it works well, however, it is extremely difficult to maintain, is there a better way to create database on C#?
Regards
You can use SMO to create databases. See MSDN sample.
One way is to just have a copy of your database file (mdf) available for pushing onto a new server. This similiar to what microsoft does with it's Model database. Whenever you create a new database it starts by making a copy of that one.
Your button click could copy the database file to the desired location, rename it as appropriate, then attach it to the running sql server instance. Whenever you need to update the database with a new schema, just copy the mdf to your deployment directory.
Sql Server 2008 Management Studio can generate SQL script for database generation. Save script in text file and later spool it to RDBMS and create database.
This is not too hard to maintain.
To generate script in SqlServer 2008 Management Studio: right click on database -> Tasks -> Create Scripts, click few times and save to file.

Categories