C# 数据库连接类
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
///
/// 存储过程名称
/// 存储过程参数
///
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();
}
}
}

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

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

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

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

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

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

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

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

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