Attempts to open an SQL Connection to a database on a local drive result in System.Data.SqlClient.SqlException

1

I am working in Visual Studio (2019 V 16.8.4) using c#. All components are up-to-date.

Working within a newly created "Windows Forms App (.Net Framework)" project, I am trying to create a simple database (which I successfully generated in VS) and access it through a single V.S. form that has one button. The button invokes a click event which is intended to make a connection to the database (*.mdf) located at c:\sub_dir.

Each time debug is executed and the button is clicked, an exception occurs when "cnn.Open();" is executed. Essentially, it appears that the database file cannot be located ... no matter where I locate it. I emphasise that I want to work on my local hard drive and I am beginning to wonder if that is part of the problem. Or might it be the configuration of my PC?

What I have tried so far: The problem clearly lies in the connection string and execution of "cnn.Open();". I have tried dozens of configurations for the string and tried many, many sites for solutions, including Stack Overflow. None of the solutions are successful for me. I have looked specifically at how to structure the connection string and I have run through literally dozens of tutorials (most recently Guru99 whose copied code is shown below).

This is my first attempt at accessing data via SQL so I am new to this. I'll be grateful for whatever help is available. If this problem proves to be unresolvable I intend to try c# file handling as an alternative.

Details:

Form code is as follows:

NOTE: I couldn't get the 'using' statements to conform to your required formatting so omitted them.

namespace DemoApplication1  
{  
    public partial class Form1 : Form  
    {  
        public Form1()  
        {  
            InitializeComponent();  
        }  
  
        private void button1_Click(object sender, EventArgs e)  
        {  
            string connectionString;  
            SqlConnection cnn;  
            connectionString = @"Data Source=WIN-50GP30FGO75;Initial Catalog=Test1;User ID=sa;Password=demol23";  
            cnn = new SqlConnection(connectionString);  
            cnn.Open();  
            MessageBox.Show("Connection Open  !");  
            cnn.Close();  
        }  
    }  
}  

Exception Details

System.Data.SqlClient.SqlException  HResult=0x80131904

  Message=A network-related or instance-specific error occurred while establishing 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. (provider: Named Pipes Provider, error: 40 - Could not open a connection to SQL Server)
  Source=.Net SqlClient Data Provider
  StackTrace:
   at System.Data.SqlClient.SqlInternalConnectionTds..ctor(DbConnectionPoolIdentity identity, SqlConnectionString connectionOptions, SqlCredential credential, Object providerInfo, String newPassword, SecureString newSecurePassword, Boolean redirectedUserInstance, SqlConnectionString userConnectionOptions, SessionData reconnectSessionData, DbConnectionPool pool, String accessToken, Boolean applyTransientFaultHandling, SqlAuthenticationProviderManager sqlAuthProviderManager)
   at System.Data.SqlClient.SqlConnectionFactory.CreateConnection(DbConnectionOptions options, DbConnectionPoolKey poolKey, Object poolGroupProviderInfo, DbConnectionPool pool, DbConnection owningConnection, DbConnectionOptions userOptions)
   at System.Data.ProviderBase.DbConnectionFactory.CreatePooledConnection(DbConnectionPool pool, DbConnection owningObject, DbConnectionOptions options, DbConnectionPoolKey poolKey, DbConnectionOptions userOptions)
   at System.Data.ProviderBase.DbConnectionPool.CreateObject(DbConnection owningObject, DbConnectionOptions userOptions, DbConnectionInternal oldConnection)
   at System.Data.ProviderBase.DbConnectionPool.UserCreateRequest(DbConnection owningObject, DbConnectionOptions userOptions, DbConnectionInternal oldConnection)
   at System.Data.ProviderBase.DbConnectionPool.TryGetConnection(DbConnection owningObject, UInt32 waitForMultipleObjectsTimeout, Boolean allowCreate, Boolean onlyOneCheckConnection, DbConnectionOptions userOptions, DbConnectionInternal& connection)
   at System.Data.ProviderBase.DbConnectionPool.TryGetConnection(DbConnection owningObject, TaskCompletionSource`1 retry, DbConnectionOptions userOptions, DbConnectionInternal& connection)
   at System.Data.ProviderBase.DbConnectionFactory.TryGetConnection(DbConnection owningConnection, TaskCompletionSource`1 retry, DbConnectionOptions userOptions, DbConnectionInternal oldConnection, DbConnectionInternal& connection)
   at System.Data.ProviderBase.DbConnectionInternal.TryOpenConnectionInternal(DbConnection outerConnection, DbConnectionFactory connectionFactory, TaskCompletionSource`1 retry, DbConnectionOptions userOptions)
   at System.Data.ProviderBase.DbConnectionClosed.TryOpenConnection(DbConnection outerConnection, DbConnectionFactory connectionFactory, TaskCompletionSource`1 retry, DbConnectionOptions userOptions)
   at System.Data.SqlClient.SqlConnection.TryOpenInner(TaskCompletionSource`1 retry)
   at System.Data.SqlClient.SqlConnection.TryOpen(TaskCompletionSource`1 retry)
   at System.Data.SqlClient.SqlConnection.Open()
   at DemoApplication1.Form1.button1_Click(Object sender, EventArgs e) in C:\Guru99\DemoApplication1\DemoApplication1\Form1.cs:line 27


   at System.Windows.Forms.Control.OnClick(EventArgs e)
   at System.Windows.Forms.Button.OnClick(EventArgs e)
   at System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)
   at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
   at System.Windows.Forms.Control.WndProc(Message& m)
   at System.Windows.Forms.ButtonBase.WndProc(Message& m)
   at System.Windows.Forms.Button.WndProc(Message& m)
   at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
   at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
   at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
   at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
   at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(IntPtr dwComponentID, Int32 reason, Int32 pvLoopData)
   at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
   at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
   at System.Windows.Forms.Application.Run(Form mainForm)
   at DemoApplication1.Program.Main() in C:\Guru99\DemoApplication1\DemoApplication1\Program.cs:line 19

Inner Exception 1:
Win32Exception: The network path was not found
c#
.net
sql-server
windows
asked on Stack Overflow Jan 18, 2021 by Ian • edited Jan 18, 2021 by Larnu

1 Answer

0

Did you create a Service-Based Database that generated a .mdf file?

If so, to get your connection string, please refer to:

https://docs.microsoft.com/en-us/visualstudio/data-tools/create-a-sql-database-by-using-a-designer?view=vs-2019

answered on Stack Overflow Jan 19, 2021 by tinyDanza

User contributions licensed under CC BY-SA 3.0