Visual C#.NET 2005 Express: server=.\SQLEXPRESS is unrecognized escape sequence in the C#-ADO.NET programming
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 Snippetusing
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 objectDataAdapter.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 Snippetusing
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
Answers
Your connection string must be escaped this way:
Code Snippetstring connectionString = "Server=.\\SQLEXPRESS;" +
"Trusted_Connection=yes;database=pubs";
or this way:
Code Snippetstring connectionString = @"Server=.\SQLEXPRESS;" +
"Trusted_Connection=yes;database=pubs";
Backslash "\" must be escaped in C#.
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
Your connection string must be escaped this way:
Code Snippetstring connectionString = "Server=.\\SQLEXPRESS;" +
"Trusted_Connection=yes;database=pubs";
or this way:
Code Snippetstring connectionString = @"Server=.\SQLEXPRESS;" +
"Trusted_Connection=yes;database=pubs";
Backslash "\" must be escaped in C#.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
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.
- 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 - love u! xD this was the aswers
omg! thx 3 hours and nothing!
thx bro


