Connection String on Visual Studio 2013 with SQL Server 2014 - c#

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

Related

How to include LocalDB with project when built?

First of all, I am still very new to LocalDB and to ADO.NET as a whole, so if this is a stupid question (and it probably is, knowing me and new material), I apologize.
I am building a .NET 5 console application, and using ADO.NET to connect it to a LocalDB database. There have been a few snags, though. First of all, I have to double-click the database file in Visual Studio's Solution Explorer (I'm using Visual Studio 2022 Community Preview) in order for the app to be able to see the database. This was a mild annoyance when I was doing internal testing, but now that that's done, I can't expect all of my users to have Visual Studio.
Second, I currently have the database connection string as an absolute path:
static string connectionString = "Data Source=(LocalDB)\\MSSQLLocalDB;Initial Catalog=P:\\PROJECTS\\NACS-TRACKER\\DATABASE.MDF;Integrated Security=True;Connect Timeout=30;Encrypt=False;TrustServerCertificate=False;ApplicationIntent=ReadWrite;MultiSubnetFailover=False";
Obviously, this won't work any more now that I'm ready to have others test the app. Given that I'm new to this, I don't really know if relative paths would be appropriate here. If so, how would I go about doing it?
Finally, how would I make sure that the database file is included with the build? Thanks!

On deployment System.Data.OleDb.OleDbException (0x80040E10): No value given for one or more required parameters

My program works when running on my pc in Visual Studio 2017 and when I run the executable on my pc. But I am leaving something out when I create the setup and install it on another pc. Any idea what it could be?
CONNECTION STRING
Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\JROTC_db\JROTC_Inventorydb.accdb
QUERY.
SELECT DISTINCT BaseType FROM table1
After further searching I found a website that had the answer. The installer was corrupting the database file. I just copied the file over to the target machine and it worked fine.

Visual Studio Database Project Generate Script

I just took over a project that is using a VS Database project and I am hitting an early road block. There is a file localhost.publish.xml that I am assuming will build the local database (is that correct)? When I click Generate Script I am getting the error "Unable to connect to the target server" and everything I have read online talks about installing the Data Tools which I did and it isn't helping. It has been a few years since I have even opened Visual Studio so I know I am just missing the obvious, but I cant find documentation anywhere that is giving me a clue about what I am missing. I havent included any of the code because at this point I am not even sure what would be helpful to anyone.
When you double-click localhost.publish.xml and it opens the Publish Database dialog, take a close look at the Target Database Connection. Make sure this is the database where you plan to run the SQL script and that this is a valid server you can connect to. Click the Edit button to change it. Your problem is either a) this is not a valid server, or b) you can't connect to this server from your computer (different network, firewall rules, etc).
The reason Visual Studio needs to connect to the database in order to generate the script is that it needs to determine if the script should contain CREATE or ALTER statements for your db objects based on whether or not they already exist.

AttachDBFileName is not working after deployment

I have defined connection string in my app.config
<connectionStrings>
<add name="StoreConnectionString"
connectionString="Data Source=(local);MultipleActiveResultSets=True;AttachDBFileName=C:\Users\Firdavs\Desktop\Data\StoreServer.mdf;Integrated Security=True;User Instance=True;"
providerName="System.Data.SqlClient" />
</connectionStrings>
I am using c# and sql express in my application. When I am running in Visual Studio in my machine it works fine. But after creating setup and deploying into other machine. I am editing .exe.config file showing AttachDBFileName=Path. Application is giving me error. Sql Express is installed in that machine. I guess attaching is not working. What do you advice?
Please show me direction!
the problem is here : "AttachDBFileName=C:\Users\Firdavs\Desktop\Data\StoreServer.mdf;"
when you deploy it to another machine, that absolute path has low changes to be the same...you need to give a virtual path...
you need to copy the mdf in the same directory. Then in visual studio make sure that when you select the mdf file , you change the property of "Copy to Output Directory" to be "Copy if newer" (you change it by double-clicking on the "do not copy" text already listed there)
and then change AttachDbFilename to be ... AttachDbFilename=|DataDirectory|StoreServer.mdf;....
As I've said before on this site - the whole User Instance and AttachDbFileName= approach is flawed - at best! Visual Studio will be copying around the .mdf file and most likely, your INSERT works just fine - but you're just looking at the wrong .mdf file in the end!
If you want to stick with this approach, then try putting a breakpoint on the myConnection.Close() call - and then inspect the .mdf file with SQL Server Mgmt Studio Express - I'm almost certain your data is there.
The real solution in my opinion would be to
install SQL Server Express (and you've already done that anyway)
install SQL Server Management Studio Express
create your database in SSMS Express, give it a logical name (e.g. StoreServer)
connect to it using its logical database name (given when you create it on the server) - and don't mess around with physical database files and user instances. In that case, your connection string would be something like:
Data Source=(local);Database=StoreServer;Integrated Security=True
and everything else is exactly the same as before...
When you deploy to a customer's PC - go through the same steps: install SQL Server Express, create the database on the server, connect to it using the server=(local) (or the server=machinename approach) - and be done with it!

Pointing a ConnectionString to a special folder via Visual Studio Designer

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.

Categories