Can't access LocalDB on other computers, through application - c#

I have made an application which uses a LocalDB, to refer to some predefined values as part of some mathematics that it does.
I have created an Installer project so that when the application gets installed, a copy of the DB is sent to [ProgramData][ProductName], which is mapped as the pathway for the ConnectionString.
When I install it and run the application on my computer (out of Visual Studio), it runs exactly as I want it too - and there are no errors, warnings or messages in the code.
However when I installed it on to someones else's computer (so they can play with it and point out any errors that they come across), this is where the problem comes about.
On their computer, whenever the code refers to the LocalDB they get the below Exception Message:
"A network-related or instance specific error occurred while trying to establish a connection to SQL Server" System.Data.SqlClient.SqlException (0x80131904).
The error goes on to say that the LocalDB instance does not exist.
I've double checked the ProgramData folder, and can confirm that the LocalDB did get copied there during the install - and I have confirmed that the code is looking at the correct folder when trying to connect to it - I just can't figure out what is happening.
I have checked that the user has read/write access to the folder, and is a LocalAdmin on their computer.
Is anyone able to help guide me to a solution with this, please?
ConnectionString is below, if it helps:
AppDomain.CurrentDomain.SetData("DataDirectory", Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData), "Application_Name"));
ConnectionString = #"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=|DataDirectory|\Database1.mdf;Integrated Security=True";

You actually can connect to a localdb instance from other machines. It's a perfectly valid configuration. (Although, if you're really running it like a service, you'd be better off with SQL Server Express Edition instead).
First, you need to make sure that the localdb instance is fired up. That can be your application, or it can be via the SqlLocalDB Utility. See here: https://learn.microsoft.com/en-us/sql/tools/sqllocaldb-utility?view=sql-server-2017
Second, you need to make sure that TCP is enabled. Details of setting up a shared instance are described here: https://learn.microsoft.com/en-us/sql/database-engine/configure-windows/sql-server-express-localdb?view=sql-server-2017
Hope that helps.

Related

LocalDB - New code-first databases no longer being created on default folder

I'm using Entity Framework's Code-First and LocalDB, with the following connection string:
Data Source=(localdb)\MSSQLLocalDB;Initial Catalog=MyApp;Integrated Security=True;
Connect Timeout=30;Encrypt=False;TrustServerCertificate=True;
ApplicationIntent=ReadWrite;MultiSubnetFailover=False;MultipleActiveResultSets=true;
The instance and its database files were properly created inside this path:
C:\Users\Me\AppData\Local\Microsoft\Microsoft SQL Server Local DB\Instances\MSSQLLocalDB
But I was messing around and I renamed then deleted the MSSQLLocalDB through Visual Studio's "SQL Server Object Explorer" and its files. Now that I created it back, all databases generated via EF Code First are being created inside my %USERPROFILE% folder, i.e. C:\Users\Me.
In "SQL Server Object Explorer", if I right click and "Add New Database", it shows the right path. Right clicking the instance then properties shows that "Default Database Location" is correct.
The registry is correctly configured:
[HKEY_CURRENT_USER\SOFTWARE\Microsoft\Microsoft SQL Server\UserInstances\{9F44D466-A9BA-40E3-9DEA-21E0638C80A2}]
...
"DataDirectory"="C:\\Users\\Me\\AppData\\Local\\Microsoft\\Microsoft SQL Server Local DB\\Instances\\MSSQLLocalDB"
I tried to create a new instance through SqlLocalDB Utility with a different name, but it behaves the same way. Also tried rebooting, before and after deleting the instance, no success.
I also tried reinstalling. I uninstalled Visual Studio (Web and Desktop), MS Data Tools, MSSQL Compact and MS LocalDB, then reinstalled VS, but no success.
I don't want to change my connection string and point it to the correct path, since the project is shared and maintained by other developers.
How do I revert this behavior? I'm using Visual Studio 2015 Express.
You can set the path here (in registry):
[HKEY_CURRENT_USER\SOFTWARE\Microsoft\Microsoft SQL Server\UserInstances]
The path is not a part of your connection string, so do you really have a problem?

Visual Studio Install - Post-install where should I point local database connection string?

