Click here to Skip to main content
15,884,176 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
Hi, so i'm making a database which is for a film system. I've made the database in SQL, now what I want to do is to display information from the database in a console application using ado.net. Now i've put the code in for this work and it builds successfully but when it runs no data is displayed. Now I think this has something to do with the data source name, the server name i've used in SQL is MASTER\MASTER, but when I entered this in for the data source name it came up with an error message saying "Unregonized Escape Sequence". So to recify this I put in two backslashs instead of one so it looked like this MASTER\\MASTER. This resolved the error, but the program still doesnt work.

Heres the code i've used for it so you can see if it's this problem or something else that might be causing my program not to work.

class Program
    {
        static void Main(string[] args)
        {
            try
            {
                // set up data connection
                string cs = "Data Source=MASTER\MASTER;Initial Catalog=FilmDB;Integrated Security=True;Pooling=False";
                SqlConnection cn = new SqlConnection(cs);
                // Set up SQL command
                SqlCommand cm = cn.CreateCommand();
                ///
                ///Sql Select Command (Read table and return data)
                ///
                cm.CommandText = "SELECT FilmID, FilmName FROM [dbo].[Film];";
                // Set up adapter manager
                SqlDataAdapter da = new SqlDataAdapter();
                da.SelectCommand = cm;
                //Set dataset for results
                DataSet ds = new DataSet();
                ds.Clear();
                //Open Connection to database
                cn.Open();
                //Ask adapter to transfer data from table to dataset
                da.Fill(ds, "Film");
                //Set up data table
                DataTable dt = ds.Tables["Film"];
                //Read data from table rows
                foreach (DataRow dr in dt.Rows)
                {
                    Console.WriteLine(dr["FilmID"].ToString() + "\t" + dr["FilmName"].ToString());
                }
                //Close connection to database
                cn.Close();
            }
            catch (Exception ex)
            {
                Console.WriteLine("Error: " + ex.Message);
            }
            finally
            {
                //cn.close();
            }
        }
    }
Posted
Updated 24-Feb-11 6:38am
v3

To me it looks like your connection string is incorrect. The simple reason is I think you are confused with the data source and I don't think that you would be having a server name Master.

Have a look at http://www.connectionstrings.com/. This should help you building correct connection string.
 
Share this answer
 
Firstly, either you will need two '\' characters together or an '@' in front of the string:
C#
string cs = "Data Source=MASTER\\MASTER;Initial Catalog=FilmDB;Integrated Security=True;Pooling=False";


C#
string cs = @"Data Source=MASTER\MASTER;Initial Catalog=FilmDB;Integrated Security=True;Pooling=False";

In a string, '\' introduces an escape sequence - a character you can't enter normally such as double quote, or new line - Double backslash is the escape sequence for backslash. The '@' turns backslash escaping off.

Secondly, check your connection string: if you click on a database in the Server Explorer, the appropriate connection string is shown in the properties pane.

When you say "the program still doesnt work" what do you mean? Do you get an exception? If so what? If not, what does happen that you aren't expecting, or does happen that you aren't?
 
Share this answer
 
Comments
programmer1234 24-Feb-11 12:37pm    
It works now, I put the @ in front of the code and it displayed the info I wanted. Thanks for your help.
OriginalGriff 24-Feb-11 14:40pm    
Welcome!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900