about how to create a connection query database operation in the C # console window

Source: Internet
Author: User

First, you need to create a new table, in order to test with a student table

Once you've created a new table, you're ready to manipulate the database.

Lists the commonly used additions and deletions to search

Operation

static void Main (string[] args)
{

String str1= "";
SqlConnection Sqlcon = new SqlConnection ("Data source=.;i Nitial Catalog=db_user; Persist Security info=true; User Id=sa; Password=qwerty ");
SqlCommand sqlcom = new SqlCommand ();
Sqlcon. Open ();
Sqlcom. Connection = Sqlcon;
Console.WriteLine ("Please follow the instructions below");
Console.WriteLine ("1. Check all student Information");
Console.WriteLine ("2. Specify the age of the specified student");
Console.WriteLine ("3. Update student Information");
Console.WriteLine ("4. Delete Student Information");
Console.WriteLine ("5. Increase student Information");
String Check=console.readline ();

if (check = = "1")
{

Sqlcom.commandtext = "SELECT * from Tb_stu";
SqlDataReader read = sqlcom. ExecuteReader ();
while (read. Read ())
{
String a = read. GetValue (0). ToString ();
String a1 = read. GetValue (1). ToString ();
String a2 = read. GetValue (2). ToString ();
Console.WriteLine ("ID:" + A + "name:" + A1 + "Age:" + A2);
}
}

sqlcom = null;
Sqlcon. Close ();
Console.ReadLine ();

}

The inserted code

Sqlcom.commandtext = "INSERT into Tb_stu (name,age) VALUES ('" +name+ "', '" +age+ "')";

Code to remove

Sqlcom.commandtext = "Delete from Tb_stu where name=+ '" + name + "'";

Where Condition Query

Sqlcom.commandtext = "Update tb_stu set name= '" + str2+ "' Where Name= '" +name+ "'";

Update action

Sqlcom.commandtext = "Select age from Tb_stu where Name= '" +name+ "'";

about how to create a connection query database operation in the C # console window

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.