1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Connection string problem

Discussion in 'C#' started by Carlos, Nov 6, 2009.

  1. Carlos

    Carlos New Member

    Joined:
    Dec 5, 2008
    Messages:
    57
    Likes Received:
    0
    Trophy Points:
    0
    Occupation:
    none
    Hi! i wanna access sql database through my C# console app. but it don't produce output.
    plz see my code
    Code:
    static void Main(string[] args)
            {
                string source =@"Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\NORTHWIND.mdf;Integrated Security=True;User Instance=True";
    
    
    
                string query = "Select * from Customers";
                SqlConnection conn = new SqlConnection(source);
                try
                {
                    conn.Open();
                    SqlCommand cmd = new SqlCommand(query, conn);
    
                    int rowaffected = cmd.ExecuteNonQuery();
    
                    if (rowaffected != 0)
                        Console.Write(rowaffected.ToString());////output
                    else
                        Console.Write("No row affected");
                }
                catch (SqlException e)
                {
                    Console.Write(e.ToString());
                }
                finally
                {
                    conn.Close();
                }
                
               
    
            }
    
    I have placed a database Northwind.mdf in C:\SQL Server 2000 Sample Databases folder, and i have added a dataconnection to this through server Explorer.

    Please tell me where i am wrong ?? why it don't give output.
     

Share This Page