SqlHelper数据库访问类
生活随笔
收集整理的這篇文章主要介紹了
SqlHelper数据库访问类
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
參考上一篇:數(shù)據(jù)庫連接配置app.config
寫一個(gè)通用的數(shù)據(jù)庫訪問類。?
public class SqlHelper //數(shù)據(jù)庫連接類 {//獲取數(shù)據(jù)庫連接字符串,其屬于靜態(tài)變量且只讀,項(xiàng)目中所有文檔可以直接使用,但不能修改public static readonly string connectString = ConfigurationManager.ConnectionStrings["CoonStr"].ConnectionString;//負(fù)責(zé)查詢 ,二個(gè)參數(shù)(sql查詢語句,參數(shù))public static DataTable ExecuteDataTable(string sql, params SqlParameter[] parameters){DataTable dt = new DataTable();using (SqlConnection con = new SqlConnection(connectString)){SqlDataAdapter adapter = new SqlDataAdapter(sql, con);adapter.SelectCommand.Parameters.AddRange(parameters);adapter.Fill(dt);}return dt;}之后調(diào)用的時(shí)候:
//查找 歌名、歌手名、路徑string sql = "select sname,sername,spath from songs,singers where songs.Serid=Singers.Serid and Singers.Serid=@id";DataTable dt=SqlHelper .ExecuteDataTable (sql ,new SqlParameter("@id",singer.serid )); //拼連接字符串string sql = "select * from Users where Uname=@name";DataTable dt = SqlHelper.ExecuteDataTable(sql, new SqlParameter("@name", strName));增刪改
//負(fù)責(zé)增刪改public static int ExecuteNonQuery(string sql, params SqlParameter[] parameters){using (SqlConnection con = new SqlConnection(connectString)){using (SqlCommand cmd = new SqlCommand(sql, con)){con.Open();cmd.Parameters.AddRange(parameters);return cmd.ExecuteNonQuery();}}}//拼接查詢語句string sql = "Insert into Users(Uname,Upwd,Ubrith) values (" +"'"+Uname+"','"+Upwd+"','"+Ubrith+"')" ;DAL.SqlHelper.ExecuteNonQuery(sql); MessageBox.Show("增加會(huì)員成功");?
?
轉(zhuǎn)載于:https://www.cnblogs.com/peterYong/p/6556713.html
總結(jié)
以上是生活随笔為你收集整理的SqlHelper数据库访问类的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: qsort()与sort的用法(收藏)
- 下一篇: 回档|N皇后