none
Hi problems with connection strings RRS feed

  • Question

  • I am try to write an insert sql query.

    I have created a database called  cedDB.mdf.

    The connection string that is created is 'Data Source=(LocalDB)\v11.0;AttachDbFilename="C:\Users\Graeme\Documents\visual studio 2012\Projects\CobolED\CobolED\cedDB.mdf";Integrated Security=True'. When I used this it threw up unrecognized escape codes.

    So I tried the below

                SqlConnectionStringBuilder myBuilder = new SqlConnectionStringBuilder();

                myBuilder.DataSource = "(LocalDB)\v11.0";
                myBuilder.AttachDBFilename = "cedDB.mdf";
                myBuilder.IntegratedSecurity = true;

                SqlConnection connection = new SqlConnection();
                connection.ConnectionString = myBuilder.ConnectionString;
                
                connection.Open();

                string tmpSql= "";

                tmpSql = "INSERT INTO programTbl (programNameTxt) Values (@ProgramName)";
                SqlCommand cmd = new SqlCommand(tmpSql,connection);
                cmd.Connection = connection;
                cmd.Parameters.Add("@ProgramName", SqlDbType.VarChar, 50).Value = txtProgramName.Text;
                cmd.ExecuteNonQuery();

    When the code ran it produced this error

    An unhandled exception of type 'System.Data.SqlClient.SqlException' occurred in System.Data.dll.

    Please can someone help me understand this problem, I know it is most likely a simple thing i have overlooked.

    Thank you.

    Monday, July 1, 2019 10:44 AM

Answers

  • Done that but it created this connection string "Data Source=(LocalDB)\v11.0;AttachDbFilename=|DataDirectory|\cedDB.mdf;Integrated Security=True" but claimed that the \cedDB.mdf have an unrecognized escape sequence.

    Try

    var connectionString = @"Data Source=(LocalDB)\v11.0;AttachDbFilename=|DataDirectory|\cedDB.mdf;Integrated Security=True;User Instance=True";


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    • Marked as answer by Graeme EA Monday, July 1, 2019 2:31 PM
    Monday, July 1, 2019 12:03 PM
    Moderator

All replies

  • Hello,

    If this error is on the connection string try the following which will validate or invalid the connection string using a wizard.

    In a new temp Windows forms project

    • From the ide menu select View -> Other windows
    • Select Data Sources
    • Select Add new data source.
    • Dialog pop up select Next twice
    • Select New connection then select Test connection, if this works continue, if not stop.
    • Select one table, does not matter which then press Finish
    • Open App.config, the new connection string is there
    • Use this connection string (or examine and adapt to your current connection string).
    • Delete the temp project.

    Report back 


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    Monday, July 1, 2019 11:00 AM
    Moderator
  • Done that but it created this connection string "Data Source=(LocalDB)\v11.0;AttachDbFilename=|DataDirectory|\cedDB.mdf;Integrated Security=True" but claimed that the \cedDB.mdf have an unrecognized escape sequence.
    Monday, July 1, 2019 11:21 AM
  • An unhandled exception of type 'System.Data.SqlClient.SqlException' occurred in System.Data.dll.

    This is not enough information. You should be looking at the inner.exception.message if there that would only show in the stack trace that could give the possible reason as to why the exception was thrown.

    And what is this connectionstring builder? Where is it getting the connectionstring from?

    Monday, July 1, 2019 11:22 AM
  • Done that but it created this connection string "Data Source=(LocalDB)\v11.0;AttachDbFilename=|DataDirectory|\cedDB.mdf;Integrated Security=True" but claimed that the \cedDB.mdf have an unrecognized escape sequence.

    Try

    var connectionString = @"Data Source=(LocalDB)\v11.0;AttachDbFilename=|DataDirectory|\cedDB.mdf;Integrated Security=True;User Instance=True";


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    • Marked as answer by Graeme EA Monday, July 1, 2019 2:31 PM
    Monday, July 1, 2019 12:03 PM
    Moderator
  • It worked thanks for your help
    Monday, July 1, 2019 2:33 PM