Example Analysis of adding, deleting, modifying and querying SQL Server Database by ADO.NET
The purpose of this article is to share with you the content of the sample analysis of ADO.NET 's operation of adding, deleting, modifying and searching the SQL Server database. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.
First, define the database connection object and connection string in the header of the custom class:
String connectionString = "Data Source=SC-201607131829;Initial Catalog=Animal;Integrated Security=True"; SqlConnection conn
1. Query operation of the database, which returns a DataTable
Public DataTable doSelect () {string sql = "select * from detial"; using (conn = new SqlConnection (connectionString)) {conn.Open (); SqlDataAdapter da = new SqlDataAdapter (sql, conn); DataSet ds = new DataSet (); da.Fill (ds); / / populate DataSet return ds.Tables [0];}}
two。 Database insert operation, returning a Boolean value
Public bool doInsert (string name, string skin, string weight) {string sql = "insert into detial (name,skin,weight) values (@ name,@skin,@weight)"; SqlParameter [] newAnimal = {new SqlParameter ("name", name), new SqlParameter ("skin", skin), new SqlParameter ("weight", skin)} Using (conn = new SqlConnection (connectionString)) {SqlCommand com = new SqlCommand (sql, conn); try {if (newAnimal! = null) {foreach (SqlParameter parameter in newAnimal) {com.Parameters.Add (parameter);}} conn.Open () Int influence = com.ExecuteNonQuery (); if (influence > 0) {return true;} else {return false;}} catch (Exception exception) {return false;}
3. Database delete operation, returning a Boolean value
Public bool doDelete (string name) {string sql = "delete from detial where name = @ name"; SqlParameter [] deleteParameter = {new SqlParameter ("name", name)}; using (conn = new SqlConnection (connectionString)) {SqlCommand com = new SqlCommand (sql, conn) Try {if (deleteParameter! = null) {foreach (SqlParameter parameter in deleteParameter) {com.Parameters.Add (parameter);}} conn.Open (); int influence = com.ExecuteNonQuery () If (influence > 0) {return true;} else {return false;}} catch (Exception exception) {return false;}
4. Database update operation, returning a Boolean value
Public bool doUpdate (string name, string skin) {string sql = "update detial set skin = @ skin where name = @ name"; SqlParameter [] updateParameter = {new SqlParameter ("name", name), new SqlParameter ("skin", skin)}; using (conn = new SqlConnection (connectionString)) {SqlCommand com = new SqlCommand (sql,conn) Try {if (updateParameter! = null) {foreach (SqlParameter parameter in updateParameter) {com.Parameters.Add (parameter);}} conn.Open (); int influence = com.ExecuteNonQuery () If (influence > 0) {return true;} else {return false;}} catch (Exception exception) {return false;}
To prevent sql injection, the SqlParameter class is used.
Thank you for reading! On the "ADO.NET on the SQL Server database to add, delete and change the operation of the example analysis" this article is shared here, I hope the above content can be of some help to you, so that you can learn more knowledge, if you think the article is good, you can share it out for more people to see it!