C# linq to sql connection string changed to default setting - c#

In a C# desktop application, I am using linq to sql to connect to a sql server 2008 r2 database. The application is setup to point to various databases depending upon the value in the app.config file.
The problem is code in the *.designer.cs gets overrriden somehow by defaults in the .net problem framework. When this happens the using system.configuration gets removed from the application and the default settings values are used to get the database connection string values.
When I notice this problem occurs, I need to do the following:
place the using system.configuration code back into *.designer.cs file
change the code so the database connection info is obtained from the app.config file
remove the property settings so the default database connections are not used.
Thus can you tell me what is causing this problem to occur and how to solve the problem?

I have to do this everytime I update the dbml file, its a real pain. There are 2 things you can try.
First - after updating the dbml, right click anywhere in the dbml, go to properities, then under connection string you should be able to see the one from the web.config, or app.config that you want.
If it is not in the list , then set it to none, then go into the designer file and do what you have been doing.

Related

Issue while importing updated database on Visual Studio

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.

Release plan for my application on other machine by using .mdf file

I am using SQL server 2008 R2 and VS2010. I made simple application by using this tools. I attach database as .mdf in my application and deploy that application on other machine its works fine. Now if I plan for new release of my app which some extended features, I can upload Code by DLL, But problem is updating .mdf file, to handle this I am exporting database into .xls sheets (Application have one utility to backup database) and then import into SQL Server to create new .mdf file. Someone have better solution on this? Can I open old version of .mdf file in SQL Server(Third party software) and Execute DML/DDL script on it to make latest code and database compatible ? May I keep .sql file in one of my project code and execute it by some utility..? Any Class in C# which can handle this..?
I did not get your query completely. Do you want to upgrade the DB through the application?
You can of course run .sql files through your application, but I'm not sure it would help you change the Database configuration.
Alternatively, if you already have the updated .mdf file and the database name is same, then you can follow the following steps.
1. Detach the database by SSMS in the third party environment through SSMS.
2. Replace the .mdf, .ldf and .ndf (if any) in the disk.
3. Attach the updated .mdf file.
This will get the new Object definitions as well as data.
As far as I'm aware, there is no process for merging .mdf files, because the SQL Server might not be able to identify the similar objects properly as sys tables may be different, and also would not know which data to keep in the final data base, in case the table structure, constraints or data conflicts occur.
However, looking at your requirement, the best way I can suggest is,
1. Generate the Alter scripts for the tables modified (By right clicking on the object name and using Script Table As.. option). Of course, I assume you have the list of objects modified and the modifications.
2. Connect the two DB servers over network and write an SSIS package or Import data from the old DB to the new one for the tables you want.
Hope this helps.

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.

.mdf file does not respond to SqlCommand.ExecuteNonQuery

I have a weird problem with my local database. I added it as a .mdf file, and created 2 tables in it.
I tested the connection and the message box states that the connection is working properly.
The problem appears when I try to insert data into that database from my C# application. I tried 2 different ways:
By using SqlCommand.ExecuteNonQuery("INSERT INTO ....")
By using .dbml file (Linq-to-SQL) datacontext
When I try to insert data, no error is thrown, but no data is inserted into the database either. What could be the problem?
I'm using C# in VS 2010 and SQL Server 2005 to make my application
most perceived problems with MDF files tend to boil down to one of:
what file have I actually opened?
is my build/run process actually copying over that file every time I run the application?
do I have gratuitous error-handling that is swallowing an exception?
Check your connection string, and look in the execution folder (not the project folder), until you are very sure which file is being opened. It should get updated after your work (make sure you dispose the connection etc properly).
Also: try fetching the data in a new data-context immediately after the insert, so see if it made it in or not.

How can I store user-tweakable configuration in app.config?

I know it is a good idea to store configuration data in app.config (e.g. database connection strings) instead of hardcoing it, even if I am writing an application just for myself. But is there a way to update the configuration data stored in app.config from the program that is using it?
If you use the Settings for the project, you can mark each setting as either application or user.
If they're set as user, they will be stored per-user and when you call the Save method it will be updated in the config for that user.
Code project has a really detailed article on saving all types of settings.
app.config isn't what you want to use for user-tweakable data, as it'll be stored somewhere in Program Files (which the user shouldn't have write permissions to). Instead, settings marked with a UserScopedSettingAttribute will end up in a user-scoped .config file somewhere in %LocalAppData%.
I found the best way to learn this stuff was to mess with the Visual Studio "Settings" tab (on your project's property pages), then look at the code that it generates and look in %LocalAppData% to see the file that it generates.

Categories