Pointing a ConnectionString to a special folder via Visual Studio Designer - c#

I'm trying to set a DataAdapter connection string to point to %APPDATA% from within Visual Studio designer by editing the DataAdapter properties. I cannot seem to be able to use that moniker on the connection string property. It doesn't get translated to C:\Users\MyUserName\AppData\Roaming when I then run the application in debug mode.
How can I ensure that these type of strings can be set within the designer so they are not hardcoded and do not jeopardize any future Setup project?
Note:
I know how to handle it in code with GetFolderPath(). But is there similar functionality for controls properties and string settings/resources in the designer?

The %APPDATA% refers to a special location in your source code folder (.i.e, %csprojectLocation%/AppData for sqlserver, %csprojectLocation% for MS Access). It is not the OS Application Data.
AFAIK there is no special variable that denotes the OS Application Data for dataset designers. So you have to manually set the connection string after you deploy it to your client computer. You can use an installer to do this though.

Related

How to generate the appsettings.<EnvironmentName>.json file?

I have an ASP.NET Core 2 WebAPI which will be deployed across the following environments:
INT, QA, STAGE, PRODUCTION environments.
Based on the above, I need to have appsettings.<EnvironmentName>.json file for each environment. From the link : https://learn.microsoft.com/en-us/aspnet/core/fundamentals/environments?view=aspnetcore-2.1, I see that
In case of local development environment, the Environment Variable called ASPNETCORE_ENVIRONMENT is set to Development. In case of the deployment where ASPNETCORE_ENVIRONMENT is not set, the default is Production
I would like to know what are the steps required to take care while preparing the appsettings.<EnvironmentName>.json file for INT, QA and STAGE environment. Do I need to set the environment explicitly for each environment web server:
set ASPNETCORE_ENVIRONMENT=Development.
Can anyone help me to by providing their guidance?
I might be missing something, but this seems pretty straight-forward. To create the environment-specific setting files, you literally just create them in your project, i.e. create a new file called appsettings.Foo.json in the root of your project. Visual Studio will automatically put it under appsettings.json as a parent in the Solution Explorer.
On the server, yes, you'd need to set the environment you want to run under to ASPNETCORE_ENVIRONMENT. If it's supposed to use appsettings.QA.json, then set it to QA. That's all there is to it.
You will have to set the ASPNETCORE_ENVIRONMENT on the server with the configuration you wish to host the app.
Take a look at the sample project provided by Microsoft on Github
Sample Environment project
First, in Visual Studio you will have to right click your project name and add a new item.
Inside 'Add new Item' window chose 'Scripts' on the left column, and look for JavaScript JSON Configuration File template.
Name it as appsettings.Production.json, for example, and click 'Add'.
It's going to connect automatically to appsettings.json.
To set which environment your application runs on, just set up the
ASPNETCORE_ENVIRONMENT environment variable.
Set the variable through the command prompt by typing set
ASPNETCORE_ENVIRONMENT=Production in Windows or export
ASPNET_CORE_ENVIRONMENT=Production in Linux.

Connection String on Visual Studio 2013 with SQL Server 2014

This is my connection string on my Visual Studio, my database file name is IOOP_Database
Data Source=IDEA-PC\SQLEXPRESS;Initial Catalog=IOOP_Database;Integrated Security=True
The problem is when I run this system on another laptop, there will be a error because of the connection string. Is there any way I can state my connection string as Date Source=IOOP_Database.mdf? I've also moved my mdf file to my Visual Studio project's debug folder, so the project file and the sql database file are actually together.
You can use AttachDbFilename property in connction strings. It has some limitations and has been advised to use that only in development environment not production.
AttachDBFilename is unique to SQL Express, it spins up a user instance of SQL Express attached to a specific DB Filename for single user mode. Database is simply the name of the database to use, it has no additional connotation. For any production server, you would most likely not be using AttachDBFilename. It is strictly useful for development and experimentation in single-user mode.
from MSDN forum
Also you might find these helpful:
similar question on stackoverflow
usage example

How do I publish an Access database with my C# application using Visual Studio?