I have a major, two day head damaging question. I have a localDB for a desktop application coded in C#. The .mdf database file resides in the root directory for the application. When debugging in development this worked fine (using EF6). My connection string is as follows:
(LocalDB)\v11.0;attachdbfilename=|DataDirectory|\LocalStorage.mdf;integrated security=True;connect timeout=30
Yet when i build an installer and point my application as the 'Project Output' something happens whereby I receive the error upon successful install...
An attempt to attach an auto-named database for file C:\Program Files\Company Name\Program\LocalStorage.mdf failed. A database with the same name exists, or specified file cannot be opened, or it is located on UNC share.
It seems my database does not install to the location (it simply isnt there). I suspect the '|DataDirectory|' in conn string does not point to an internal.exe location? Help Please, im at my wits end.
Most cases like this are dealt with by having a dialog in the application that describes things about the database. That includes the security connection (Windows Integration or not) and perhaps the db name and location. I'd ask about that in a SQL forum rather than an installation forum. Although some installs sometimes ask users for that kind of thing in a dialog, most users would rather get the install finished and then worry about the configuration.

build connection string at runtime and save the same in application setting on c# windows app

I am building a C# windows application using sql server 2005 and visual studio 2008.
To deploy the application on clients computer. I do this this in this order
I install the sql server 2005 with the same user/pass (ie sa/pass) i made in my app.
I run my windows application msi file. It install my databasae in sql server 2005.
when I try to run the application I fail due to different machine name and sql server instance name on my clients computer. i am getting error in connection string. i need a way to extract the target computername and sql server instance and save the same in my application setting during deployment (using in my c# code so that i can make a connection string at runtime.)
To resolve the problem what i am doing is I am installing visual studio on my client's computer and editing the source code to change the machine name and sql server instance. Then my windows application runs. I don't want to resolve my problem this way but rather need a way where i can set my parameters at deployment time or leave it up to you to suggest a better way.
I would request you to help me.
Google will give you lot of options. Have you followed the right steps while copying a database.
Follow these links http://blogs.msdn.com/b/sreekarm/archive/2009/09/11/move-a-database-from-one-server-to-another-server-in-sql-server-2008.aspx
and this will answer yours DBA Stack Exchange
Also post the Exact error message you get.It ll be useful to figure out the issue
Using SQL CE will require a lot of rework.
The full install order is:
Install Windows Installer 3.1 (if its not installed)
Install .Net 2.0 (if its not installed)
Check if SQL is NOT already installed:
HKLM "SOFTWARE\Microsoft\Microsoft SQL
Server\${instance_name}\MSSQLServer\CurrentVersion" "CurrentVersion"
Then you need to install SQL Server:
${installer_path}\SQLEXPR.exe -q /norebootchk /qn
reboot=ReallySuppress addlocal=all INSTANCENAME=${instance_name}
SAPWD=${password} SECURITYMODE=SQL SQLBROWSERAUTOSTART=1
SQLAUTOSTART=1 AGTAUTOSTART=1 ASAUTOSTART=0 RSAUTOSTART=0
DISABLENETWORKPROTOCOLS=0 ERRORREPORTING=1 SQMREPORTING=0 ENABLERANU=0
ADDUSERASADMIN=1
To connect to the SQL Server instance use your data link properties dialog. You will need to give clear instruction to your users how to connect to the instance. eg
If you use the .\Instance_Name syntax where the "." dot represents the client PCs name that should solve the problem. Also use this in your connection string.
So, if I understand you correctly, you have hardcoded your connection string inside your source code. And of course, you have discovered at your expense how this is really a bad idea.
You need to have that connection string saved in your app.config file. In this way, when you install your application to your customer, you need only to change the config file and your code is ready to go.
To insert your connection string in your app.config:
right click on your project and select Properties, then go to the
Settings page. This will create a settings.settings file for your
project and will open a GUI editor for your properties.
Now insert a string in the column name (for example
testConnection)
Select from the Type column the ConnectionString type
Insert, in the value column, a connection string valid for your
development machine
Now if you open the app.config you will notice a new section called ConnectionStrings that will look like this:
<connectionStrings>
<add name="ProjectName.Properties.Settings.testConnection" connectionString="Provider=Microsoft.Jet.OLEDB.4.0;Data Source=D:\temp\mytemp.mdb;User Id=admin;Password=;" />
</connectionStrings>
Now it is time to fix your code once and forever.
In every place where you have an hardcoded connection string replace that code with something like this
string constring = ConfigurationManager.ConnectionStrings["ProjectName.Properties.Settings.testConnection].ConnectionString;
And while we are here, check if the LocalDB version of SqlServer Express 2012 could be used to remove also the need to install SqlServer.

Setup a test environment on my local machine using Visual Studio

What is the easiest way to setup a test environment on my local machine using Visual Studio 2008 with a website that that has a mssql database, which is hosted through a webhost?
I am web designer and I am re-skinning a live website that is built in .NET C#. I have access to the files and when I run it in Visual Studio (localhost) only the static files are obviously pulling up , which is problematic for testing. I need to be able to run the entire website on my local. I am not going to be messing with the database at all, but I know that I am going to need it to be able to have a local copy that works. I am not extremely savvy on these types of things. I was hoping that someone could either point me in the right direction (ie. search terms, keywords) or give me some instructions on how to make this work. Any help is appreciated.
Thanks.
You should set up MSSQL server first. You can take backup from your production database using
RMC on DB -> Tasks -> Backup..
Then you use the generated .bak file on your local server to insert the copy of the database into your SQL Server instance like this:
RMC on Databases -> Restore Database -> Path to your backup..
At this step your instance should contain the database with all the tables and data. Next thing to do would be to change your web.config file connection string (or any place else, where the "Connection string" is set pointing the website to the database) accordingly to your instance. If it's MSSQLSERVER instance name, you can just use following connection string:
Data Source=(local);Initial Catalog=<database name>;Integrated Security=True;
After this compiling and running your source codes should be returning you the site in its fullest.

WebService to use ODP.NET from a File Share

I have a C# ASMX web service that uses Oracle.DataAccess.Client to connect to an Oracle database. When I try to call a web method I get this error message:
unable to load dll 'OraOps10.dll'
It's the same problem as asked before, with the significant difference that I do have ODAP (at least I have OraOps10.dll in an Oracle client installation's bin directory), but it is installed on a File Share (W:), and not registered on my machine (where the web service runs on VisualStudio's test web server).
Even when I tried and copied the said DLL into the web service bin/ directory, it wouldn't find it. The stacktrace sounds like it is looking for it in the registry.
Is there any way I can tell my web service where to find the ODAP installation? (Set the ORACLE_HOME in the web.config or similar)
(Please note: A working Oracle client installation is available on every target machine and it works fine to open an Oracle connection with System.Data.Oracle, only that client installation seems not to include the ODAP: I cannot find any "Oracle" entry in "Add Reference -> .NET".*)
If I am reading this correctly, it sounds like you have the Oracle Client installed locally but not the ODP.net client installed locally -- rather it is 'installed' on a different computer but it uses it's ORA home as the shared 'W:' drive.
Do you know what type of ODP installation is on the W: drive?
if it is XCOPY (but you may also be safe to try this out with a classic install) you may be safe with simply adding the PATH variables: (as always, before monkeying around with the PATH or the registry BACK IT UP!)
http://www.oracle.com/technetwork/oramag/2008/08-nov/o68odpnet-101744.html
Step 8:
The last step in installing Oracle
Data Access Components and Oracle
Database Instant Client on the
developer machine is to add two items
to the Path environment variable.
Right-click the My Computer icon and
select Properties from the context
menu. Next, click the Advanced tab in
the System Properties dialog box and
then click the Environment Variables
button. In the System variables group,
locate the Path variable in the list
(scroll down if necessary). Click the
Path variable in the list and then
click Edit. In the Edit System
Variable dialog box, place the cursor
at the beginning of the entries and
type
C:\oracle\11.1\odac;C:\oracle\11.1\odac\bin;
. Click OK to close the Edit System
Variable dialog box, click OK to close
the Environment Variables dialog box,
and finally click OK again to close
the System Properties dialog box
As for the registry; check
HLM\SOFTWARE\ORACLE\ODP.NET
A sure fire way to get it to work is to install ODP locally onto the test box, but the PATH variables should be all that you need. You can find the files #
http://www.oracle.com/technetwork/topics/dotnet/whatsnew/index.html

Categories

Resources