In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-09-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
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!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
The market share of Chrome browser on the desktop has exceeded 70%, and users are complaining about
The world's first 2nm mobile chip: Samsung Exynos 2600 is ready for mass production.According to a r
A US federal judge has ruled that Google can keep its Chrome browser, but it will be prohibited from
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
About us Contact us Product review car news thenatureplanet
More Form oMedia: AutoTimes. Bestcoffee. SL News. Jarebook. Coffee Hunters. Sundaily. Modezone. NNB. Coffee. Game News. FrontStreet. GGAMEN
© 2024 shulou.com SLNews company. All rights reserved.