Search code examples
c#asp.net-mvcsqlconnection

Keyword not supported: '"data source'. in MVC


I'm running into this error

([ArgumentException: Keyword not supported: ''data source'.])

in my MVC app as I try to run a standard SQL Query in my controller without using EF. I've read other articles which talk about escaping quotes but that has not been of any avail thus far. My Connection code is as follows:

userDataQry = -->Long SQL Query contained in this variable <---;
connString ="\"Data Source = data.testdomain.com; Initial Catalog = DashboardData; IntegratedSecurity = True; Application Name = DMetricsApp; \"providerName=\"System.Data.SqlClient\""; 

C# Sql Connection Code:

using(SqlConnection conn = new SqlConnection(connString))
{
    using(SqlCommand objCommand = new SqlCommand(userDataQry, conn))
    {
        objCommand.CommandType = CommandType.Text;
        DataTable dt = new DataTable();
        SqlDataAdapter sdp = new SqlDataAdapter(objCommand);
        conn.Open();
        sdp.Fill(dt);

        if (dt != null)
        {
            list = dt.AsEnumerable().ToList();
        }//End if
    }//End using
}//End using

Solution

  • Don't put double quotes at the beginning and at the end of the connection string

    connString =@"Data Source=data.testdomain.com;
                  Initial Catalog=DashboardData;
                  IntegratedSecurity = True; 
                  Application Name = DMetricsApp;";
    

    Also remove the provider name part. It is not needed when you use the classes in System.Data.SqlClient