Solution wide connection strings and DataSets for design time and runtime - c#

I'm developing a Windows Form application with Visual Studio 2013 that needs access to multiple Oracle databases.
The main application is an MDI window and from a menu you can open many different child forms. Each child form has access to a different Oracle database and shows different DataGridView and other data controls.
I'm using Oracle Developer Tools and the child forms contains Oracle controls taken from the VS Toolbox, using drag and drop and wizard, so DataBinding is automatically configured.
The solution consists of 3 projects: Main Application EXE (contains forms, DataSet.xsd and .config files), Data Access Classes DLL, Reports DLL (contains RDLC reports as embedded files).
For each database I need to refer to the same ConnectionString, both for design time (used by wizards) and runtime. At design time, the form controls automatically use a reference to the DataSet.xsd file and so the ConnectionString automatically created the first time with the wizard. Then I also need to access to the same database during runtime and so when I deploy the application I need to keep all the correct references, both for debug and release.
Now I have all the ConnectionStrings and DataSets XSD files stored on the Main App EXE project and my actual config files are:
When I add a new report on the Reports DLL project using the Report Wizard Tool, I'm asked to create a new DataSet and so a new ConnectionString because Visual Studio doesn't show me the existing connections stored inside the Main App EXE project.
To sum up I need to be able to:
Share ConnectionStrings between Main App EXE project and Reports DLL project;
Share DataSets XSD files between Main App EXE project and Reports DLL project;
Have distinct ConnectionStrings for Debug/Release and share them all between projects;
Automatically update DataSets XSD files whenever I make changes to the underlying databases;
Change database context (test environment for debug and production environment for release) without breaking DataSet.xsd or, if this is not possible, by having two distinct DataSet XSD files, one for test and another one for production, that are automatically selected based on the debug/release mode.
Now I need to manually change code in XSD file to replace the ConnectionString (test/production). But since the DataSet refers to the same database structure I need a mechanism to make this easier.

Related

ASP.NET MVC project web.config and app.config files

When creating an ASP.NET MVC project, a web.config file gets generated automatically and is used for all the connection strings and dll versions relevant to the solution.
I've added a new project to the solution for handling data operations using Entity Framework and can see that it has it's own app.config that gets created as well.
Previously, all data and web was on the one ASP.NET project and now I've split them up into two different projects so I can have the web project just have web components and data project have database connections using Entity Framework.
Initially it looked like the app.config of the data project would require the connection strings to the database be present as well, but it looks like the data project can connect fine to the database using the connection strings from the web.config file.
I've tried removing the app.config file completely from the data project and it looks like the solution runs even without it.
Are there any special reasons the app.config file exists in an ASP.NET MVC project or can this file be safely deleted without affecting the website?
For example, I tried putting in 2 different connection strings, one in web.config and a different one in app.config. When accessing the site, it looks like only the connection strings and app variables from the web.config file is used.
Is there some sort of hierarchy that gets used where the app.config details will override the web.config details?
In .Net, there only have three types of project: windows application, console application and class library.
The app.config will be generated automatically with the first two type applications, and it will be used while the application running;
For the web site/application/services, the web.config will be generated and used while accessing web pages. But actually, the web site/application/services are class library type.
In your case, the Data project might be created as a console/windows application, so the app.config will be generated. So you should change the Data project to class library and add a project reference in the Web project, then you can delete app.config safely.
To change project type:
Right click the Data project -> Property -> Application in the left panel -> Output type -> Class Library
To add project reference:
Right click the Web project -> Add -> Reference.. -> Projects in the left panel -> check the Data project -> OK
Finial, the reason why only the connection string in web.config is being used is: the Data project is treated as a class library and it will fetch the configurations from web.config at the runtime. And if you run the Data project alone, it will fetch the configurations in app.config instead.
Both are used to configure the project, the only difference is the type of project.
From the source
Web.Config is used for asp.net web projects / web services. (or Say Web application)
App.Config is used for Windows Forms, Windows Services, Console Apps and WPF applications (or say window application)
So might be your second project for data is window or console-based, that's why created.
Until any file in a project, you do not have the reference in other files, you can remove and run the project.
Ideally connection string at your main project and give the reference in the data project which is handling the single connection string in the solution or in multiple projects.
https://www.c-sharpcorner.com/UploadFile/8a67c0/web-config-vs-app-config-vs-machine-config/

Adding app.config (Application Configuration File) in Visual Studio Professional 2017

I can't get the Application Configuration File template to show up under my Add > New Item > Visual C#. I've run a repair. I've uninstalled and reinstalled. I've deleted and added Cache folders. I've run devenv.exe /InstallVSTemplates. Nothing is working.
If anyone can tell me specifically what installation elements I should be including, and specifically what type of Project I need to start that might help.
I want to create a simple .Net Web Form in .Net and C# and need to include an app.config file.
When you create a new project (depending on the type of project like a web application for example) it will automatically create a template app.config file.
Worst case just make a txt file in notepad and call it [Appname].config
Another way would be to go in the project properties, then the Settings tab. If there are no settings yet, a link should exist in the middle of the empty tab to create a default setting file. Add one Application or User setting and save. Among other things a config file will be created for you too.

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...

Service-based database file difference

I am creating a C# winform application that is using a .mdf database I am creating and attaching to the project via the Add New -> Service Based Database. I can add the database and and add tables just fine via Visual Studio and programatically. But when I go to view the data in Visual Studio, my data I inserted is not present. I believe this is because of the fact that the .mdf file that Visual Studio sees and reads is in the project folder where as the .mdf file the code modifies is put in the bin folder.
Please refer the MSDN link : How to: Manage Local Data Files in Your Project

how do I duplicate LightSwitch DB structure in a second LightSwitch application?

Suppose I have two MS LightSwitch projects. I really like my table layout in one project and want to copy it to the other project. How is this done? What files have the table and key definitions?
The easiest way would be to publish the application, then use a copy of the published database as an attached data source in the new project.
The defintions that you ask about are stored in an lsml file (a LightSwitch-specific XML file), ApplicationDefinition.lsml for LS 2011, & Common.lsml for LS 2012. But be VERY careful if you try to edit this file in any project, it only takes ONE mistake for the whole application to no longer load.
[WARNING: NEVER DO THE FOLLOWING! IT MESSES UP YOUR PROJECT IN VISUAL STUDIO]
What I did in LS 2013 was to create tables with the same name in the new project, so they are already "known" to your project settings. Then I went to [project folder]\[project name]\[project name]\[project name].server\DataSources\ApplicationData of the project I wanted the tables from. I copied the respective files (including script files, if you did data validation, which I also created in the new project with dummy entries) and then overwrote the files with the same name in the respective ApplicationData folder of the new project, which I had created.
When I loaded the project again, Lightswitch showed the new tables in my old project.

Categories