I'm trying to figure out how to publish an application I wrote. We didn't get to publishing in class, and my programming teacher is having some health issues and isn't available right now.
I found the "Publish" option, and can get it to create a folder with an install program... but I open it, and it just opens the program, and spams me infinitely, complaining that my Access database (located in the bin > debug in the build stage) can't be accessed (from some weird path I don't recognize). I tried using WiX, but it gave me an error when I tried to install, saying it doesn't have access to the install folder (running as admin). I've been googling for a few hours, poking at it, exploring, and I'm not getting too far. Can anyone ELI5?
When you use the "publish" option for desktop apps, VS creates a click-once installer that will place all the files it knows about in the appropriate locations.
Unfortunately, it can't guess which other files your application needs so you need to tell it explicitly.
If you right-click the Project->Properties, go to the Publish tab and click the "Application Files" button, you'll see all files that will be added to the installer.
Next, click "Show all files" at the bottom. Find your database, and change the Publish Status to "Data File".
Note that I've only ever used the Click Once installer to install static files (like images/documentation) that are never modified, only replaced in later releases. I'm not sure whether your (modified) db will be preserved during an update but I suspect not.
If the Click Once install process is too simple for your needs, VS2010 has "Setup Projects" which create more complex installers that support logic/code. For VS2012, the commonly suggested option is Wix. Unfortunately, it's got a steep learning curve but it can do pretty much anything you need.
I believe VS2013 and later have setup projects again through an extension but I haven't tried it myself.
Edit:
The easiest way around this is likely to set the connection string programatically based on where the application is executing from.
Note that as per this answer clickonce apps are usually executed from deep inside the user profile directory (also read the answer below about data directories). It's a side-effect of how ClickOnce works (it wants to install somewhere the user is guaranteed to have write access).
Check if there really is an .mdb in that folder. If not, you need to tweak the installer or the properties for the .mdb. Assuming it's in the same location as the executable, you can tell your application where to find it...
string dbPath = IO.Path.Combine(
AppDomain.CurrentDomain.BaseDirectory,
"access.mdb");
string connectionString = String.Format(
"Provider=Microsoft.Jet.OLEDB.4.0; Data Source={0}",
dbPath);
I'm not sure why you think the database file added by the installer is in a directory directly under C:\. It's possible you're not looking at the file created by the installer.
To avoid confusion, try changing the name from access.mdb to something else (temp.mdb?), build the installer, rename back to access.mdb. Now, when you've installed the application, make sure the file you're looking at is now called temp.mdb. If not, you're looking at the wrong file.
This Link Has Full demonstration of Database Connectivity And Publish a C# application with database. The application is also running on another machines.
How to Publish C# Application with access database

SQLCLR database name adds _1 every time I make a change to the project

This is a new phenomenon I am seeing, my Database name is: MySQLCLR, there is a script that always give this name in it:
:setvar DatabaseName "MySQLCLR"
all of a sudden now, everytime I make any change to my sqlclr project code and re-compile, the new output script has the name with an added _1 in it, like the following:
:setvar DatabaseName "MySQLCLR_1"
Another change to the code and subsequent build will generate this:
:setvar DatabaseName "MySQLCLR_1_1"
and so forth....
any idea why this is happening?
VS2012, MSSQL2008 R2 on windows 2008 R2
Go to your project's properties, go to Debug and under the Target Connection String section, click on Restore Default.
Maybe you'll need to install the SQL Server Data Tools - December 2012 update.
I think it is because it saves the database at the same location without overwriting the old database, is your database connected in visual studio? if it is, try this:
Search for your database in the solution explorer example: "database.sdf". click on the database and look in the properties. There should probably be a property which is for default "don't copy" or something, change that to copy if newer and then recompile. does the problem then still exists?
I have seen visual studio cause this issue with other SQL project files as well. In my case, it was because their are files already in that location with the name you are trying to give it. Check that directory and see if the files already exist. If they do, delete them and then your script should behave as expected.
I ran into this issue when people would do a bad TFS merge and left hanging generated files on their local computer. When they went to re-generate them, the generator incremented all of the file names in a similar fashion to what you are describing.
I ran into this too. The setting that you must change is in the Project Properties / DEBUG tab on the left. (Right click on your database project, select properties.) In there, you will see a target connection string. In there you will see the hated _1 (or _2) appended to your database name. THIS is where you need to make the edit so that your deployment scripts (e.g. mydatabase.sql) that is generated as part of the deploy step has the right database name.
Yes, this setting even affects the RELEASE version too, even though it is in the debug tab. Go figure.
Simply select the 'Restore Default' button and the appended number should disappear. Hit the green > button or f5 (even in release mode) and you will get a nice clean deployment script with the right name in it.

