Using system;using system.collections.generic;using system.data.sqlclient;using system.linq;using System.Text;using System.threading.tasks;namespace _03.ado.net{class Program {static void Main (string[] args) { To #region Connect to a database////connect to a database:////1. Create a connection string////data source= server name; Initial catalog= database name; Integrated security=true; Declaration authentication method////user name, password method//string constr = "Data source=localhost;initial Ca TALOG=ZWT;UID=ROOT;PWD=ZWT "; Integrated////string constr = "Data source=localhost;initial catalog=zwt;integrated security=true"; 2. Create Connection object//using (SqlConnection con = new SqlConnection (CONSTR))//{////test, open connection 3. Open the connection (if there is no problem opening the data connection, indicates a successful connection)//con. Open (); Console.WriteLine ("Database connection succeeded"); 4. Close the connection and release the resource//con. Close (); Con. DIspose (); }//console.writeline ("Disconnected"); Console.readkey (); #endregion #region Add data////to connect to the database:////1. Create a connection string////data source= server name; Initial catalog= database name; Integrated security=true; Declaration authentication method////user name, password method//string constr = "Data source=localhost;initial Ca TALOG=ZWT;UID=ROOT;PWD=ZWT "; Integrated////string constr = "Data source=localhost;initial catalog=zwt;integrated security=true"; 2. Create the Connection object//using (SqlConnection con = new SqlConnection (CONSTR))//{////4. Writing the SQL language sentence//String sql = "INSERT into ZWT values (' Tony ', 23, ' Male ')"; 5. Create an object that executes a SQL statement (Command object) SqlCommand//using (SqlCommand cmd = new SqlCommand (sql, con))// {////6. Start execution of SQL statements////3 Command objects////cmd. ExecuteNonQuery ();The method has a return value of type int that represents the number of rows affected after the INSERT, delete, UPDATE statement is executed, and///and returns the number of rows affected when the 3 statements are executed, and the other statements return-1// int r = cmd. ExecuteNonQuery (); CMD. ExecuteNonQuery ();//execute INSERT, DELETE, UPDATE statement////cmd. ExecuteScalar ();//execution returns a single result////cmd. ExecuteReader ();//query out multiple rows, multiple columns of results////3. Open the connection (the connection object is opened at the latest, the earliest close, save resources)//con. Open (); Console.WriteLine ("successfully inserted {0} row data", r); }//}//console.readkey (); #endregion #region Delete Data////1. Create connection string//string Constr = "Data source=localhost;initial Ca Talog=zwt;integrated Security=true "; 2. Connection object//using (SqlConnection con = new SqlConnection (CONSTR))//{////3.sql statement String sql = "Delete from zwttable where name=tony"; 4. Create SqlCommand object//using (SqlCommand cmd = new sqlcomMand (sql, con))//{////5. Open link//con. Open (); 6. Execute//int r = cmd. ExecuteNonQuery (); Console.WriteLine ("Successfully deleted {0} row data", r); }//}//console.readkey (); #endregion #region Modify the data////1. Create connection string//string Constr = "Data source=localhost;initial Ca Talog=zwt;integrated Security=true "; 2. Connection object//using (SqlConnection con = new SqlConnection (CONSTR))//{////3.sql statement String sql = "Update zwttable set name=tony,age=26"; 4. Create SqlCommand object//using (SqlCommand cmd = new SqlCommand (sql, con))//{ 5. Open the link//con. Open (); 6. Execute//int r = cmd. ExecuteNonQuery (); Console.WriteLine ("Successfully updated {0} row data", r); }//}//console.readkey (); #endregion #region Query a data////1. Create connection string//string Constr = "Data source=localhost;initial Catalog=zwt;integrated Security=true "; 2. Connection object//using (SqlConnection con = new SqlConnection (CONSTR))//{////3.sql statement String sql = "SELECT COUNT (*) from zwttable"; 4. Create SqlCommand object//using (SqlCommand cmd = new SqlCommand (sql, con))//{ 5. Open the link//con. Open (); 6. Execute////sql statement execution if it is an aggregate function, the return of ExecuteScalar () cannot be null, if it is not an aggregate function you need to determine whether it is null// Object count = (int) cmd. ExecuteScalar (); Avoid exception//Object count = Convert.ToInt32 (cmd). ExecuteScalar ()); Console.WriteLine (Total {0} data in the Zwttable table, count); // } //} Console.readkey (); #endregion #region querying multiple data////1. Create connection string//string Constr = "Data source=localhost;initial Catalog=zwt;integrated Security=true "; 2. Connection object//using (SqlConnection con = new SqlConnection (CONSTR))//{////3.sql statement String sql = "SELECT * from zwttable"; 4. Create SqlCommand object//using (SqlCommand cmd = new SqlCommand (sql, con))//{ 5. Open the link//con. Open (); By calling the ExecuteReader () method, the given SQL statement is executed on the server side////After execution, the server has already looked up the data, but the data is stored in the database server memory. It is not returned to the application,//////The application is simply returned to a reader object, which is the object used to get the data//using (SqlDataReader reader = Cmd. ExecuteReader ()////////////////////////////1. In the reader object. Before you get the data, determine if the data//if (reader. HasRows)//If there is a data of true, otherwise false//{////2. If you have data, then you need to get the data The reader is called every time the data is fetched. The read () method, which moves backward one piece of data, returns True if it is successfully moved to a data/////and returns false//while (read Er. Read ())//{////Get the current reader pointing to the data// Reader. FieldCount can get the number of columns queried by the current query statement//for (int i = 0; i < reader. FieldCount; i++)//{//Console.Write (reader[i]+ "| "); }//Console.WriteLine (); }//}//Else//{// Console.WriteLine ("No Data Found"); }//}//}//}//consOle. ReadKey (); #endregion}}}
C # Connect to database and add, delete, change, check operations