The database connects to ADO. NET additions and deletions change

Source: Internet
Author: User
Tags readline

ADO: Database access Technology
The data that the program operates on are variables or objects that exist in memory.
But there is a lot of data that exists in the database, and the data in the database exists on the hard disk

Function: The data in the database is taken into memory, so that the program can operate
Writes the in-memory data to the database

1, using System.Data.SqlClient; Reference namespaces

2. Establish database connection class
String str = "server=.; database=data0515;user=sa;pwd=123; "; /connection string
SqlConnection conn = new SqlConnection (str);

3. Set up Database Operation class
SqlCommand cmd = conn. CreateCommand ();

Cmd.commandtext = "Select *from student";

4. Open the Database connection channel
Conn. Open ();

5, read data, data read class
SqlDataReader dr = cmd. ExecuteReader ();

if (Dr. HasRows)//Whether the data is read
{
while (Dr. Read ())//loop down one line at a time
{
Actions to be performed for each line read
Console.WriteLine (Dr[0]. ToString () + "" + dr[1]. ToString () + "" + dr[2]. ToString () + "" + convert.todatetime (Dr[3]). ToString ("yyyy years mm DD Day") + "" + Convert.ToInt32 (dr[4));
}
}

6. Close the Database channel!!!!!
Conn. Close ();
------------------------------------------------------------------------------------------------
Add to

Let the user enter the content added "Remember to restrict content according to specific content!!!" 】
Console.Write ("Please enter student number:")
String Code = Console.ReadLine ();

SqlConnection conn = new SqlConnection ("server=.; Database=data0515;user=sa;pwd=123 ");
SqlCommand cmd = conn. CreateCommand ();

Cmd.commandtext = "INSERT into student values ('" + Code + "', '" + Name + "', '" + Sex + "', '" + Time + "'," + Class + ");";

Try
{
Conn. Open ();
Cmd. ExecuteNonQuery ();
Console.WriteLine ("added successfully! ");
}
Catch
{
Console.WriteLine ("Add failed! ");
}
Finally
{
Conn. Close ();
}

Modify
SqlConnection conn = new SqlConnection ("server=.; Database=data0515;user=sa;pwd=123 ");
SqlCommand cmd = conn. CreateCommand ();
String upcode = "";

while (true)
{
BOOL has = false;
The primary key field value to modify
Console.Write ("Please enter the value of the primary key field to be modified:");
Upcode = Console.ReadLine ();

Cmd.commandtext = "Select *from student where sno=" + upcode;
Conn. Open ();
SqlDataReader dr = cmd. ExecuteReader ();
if (Dr. HasRows)
{
has = true;
}
Conn. Close ();
if (has)
{
Break
}
Else
{
Console.WriteLine ("Do not have this (field value corresponding) information, please determine (field value) input is correct!" ");
}
}
Modified Information
Console.Write ("Please enter the modified information:");
string Field name = Console.ReadLine ();

modifying database data
Cmd. CommandText = "Update student set Sname= '" + Name + "', ssex= '" + Sex + "', sbirthday= '" + Time + "', class= '" + Class + "' W Here sno = ' "+ Upcode +" ' ";
Try
{
Conn. Open ();
Cmd. ExecuteNonQuery ();
Console.WriteLine ("Modified successfully! ");
}
Catch
{
Console.WriteLine ("Modify failed! ");
}
Finally
{
Conn. Close ();
}


Delete
SqlConnection conn = new SqlConnection ("server=.; Database=data0515;user=sa;pwd=123 ");
SqlCommand cmd = conn. CreateCommand ();
String delcode = "";
while (true)
{
BOOL has = false;
The value that corresponds to the primary key to delete
Console.Write ("Please enter the student number to delete:");
Delcode = Console.ReadLine ();

Cmd.commandtext = "Select *from student where sno=" + delcode;
Conn. Open ();
SqlDataReader dr = cmd. ExecuteReader ();
if (Dr. HasRows)
{
has = true;
}
Conn. Close ();
if (have)
{
break;
}
Else
{
Console.WriteLine ("If you do not have this student information, please make sure the number entered is correct!") ");
}
}
bool Isokok = false;
1. Ask the user if they are sure to delete the
Console.Write ("found" + Delcode + "Student's information, are you sure you want to delete it? (y/n): ");
while (true)
{
String isok = Console.ReadLine ();
if (isOK. ToUpper () = = "Y")
{
Isokok = true;
break;
}
Else if (isOK. ToUpper () = = "N")
{
Isokok = false;
break;
}
Else
{
Console.Write ("wrong input! Please re-enter: ");
}
}
//2, execute delete
if (Isokok)
{
Cmd.commandtext = "Delete from student where Sno =" + Delcode;
Conn. Open ();
Cmd. ExecuteNonQuery ();
Console.WriteLine ("Delete succeeded! ");
Conn. Close ();
}
cmd. ExecuteNonQuery ();
Specific format Please refer to the Job Code

-----------------------------------------------------------------------
Homework:
Create a data table:
Adostudent Table Name:
Code-Number: Primary key S101 S102 S103
Name-Name:
Sex-Gender: Male is true, female is false
Birthday-Birthday:
Score-Score: decimal

Requirements:
Once the program is open, all the student information is listed:
School number name Sex birthday score
S101 Zhang Sanman 1999-1-1 99.8
...

Please enter what you want to do: 1-Add, 2-Modify, 3-delete: 1

After adding success, refresh to display the latest data console.clear ();

The database connects to ADO. NET additions and deletions change

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.