0

I am probably missing something silly...but iv'e been trying to figure it out all day so I give up. can anyone spot the error?

App.config

  <connectionStrings>
<add name="linkednDb" connectionString="Server=.;Database=linkedin.usernames;Uid=root;Pwd=1234;" providerName="System.Data.SqlClient"/>

Query method :

public void AddUser(string username)
    {
        using (IDbConnection connection = new System.Data.SqlClient.SqlConnection(Helper.Connection("linkednDb")))
        {
            connection.Query<User>($"INSERT INTO usernames (UserName) VALUES ('{username}')");
            Console.WriteLine("adding" + username);
        }
    }

Helper class:

 public static class Helper
{
    public static string Connection(string name)
    {
     return ConfigurationManager.ConnectionStrings[name].ConnectionString;  
    }
}

the table

FORGOT TO ADD error!

System.Data.SqlClient.SqlException: '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)'
6
  • what's the error message Commented Nov 13, 2019 at 14:13
  • Do you get an error message, unexpected results, something else? Are you using Dapper? Dapper doesn't use string interpolation because it's a bad idea. For Dapper, this remains something we wouldn't add - it's not worth the safety trade-offs :) Instead of a parameterized query you're executing a dynamically generated string that could easily be invalid Commented Nov 13, 2019 at 14:15
  • 1
    For example, what if the user name is O'Reilly? You'd get INSERT INTO usernames (UserName) VALUES ('O'Reilly') and an invalid query error Commented Nov 13, 2019 at 14:15
  • 1
    System.Data.SqlClient is the SQL Server client. You can't use it to connect to MySQL Commented Nov 13, 2019 at 14:21
  • 2
    @fubo yes, and the question is marked mysql, while the code uses a MySQL connection string. Commented Nov 13, 2019 at 14:21

1 Answer 1

2

System.Data.SqlClient is the ADO.Net provider for Sql Server, which is a different database platform entirely from MySql.

You need a MySql provider. This provider is not included with .Net out of the box, but there are a few options out there you can install and reference in Visual Studio.

While I'm here, that string substitution in the SQL code is NOT OKAY. String-substitution in SQL queries are crazy-vulnerable to SQL injection issues. This is one of the big ways applications end up hacked. Less-malicious issues can occur, too; it doesn't take an attack by a l33t hacker for this cause problems. Regular old Joe user can break an app doing this just by putting in real data. You need to learn how to use parameterized queries.

Sign up to request clarification or add additional context in comments.

1 Comment

Alright first problem is fixed, gonna change the statement now, thanks!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.