ADO.NET實現(xiàn)對SQL Server數(shù)據(jù)庫的增刪改查示例
了解了上一篇的ADO.NET簡介,我們就可以來對數(shù)據(jù)庫進行增刪改查等基本操作了!下面是每種操作的具體實現(xiàn)。
先在自定義類的頭部定義好數(shù)據(jù)庫連接對象和連接字符串:
string connectionString = "Data Source=SC-201607131829;Initial Catalog=Animal;Integrated Security=True"; SqlConnection conn;
1.數(shù)據(jù)庫的查詢操作,返回一個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); //填充DataSet return ds.Tables[0]; } }
2.數(shù)據(jù)庫插入操作,返回布爾值
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.數(shù)據(jù)庫刪除操作,返回布爾值
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.數(shù)據(jù)庫更新操作,返回布爾值
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; } } }
其中為了防止sql注入,用到了SqlParameter類。
以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關文章
gridview實現(xiàn)服務器端和客戶端全選的兩種方法分享
這篇文章主要介紹了gridview實現(xiàn)服務器端和客戶端全選的兩種方法,需要的朋友可以參考下2014-02-02asp.net HttpWebRequest自動識別網(wǎng)頁編碼
HttpWebRequest獲取網(wǎng)頁源代碼時自動識別網(wǎng)頁編碼,通過讀取頁面中的charset和讀取http頭中的編碼信息獲取頁面的編碼,基本可以正確獲取網(wǎng)頁編碼2008-09-09在Code First模式中自動創(chuàng)建Entity模型
這篇文章介紹了在Code First模式中自動創(chuàng)建Entity模型的方法,文中通過示例代碼介紹的非常詳細。對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2022-06-06MVC使用Controller代替Filter完成登錄驗證(Session校驗)學習筆記5
這篇文章主要介紹了MVC使用Controller代替Filter完成登錄驗證即Session校驗,具有一定的參考價值,感興趣的小伙伴們可以參考一下2016-09-09asp.net uploadify實現(xiàn)多附件上傳功能
這篇文章主要為大家詳細介紹了asp.net uploadify實現(xiàn)多附件上傳功能,具有一定的參考價值,感興趣的小伙伴們可以參考一下2016-11-11