New Connection fails during Add New Data Source Dialog

The Problem
If I have Visual Studio 2010, Service Pack 1 installed, and I go to Data, Add New Data Source and click Database, it brings up the standard Choose Your Data Connection wizard page.
On that page, if I have an existing connection configured, I can then select it, and add that data source to my project, no problems, no issues.
On the other hand, if I click New Connection... it just bombs out, wizard closes, as if I had done nothing. Now keep in mind, Visual Studio doesn't close, and there are no exceptions, errors, or warnings, the dialog window just disappears.
Unsuccessful Attempts to Resolve Issue
I've checked the simple things, using a debugger, and a valid machine config file, not sure what else I might be missing.
Using a Debugger
I tried starting a 2nd copy of Visual Studio as I did this, and
attaching a debugger to the process, with all exceptions checked in
my debug menu, and still nothing.
Validating machine.config file is valid
I backed up my machine.config file, and used another person's working machine's machine.config file, and I'm still having issues.
I have since reverted back to my own machine.config file.
I suspect the machine.config might be the issue, but I thought
taking a working one should work, unless I'm missing one of the
registered providers? I don't know enough about how this wizard
works behind the scenes though, or what sections/lines of the
machine.config file exactly drive the wizard.
Uninstalling all third-party database providers
I have recently tried uninstalling Oracle, SQLite, beta versions of SQL Server Compact Edition, and MySQL providers in hopes that one of them set a registry entry or configuration entry that was negatively impacting my ability to create new connections and the error still occurs.
Reinstalling Visual Studio
I reinstalled Visual Studio, complete uninstall, and reinstall, no extensions, or nothing, and the issue exists, definitely convinced its a configuration error at this point, I could use the expert opinion of someone who knows more about the configuration behind the scenes for this dialog though.
Any help in resolving the issue would be greatly appreciated.
Visual studio 2012 solution.
Close visual studio.
Open the event viewer, go to Application Log, and find Errors with source devenv.
Mine said
The following information was included with the event:
.NET Framework Data Provider for MySQL: An unexpected error occurred in the data provider.
Then i opened registry editor and went to:
HKEY_CURRENT_USER\Software\Microsoft\VisualStudio\11.0_Config\DataProviders\
And deleted the key that reffered to the faulty data provider
{c6882346-e592-4da5-80ba-d2eadcda0359}
(default) = .NET Framework Data Provider for MySQL
I have the same issue. Delete all records in event viewwr and works.
Start -> run
Type "eventvwr"
Select application
Select "Action" > "Clear All Events" from menu
Save or not as you wish the events backup. Do the same with "System"
Try using Server Explorer window to add your Connection.
Try defining your Connection String from Settings.settings file
Try moving/deleting all the *.datasource files from the project/solution before adding the connection
Try creating a new, empty solution and see if the problem is solution-indepentent
Try finding out what registry keys and files Visual Studio uses when the issue occurs with Process Monitor from Sysinternals
I have had the same issue today.
Solved it by removing a tag in machine.config file:
**32-bit**
%windir%\Microsoft.NET\Framework\[version]\config\machine.config
**64-bit**
%windir%\Microsoft.NET\Framework64\[version]\config\machine.config
Tag to remove:
<DbProviderFactories/>
The problem is that when the dataset is modified, the dataset is linked to the bi object. I solved the problem like this: I made a change and registered the dataset and it came up an error. I then undo it with CTRL + Z, the error is resolved. Then I moved forward with CTRL + Y, and the error went away. Thanks to this solution, I added the query, added a new table.

Categories