Home php教程 php手册 C# 数据库连接类

C# 数据库连接类

Jun 06, 2016 pm 08:01 PM
php system using database connect

using System; using System.Collections.Generic; using System.Text; using System.Data; using System.Data.SqlClient; using System.Configuration; namespace XXD.HZD.DBCONN { public class Dbconn { private SqlConnection scn = null; private strin

using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;

 

namespace XXD.HZD.DBCONN
{
    public class Dbconn
    {

 private SqlConnection scn = null;
 
 private string connectionstring = "";

 public Dbconn()
 {
     connectionstring = ConfigurationManager.AppSettings["connectionstring"].ToString(); //从web.config中读取数据库连接字符串
     scn = new SqlConnection();
     scn.ConnectionString = connectionstring;
 }
 
 ///


 /// 数据库打开
 ///

 private void DbOpen()
 {
     if ( scn == null)
     {
  scn = new SqlConnection();
  scn.ConnectionString = connectionstring;
     }
     if ( scn.State == ConnectionState.Closed)
  {
      scn.Open();
  }
  else if (scn.State == ConnectionState.Broken)
  {
      scn.Close();
      scn.Open();
  }
 }

 ///


 /// 数据库释放
 ///

 public void DbClose()
 {
     if ( scn != null)
     {
  scn.Close();
  scn.Dispose();
  scn = null;
     }
 }

 ///


 /// 返回DataSet
 ///

 /// SQL语句
 /// 数据集
 public DataSet getDataSet(string strselect)
 {
     DataSet ds = new DataSet();
     SqlDataAdapter sda = new SqlDataAdapter();
     DbOpen();
     using ( sda = new SqlDataAdapter())
  {
      sda.SelectCommand = new SqlCommand();
      sda.SelectCommand.Connection = scn;
      sda.SelectCommand.CommandType = CommandType.Text;
      sda.SelectCommand.CommandText = strselect;
      sda.SelectCommand.ExecuteNonQuery();
      sda.Fill(ds);
  }
     DbClose();
     return ds;
 }

 ///


 /// 存储过程返回记录集
 ///

 /// 存储过程名称
 /// 参数
 /// 符合条件的记录集
 public DataSet getDataSet(string procName, SqlParameter[] sqlparameter)
 {
     DataSet ds = new DataSet();
     DbOpen();
     SqlCommand cm = getCommand(procName, sqlparameter);
     SqlDataAdapter sda = new SqlDataAdapter(cm);
     sda.Fill(ds);
     DbClose();
     return ds;
 }

 public void getDataSet(string procName, SqlParameter[] sqlparameter, out DataSet ds)
 {
     ds = new DataSet();
     DbOpen();
     SqlCommand cm = getCommand(procName, sqlparameter);
     SqlDataAdapter sda = new SqlDataAdapter(cm);
     sda.Fill(ds);
     DbClose();
 }


 ///


 /// 删除一条信息
 ///

 /// sql语句
 /// 删除条数
 public int delItem(string strdelete)
 {
     int i = 0 ;
     DbOpen();
     using ( SqlCommand cm = new SqlCommand())
     {
     cm.Connection = scn;
     cm.CommandType = CommandType.Text;
     cm.CommandText = strdelete;
     try
     {
  i = cm.ExecuteNonQuery();
     }
     catch (SqlException ex)
     {  
  throw;
     }
     } 
     return i;   
 }

 ///


 /// 取得第一行第一个单元格的值
 ///

 /// SQL语句
 /// 返回记录
 public string getScalar(string strselect)
 {
     DbOpen();
     SqlCommand cm = new SqlCommand();
     cm.Connection = scn;
     cm.CommandType = CommandType.Text;
     cm.CommandText = strselect;
     string gradename = cm.ExecuteScalar().ToString();
     DbClose();
     return gradename;
 }

 ///


 /// 调用存储过程
 ///

 /// 存储过程名
 /// 存储过程参数
 public int executrProc(string procedureName, SqlParameter[] sqlparameter)
 {
     SqlCommand  cm = null;
     int returnvalue = 0;
     DbOpen();
     using (cm = new SqlCommand())
     {
  cm.Connection = scn;
  cm.CommandType = CommandType.StoredProcedure;
  cm.CommandText = procedureName;

  foreach (SqlParameter sqlpara in sqlparameter)
  {
      cm.Parameters.Add(sqlpara);
  }

  cm.ExecuteNonQuery();
  returnvalue = Convert.ToInt32(cm.Parameters[8].Value);
     }
     DbClose();
     return returnvalue;
 }

 ///


 /// 存储过程有返回值
 ///

 /// 存储过程名称
 /// 存储过程参数
 /// 存储过程返回值
 public string returnProc(string procName, SqlParameter[] sqlparameter)
 {
     DbOpen();
     SqlCommand cm = getCommand(procName, sqlparameter);
     cm.ExecuteNonQuery();
     DbClose();
     return cm.Parameters[sqlparameter.Length-1].Value.ToString();
 }

 ///


 /// 返回一个command
 ///

 /// 存储过程名称
 /// 存储过程参数
 /// command对象
 private SqlCommand getCommand ( string procName, SqlParameter[] sqlparameter)
 {
         SqlCommand cm = new SqlCommand();
     cm.Connection = scn;
     cm.CommandType = CommandType.StoredProcedure;
     cm.CommandText = procName;
     foreach(SqlParameter sp in sqlparameter)
     {
  cm.Parameters.Add(sp);
     }
     return cm;
 }

 public SqlDataReader getReader(string procName, SqlParameter[] sqlparameter)
 {
     DbOpen();
     SqlCommand cm = getCommand(procName, sqlparameter);
     return cm.ExecuteReader();
 }

 ///


 /// 执行删除,更新语句
 ///

 /// 存储过程名称
 /// 存储过程参数
 public void execuCommand( string procName, SqlParameter[] sqlparameter)
 {
     DbOpen();
     SqlCommand cm = getCommand(procName, sqlparameter);
     cm.ExecuteNonQuery();
     DbClose();    
 }
    }
}

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

CakePHP Project Configuration CakePHP Project Configuration Sep 10, 2024 pm 05:25 PM

In this chapter, we will understand the Environment Variables, General Configuration, Database Configuration and Email Configuration in CakePHP.

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian Dec 24, 2024 pm 04:42 PM

PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

CakePHP Date and Time CakePHP Date and Time Sep 10, 2024 pm 05:27 PM

To work with date and time in cakephp4, we are going to make use of the available FrozenTime class.

CakePHP File upload CakePHP File upload Sep 10, 2024 pm 05:27 PM

To work on file upload we are going to use the form helper. Here, is an example for file upload.

CakePHP Routing CakePHP Routing Sep 10, 2024 pm 05:25 PM

In this chapter, we are going to learn the following topics related to routing ?

Discuss CakePHP Discuss CakePHP Sep 10, 2024 pm 05:28 PM

CakePHP is an open-source framework for PHP. It is intended to make developing, deploying and maintaining applications much easier. CakePHP is based on a MVC-like architecture that is both powerful and easy to grasp. Models, Views, and Controllers gu

How To Set Up Visual Studio Code (VS Code) for PHP Development How To Set Up Visual Studio Code (VS Code) for PHP Development Dec 20, 2024 am 11:31 AM

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

CakePHP Creating Validators CakePHP Creating Validators Sep 10, 2024 pm 05:26 PM

Validator can be created by adding the following two lines in the controller.

See all articles