How to run .mdf database from LAN? - c#

I have a C# (.NET Framework 4.5 - MVS 2015) project which has a Service-based Database with a local generated .mdf file. My Microsoft SQL Server that I use has this version: 13.0.1601.5.
Everything works fine on the server PC. I share in LAN the application with the .mdf files, but the clients from the same LAN can't open the application. It seems that the connection with the DB is bad.
My connection string is formed like this:
public static string attachedDbFile = "AttachDbFilename=" + currentWorkinglocation + "NCI_DB.MDF;";
public SqlConnection mySqlConnection = new SqlConnection(#"Data Source=(LocalDB)\MSSQLLocalDB;" + attachedDbFile + "Integrated Security=True");
What am I missing, please?
Later Edit:
After following the steps provided by you guys:
My Data Source is Microsoft SQL Server now (and not MSQL Server Db file). I have attached the .mdf file to a database from my server (in MSQL Server Management Studio), the "Allow remote connection option" is checked and I have changed the connection string to:
public SqlConnection mySqlConnection = new SqlConnection(#"Data Source=(LocalDB)\MSSQLLocalDB;Initial Catalog=myDbName;Integrated Security=True");
Now, the application works on my local host (the PC that has the sql server installed) without the need of the .mdf file. It's ok.
But if the clients try to access the application from LAN, from my shared location, the app .exe crashes once again with this error message:
Did I missed something?
SOLUTION:
After following some pointers from our colleagues and after investigating further the problem the solution is:
- install sql server 2017 (full version ! - not express edition)
- install microsoft sql server management studio
- attach mdf to a database
- configure an account with rights for that database
- update the connection string according to this:
public SqlConnection mySqlConnection = new SqlConnection(#"Data Source=ip,port(1433-default);Initial Catalog=db_name;User ID=user;Password=pwd");

SQL server only supports database files on local disks (or more exotic links than LANs, such as iSCSI, etc). If you want to share access to a database across a local network, it's time to stand up a SQL Server instance that will own the file and then connect to that SQL Server client/server. Stop trying to access it as a file across the network.

Related

ORA-12541: TNS: No listener error when i change the datasource

I am changing the data source from the SQL server to Oracle.
There is a SQL Server (server1), Oracle Server (server2), Windows Service (Server3).
This windows service C#, do the job of picking the data from SQL table to a different table.
I deploy windows service in Server3.
For this in the code, I need to mention the oracle connection string instead of SQL which is in server3.
SQL Server connection string:
'Dim strConn As String = "Provider=SQLOLEDB; Data Source=support.sdfdf.com; Initial Catalog=countssss; User ID=sdfsdf; Password=*****"
Oracle connection string:
Dim strConn As String = "Data Source=(DESCRIPTION=(ADDRESS=(PROTOCOL=tcp)(HOST=10.xxx.xx.xxx)(PORT=1521))(CONNECT_DATA=(SERVICE_NAME=BI_HRxxxx)));User Id=jp;Password=*asdasd**;"
Dim conn As New OracleConnection(strConn)
The moment I changed the code, I am getting an error,
ORA-12541: TNS: No listener
I know this is happening because tns listener service is not running.
This tns listener service needs to be installed.
I have two questions:
Is oracle installation is mandatory or any other alternative without installation oracle?.
Is Entire Oracle installation(Full Package) is mandatory to deploy tns service in server3.
If yes where do I get the installation file.
If not, what is the package that will install the TNS service?
Is there any specific version that needs to install? means do I need to install the same version of the package for tns service in server3, which is the same version of the Oracle server running on Server2.

How to make ConnectionString work on client computer?

I created a setup for a management system in Visual Studio and I used a Microsoft SQL Server database file, but when I used it on the user's computer or on another computer, I get this message when I tried to connect to the database. What is the problem?
I used SQL Server 2017 Express.
My connection string to work on client's PC :
#"Data Source=.\SQLEXPRESS01;Integrated Security=True;User Instance=True;AttachDBFilename=|DataDirectory|gym.mdf";
I installed SQL Server 2017 Express on the client machine, but it didn't work.
What is the problem ?
By default, when you don't change any settings during installation, a SQL Server Express instance will get the SQLEXPRESS instance name - so try this:
Data Source=.\SQLEXPRESS;Integrated Security=True;User Instance=True;AttachDBFilename=|DataDirectory|gym.mdf
(not SQLEXPRESS01 - that's not standard)
The error you get means that the server is not correct.
First, you can try the following code to get installed ServerName and instanceName.
var instances = SqlDataSourceEnumerator.Instance.GetDataSources();
foreach (DataRow instance in instances.AsEnumerable())
{
Console.WriteLine(instance["ServerName"]);
Console.WriteLine(instance["InstanceName"]);
}
Second, you can use the following connectionstring to connect db file.
string connstr = #"Data Source=server\\instance;
AttachDbFilename=D:\Product.mdf;
Integrated Security=True;
Connect Timeout=2;";

SQL Server network interfaces error 52- Unable to locate a database at runtime installation

I am creating a POS system in Windows Forms (C#) in which I use a SQL Server database file (.mdf) to store items (completely offline). When I install the application on my computer, it works fine, but when I install it on my clients PC, an error happens:
(provider: SQL Network Interfaces, error: 52 - Unable to locate a Local Database Runtime installation"
I read somewhere that the problem is caused due to the fact that the connection string of the database of my client's PC is different. I tried to add the connection string dynamically on runtime but again it only worked on my computer.
Another reason that might be causing the problem is that I used 'server-based database' since local database option isn't available in Visual Studio 2017 for some reason.
Another solution I looked up stated that I should install SQL Server Express on my client's PC. That also failed (maybe I have to set it up in a way or something).
I also tried adding the database.mdf and database_log files in the setup folder.
Lastly I tried installing 3rd party installers (Advanced installers 15.8 and InstallShield Wizard in VS 2015) which also failed.
(I have provided the code for the connection of database taking place and the connection string)
public void ConnectToDB()
{
DBConnection = new SqlConnection(#"Data Source = (LocalDB)\MSSQLLocalDB; AttachDbFilename=C:\Users\SAIM NASSER\Desktop\app layer\data layer\Database1.mdf; Integrated Security = True");
DBConnection.Open();
ResultSet = new DataSet();
}
If I understand you correct, you want to use LocalDB
That means using Sql Server without installing a full sql server, but just the localdb part from sql server express.
For this to work you need to install the LocalDB Driver, which can be found here
https://www.microsoft.com/en-us/download/details.aspx?id=29062
You need only the ENU\x64\SqlLocalDB.MSI
This is the only thing you need to install in your clients computer, I believe it can also be installed silent, you have to research a bit for that.
And yes, you also should change the connection string on the clients computer, you need to alter it so it points to the MDF file on the clients computer, because that location will probably be different then on your computer
EDIT
To get the connection string working, you can try this
On the clients computer, create a text file and rename the extension to .udl
So for example you have a file test.udl
Now from explorer, double click it, this will open the datalink editor.
From here you can enter/choose different settings, and click on the test connection button.
Once you get a working connection, save it, and open this file with notepad.
Inside you will find the working connection string
Hope this helps

C# app with SqlClient does not work on another computer

I have written an application that uses SqlClient and a local mdf database. However, it does not work on another machine (throwing an exception that the server cannot be run) and I assume this is because there is no SQL server installed? But I thought that using local database and SqlClient elimites the need for the server, something like the old SQL CE. Is that the problem?
EDIT Exception: System.Data.SqlClient.SqlException (0x80131904): A network-related or instance-specific error occurred while extablishing a connection to SQL Server. The server was not found or was not accessible. Verify that the instance name is correct and that SQL Server is configured to allow remote connections
As for connection string, I keep the one generated by Visual Studio Express, just modify the path to use the app folder.
SqlConnection s = new SqlConnection(#"Data Source = (LocalDB)\MSSQLLocalDB; AttachDbFilename ="+ System.IO.Path.GetDirectoryName(Application.ExecutablePath)+ #"\Database1.mdf; Integrated Security = True");
Is there a simple way how to rework this so that my app does not require SQL server installed? All I need is to have it use the local database file in its folder.

Database connectivity error in Visual Studio?

I have lost connectivity using Visual Studio 2012 database...
when i try to reconnect and run the application then here us the Exception
NOTE: The previous file was deleted mistakenly, and trying to attach the new one it says it already exist?
what is the Solution?
What kind of database are you running? Specify:
If you have SQLEXPRESS Database:
First you need to change your Data Source name like this.
"Data Source=(LocalDB)\v11.0" to "Data Source=.\SQLEXPRESS"
To connect to your local SQL Server instance, issue the following string:
Server=.\SQLExpress;AttachDbFilename=C:\MyFolder\MyDataFile.mdf;Database=dbname Trusted_Connection=Yes;
To connect to your local SQL Server instance with a database file located in a data directory, issue the following string:
Server=.\SQLExpress;AttachDbFilename=|DataDirectory|mydbfile.mdf;Database=dbname Trusted_Connection=Yes;
To connect to LocalDB Automatic Instance, issue the following string:
Server=(localdb)\v11.0;Integrated Security=true;
To connect to LocalDB Automatic Instance with a specific data, issue the following string:
Server=(localdb)\v11.0;Integrated Security=true; AttachDbFileName=C:\MyFolder\MyData.mdf;
To connect to LocalDB named Instance, issue the following string:
Server=(localdb)\MyInstance;Integrated Security=true;
You need to get a string that matches your specific DB exactly, for it to connect.
In you connection string just add the 'database='. This prevents SQL server to create the auto-named database.

Categories

Resources