none
Visual C#.NET 2005 Express: server=.\SQLEXPRESS is unrecognized escape sequence in the C#-ADO.NET programming RRS feed

  • Question

  • Hi all,

     

    I have Visual C#.NET 2005 Express and SQL Server Management Studio Express (SSMSE) in the terminal PC of our LAN NT 4 Network System.

     

    This is my first Visual C#.NET 2005 Express-ADO.NET programming. I created a Windows Application project 'scWorkingWithADONetpubs' with the following code:

    ////--Form1.cs---/////

    Code Snippet

    using System;

    using System.Collections.Generic;

    using System.ComponentModel;

    using System.Data;

    using System.Data.SqlClient;

    using System.Drawing;

    using System.Text;

    using System.Windows.Forms;

    namespace scWorkingWithADONETpubs

    {

       public partial class ADONetForm1 : Form

         {

          public ADONetForm1()

              {

                InitializeComponent();

                // connect to my local server, pubs database

                   string connectionString = "server=.\SQLEXPRESS;" +

                     "Trusted_Connection=yes; database=pubs";

               //get records from the authors table

                     string commandString = "Select au_lname, au_fname from pubs";

               // create the data set command object and the DataSet

                     SqlDataAdapter DataAdapter =

                          new SqlDataAdapter(commandString, connectionString);

                     DataSet DataSet = new DataSet();

               // fill the data set object

                     DataAdapter.Fill(DataSet, "authors");

              // Get the one table from the DataSet

                     DataTable dataTable = DataSet.Tables[0];

             // for each row in the table, display the info

                     foreach (DataRow dataRow in dataTable.Rows)

                    {

                       lbAuthors.Items.Add(dataRow["au_lname"] +

                              " (" + dataRow["au_fname"] + ")");

                    }

               }

          }

    }

     

     

    /////--Program.cs--/////////

    Code Snippet

    using System;

    using System.Collections.Generic;

    using System.Windows.Forms;

    namespace scWorkingWithADONETpubs

    {

            static class Program

             {

                /// <summary>

                /// The main entry point for the application.

                /// </summary>

               [STAThread]

               static void Main()

               {

                 Application.EnableVisualStyles();

                 Application.SetCompatibleTextRenderingDefault(false);

                 Application.Run(new ADONetForm1());

               }

            }

    }

     

     

    Immediately after typing the code, I saw an error "unrecognized escape sequence" that was associated with the "server=.\SQLEXRESS;..." I put in the following code statement "string connectionString = "server=.\SQLEXPRESS;" + "Trusted_Connection=yes; database=pubs";.  I just started the Visual C#.NET 2005 Express - ADO.NET programming. Please help and tell me what is wrong with that code statement and how to correct this error.

     

    Thanks in advance,

    Scott Chang

    Monday, October 6, 2008 5:56 PM

Answers

  • Your connection string must be escaped this way:

    Code Snippet

     

                   string connectionString = "Server=.\\SQLEXPRESS;" +

                     "Trusted_Connection=yes;database=pubs";

     

     

    or this way:

    Code Snippet

     

                   string connectionString = @"Server=.\SQLEXPRESS;" +

                     "Trusted_Connection=yes;database=pubs";

     

     

    Backslash "\" must be escaped in C#.
    Monday, October 6, 2008 8:48 PM

All replies

  • I changed it to "server=SQLEXPRESS" and executed this project.  I got a new error"

       SqlException was unhandled

       An error has occurred while establishing to the server.  When connecting to SQL Server 2005, this failure may be caused by the fact that under the default settings SQL server does not allow remote connection.

     

    This error is pointing to the following code statement:   DatyaAdapter.Fill(DataSet, "authors");

     

    I did not know how it happened - I had the administrator previledge to use the Visual C#.NET 2005 and SSMSE before.  I wonder whether our NT4 LAN System was updated few times and my access to SSMSE was skew up!!!???  Please help and advise me what is going on in my program.

     

    Thanks,

    Scott Chang

    Monday, October 6, 2008 6:43 PM
  • Your connection string must be escaped this way:

    Code Snippet

     

                   string connectionString = "Server=.\\SQLEXPRESS;" +

                     "Trusted_Connection=yes;database=pubs";

     

     

    or this way:

    Code Snippet

     

                   string connectionString = @"Server=.\SQLEXPRESS;" +

                     "Trusted_Connection=yes;database=pubs";

     

     

    Backslash "\" must be escaped in C#.
    Monday, October 6, 2008 8:48 PM
  • Hi Oleg,  Thanks for your response.

     

    I used 'string connectionString = "Server=.\\SQLEXPRESS;" +

                     "Trusted_Connection=yes;database=pubs";'

    in my project and I got a different error message: 

              SQLExption was unhandled

              Invalid object name 'pubs'.

    which is pointing to the 'DataAdapter.Fill(Dataset, "authors")' code statement;.

     

    I am lost in this problem.  Please help and advise me how I should do to resolve this problem.

     

    Thanks,

    Scott Chang 

      

    Tuesday, October 7, 2008 11:51 AM
  • I found a mistake in the code statement 'string commanding = "Select au_lname, au_fname from pubs":'.

    I changed the "pubs" to "authors" in that code statement and I executed the project. It ran nicely and gave me the right output.

     

    Scott Chang.

    Wednesday, October 8, 2008 5:49 PM
  • About  the line DataAdapter.Fill(DataSet, "authors");

    when I try to execute the code then I got an error cannot connect to the database...

    what should I do or how could I change the "authors"

    please reply this message....plz
    Tuesday, October 14, 2008 6:07 PM
  • love u! xD this was the aswers
    omg! thx 3 hours and nothing!
    thx bro
    Sunday, November 1, 2009 5:56 PM