How to connect a console application to a database

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • femi ibitolu
    New Member
    • Oct 2010
    • 2

    How to connect a console application to a database

    i just built a console application, please i need someone to tell me if its possible
  • Subin Ninan
    New Member
    • Sep 2010
    • 91

    #2
    It is possible! You can connect to database exactly the same way like it is done in windows application or asp.net.

    Comment

    • femi ibitolu
      New Member
      • Oct 2010
      • 2

      #3
      please i wouldnt mind if you show a quick step on how to do it, because i am quite new to the .net world. though i am a fast learner.

      Comment

      • Subin Ninan
        New Member
        • Sep 2010
        • 91

        #4
        Suppose you have a table "students" in sql server like:

        Code:
        CREATE TABLE students
        (stuID int identity(1,1) primary key,
        sname varchar(20) not null)
        to insert records to this table:

        Code:
        SqlConnection con = new SqlConnection(specify  connection string here);
        
        SqlCommand cmd = new SqlCommand("INSERT INTO students VALUES(@sname)", con);
        
        Console.Write("Enter student name: ");
        
        SqlParameter sname = cmd.Parameters.Add("@sname", dbtype.varchar);
        
        sname.Value = Console.ReadLine();
        
        cmd.ExecuteNonQuery();

        Comment

        Working...