Connect SQL Server from c# console application

Posted on 2005-04-25
Last Modified: 2008-02-01
hai friends,
how can i connect sql server from c# console application.pls give some sample codes.

thank you.

Question by:beeravally
    LVL 13

    Accepted Solution

    You need to using System.Data.SqlClient or System.Data.Oledb. Here is an example that using System.Data.SqlClient namespace

    using System;
    using System.Data;
    using System.Xml;
    using System.Data.SqlClient;

    namespace TestData
          /// <summary>
          /// Summary description for Class1.
          /// </summary>
          class Class1
                /// <summary>
                /// The main entry point for the application.
                /// </summary>
                static void Main(string[] args)
                      // create a connection
                      SqlConnection cn = new SqlConnection("Data Source=(local);User ID=sa;Password=khongbiet;Initial Catalog=northwind;");
                      // query string
                      string strSql = "Select FirstName, LastName From Employees";
                      SqlDataAdapter adapter = new SqlDataAdapter(strSql, cn);
                      // create datatable to store data from database
                      DataTable tblCity = new DataTable("Employee");
                      // fill data to data table
                      // print record
                      foreach (DataRow drowData in tblCity.Rows)
                            Console.WriteLine("FirstName: {0} - Lastname {1}", drowData["FirstName"].ToString(), drowData["FirstName"].ToString());
    LVL 8

    Assisted Solution


    Featured Post

    How to improve team productivity

    Quip adds documents, spreadsheets, and tasklists to your Slack experience
    - Elevate ideas to Quip docs
    - Share Quip docs in Slack
    - Get notified of changes to your docs
    - Available on iOS/Android/Desktop/Web
    - Online/Offline

    Join & Write a Comment

    Introduction                                                 Was the var keyword really only brought out to shorten your syntax? Or have the VB language guys got their way in C#? What type of variable is it? All will be revealed.   Also called…
    This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
    To add imagery to an HTML email signature, you have two options available to you. You can either add a logo/image by embedding it directly into the signature or hosting it externally and linking to it. The vast majority of email clients display l…
    This video is in connection to the article "The case of a missing mobile phone (". It will help one to understand clearly the steps to track a lost android phone.

    729 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    22 Experts available now in Live!

    Get 1:1 Help Now