資源簡介
SQL Server數(shù)據(jù)庫操作類是C#語言的,可實(shí)現(xiàn)對SQL Server數(shù)據(jù)庫的增刪改查詢等操作,并且該操作類可實(shí)現(xiàn)對圖片的存儲(chǔ)
代碼片段和文件信息
using?System;
using?System.Data;
using?System.Configuration;
using?System.Data.SqlClient;
namespace?SQLServerDatabase
{
????public?class?DatabaseOp
????{
????????private?string?SqlConnectionString;????//數(shù)據(jù)庫連接
????????///?
????????///?構(gòu)造函數(shù)
????????///?初始化連接數(shù)據(jù)庫參數(shù)
????????///?
????????public?DatabaseOp()
????????{
????????????SqlConnectionString?=?“Data?Source=.;Initial?Catalog=DatabaseName;User?ID=sa;pwd=123456;Connection?Lifetime=0;max?pool?size=200“;
????????}
????????///?
????????///?構(gòu)造函數(shù)
????????///?初始化連接數(shù)據(jù)庫參數(shù)
????????///?
????????///?連接對象
????????public?DatabaseOp(string?ConSqlServer)
????????{
????????????SqlConnectionString?=?ConSqlServer;
????????}
????????///?
????????///?打開數(shù)據(jù)庫連接
????????///?
????????///?連接
????????public?void?Open(SqlConnection?cn)
????????{
????????????if?(cn.State?==?ConnectionState.Closed)
????????????{
????????????????cn.Open();
????????????}
????????}
????????///?
????????///?關(guān)閉數(shù)據(jù)庫連接
????????///?
????????///?連接
????????public?void?Close(SqlConnection?cn)
????????{
????????????if?(cn?!=?null)
????????????{
????????????????if?(cn.State?==?ConnectionState.Open)
????????????????{
????????????????????cn.Close();
????????????????}
????????????????cn.Dispose();
????????????}
????????}
????????///?
????????///?查詢
????????///?
????????///?SQL語句
????????///?是否存在
????????public?bool?ChaXun(string?strSql)
????????{
????????????SqlConnection?cn?=?new?SqlConnection(SqlConnectionString);
????????????SqlCommand?cmd?=?new?SqlCommand();
????????????try
????????????{
????????????????Open(cn);
????????????????cmd?=?new?SqlCommand(strSql?cn);
????????????????return?cmd.ExecuteReader().Read();
????????????}
????????????catch?(Exception?e)
????????????{
????????????????throw?new?Exception(e.Message);
????????????}
????????????finally
????????????{
????????????????cmd.Dispose();
????????????????Close(cn);
????????????}
????????}
????????///?
????????///?查詢
????????///?
????????///?SQL語句
????????///?第一行第一列結(jié)果
????????public?string?ChaXun2(string?strSql)
????????{
????????????SqlConnection?cn?=?new?SqlConnection(SqlConnectionString);
????????????SqlCommand?cmd?=?new?SqlCommand();
????????????try
????????????{
????????????????Open(cn);
????????????????cmd?=?new?SqlCommand(strSql?cn);
????????????????return?cmd.ExecuteScalar().ToString().Trim();
????????????}
????????????catch?(Exception?e)
????????????{
????????????????throw?new?Exception(e.Message);
????????????}
????????????finally
????????????{
????????????????cmd.Dispose();
????????????????Close(cn);
????????????}
????????}
????????///?
????????///?查詢(SqlDataReader)
??????
評論
共有 條評論