I'm totally new in C#. I'm having troubles with connecting my application with a SQL Server database, the problem occurs when I click the button which checks or updates my database.
I tried with 2 connection string methods I found on internet but neither of them work. Now maybe I understood it wrongly. I think it could be the SERVER
part of code or the other version Data Source
where I made a mistake but not quite sure. Basically I've put my VPS' IP address there (which I use in nodejs to connect to database as well).
Those are my 2 connection string methods I used which doesn't work:
const string connectionString = "SERVER = **.***.76.9; DATABASE = dbName; USER ID = root; PASSWORD =myPassword";
const string connectionString = "Data Source=**.**.76.9; Initial Catalog = dbName; User ID = root; Password=myPassword";
I'm also using sqlConn.Open();
to open connection which causes the error as I mentioned above.
System.Data.SqlClient.SqlException
HResult=0x80131904A 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, TaskCompletionSource1 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, TaskCompletionSource1 retry, DbConnectionOptions userOptions) at System.Data.ProviderBase.DbConnectionClosed.TryOpenConnection(DbConnection outerConnection, DbConnectionFactory connectionFactory, TaskCompletionSource
1 retry, DbConnectionOptions userOptions) at System.Data.SqlClient.SqlConnection.TryOpenInner(TaskCompletionSource1 retry) at System.Data.SqlClient.SqlConnection.TryOpen(TaskCompletionSource
1 retry) at System.Data.SqlClient.SqlConnection.Open() at myProject.Forms.activation.isActivated(String key) in C:\Users\PC\Desktop\myProject\WindowsClient\myProject\Forms\activation.cs:line 24 at myProject.Forms.activation.activateSoftware(String key) in C:\Users\PC\Desktop\myProject\WindowsClient\myProject\Forms\activation.cs:line 35 at myProject.Forms.LoginForm.button1_Click(Object sender, EventArgs e) in C:\Users\PC\Desktop\myProject\WindowsClient\myProject\Forms\LoginForm.cs:line 100 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 myProject.Program.Main() in C:\Users\PC\Desktop\myProject\WindowsClient\myProject\Program.cs:line 21Inner Exception 1:
Win32Exception: The network path was not found
This is my code:
public static bool isActivated(string key)
{
using (SqlConnection sqlConn = new SqlConnection(connectionString))
{
string checkForActivationQuery = "SELECT activated FROM activationTable WHERE serialKey =@key";
SqlCommand cmd = new SqlCommand(checkForActivationQuery, sqlConn);
cmd.Parameters.AddWithValue("@key", key);
sqlConn.Open();
int result = Convert.ToInt32(cmd.ExecuteScalar());
if (result > 0)
{
return true;
}
return false;
}
}