DBHelper 博客分类: C# SQL
程序员文章站
2024-02-14 19:15:34
...
using System; using System.Collections.Generic; using System.Text; using System.Data; using System.Data.SqlClient; //数据库访问工具类 namespace userWeb.Util { public static class DBHelper { // private static SqlConnection connection; public static string connectionString = "Data Source=localhost;Initial Catalog=user_db;User ID=sa;password=abc"; //public static SqlConnection Connection //{ // get // { // string connectionString = "Data Source=localhost;Initial Catalog=user_db;User ID=sa;password=abc"; // return new SqlConnection(connectionString); // //if (connection == null) // //{ // // connection = new SqlConnection(connectionString); // // connection.Open(); // //} // //else if (connection.State == System.Data.ConnectionState.Closed) // //{ // // connection.Open(); // //} // //else if (connection.State == System.Data.ConnectionState.Broken) // //{ // // connection.Close(); // // connection.Open(); // //} // //return connection; // } //} public static SqlConnection getCon() { SqlConnection con = new SqlConnection(connectionString); con.Open(); return con; } public static int ExecuteCommand(string safeSql) { SqlCommand cmd = new SqlCommand(safeSql, getCon()); int result = cmd.ExecuteNonQuery(); return result; } public static int ExecuteCommand(string sql, params SqlParameter[] values) { SqlCommand cmd = new SqlCommand(sql, getCon()); cmd.Parameters.AddRange(values); return cmd.ExecuteNonQuery(); } public static int GetScalar(string safeSql) { SqlCommand cmd = new SqlCommand(safeSql, getCon()); int result = Convert.ToInt32(cmd.ExecuteScalar()); return result; } public static int GetScalar(string sql, params SqlParameter[] values) { SqlCommand cmd = new SqlCommand(sql, getCon()); cmd.Parameters.AddRange(values); int result = Convert.ToInt32(cmd.ExecuteScalar()); return result; } public static SqlDataReader GetReader(string safeSql) { SqlCommand cmd = new SqlCommand(safeSql, getCon()); SqlDataReader reader = cmd.ExecuteReader(); return reader; } public static SqlDataReader GetReader(string sql, params SqlParameter[] values) { SqlCommand cmd = new SqlCommand(sql, getCon()); cmd.Parameters.AddRange(values); SqlDataReader reader = cmd.ExecuteReader(); return reader; } public static DataTable GetDataSet(string safeSql) { DataSet ds = new DataSet(); SqlCommand cmd = new SqlCommand(safeSql, getCon()); SqlDataAdapter da = new SqlDataAdapter(cmd); da.Fill(ds); return ds.Tables[0]; } public static DataTable GetDataSet(string sql, params SqlParameter[] values) { DataSet ds = new DataSet(); SqlCommand cmd = new SqlCommand(sql, getCon()); cmd.Parameters.AddRange(values); SqlDataAdapter da = new SqlDataAdapter(cmd); da.Fill(ds); return ds.Tables[0]; } } }
推荐阅读
-
Oracle 博客分类: Oracle Oracle数据结构SQL配置管理应用服务器
-
DBHelper 博客分类: C# SQL
-
Oracle PL/SQL 中循环语法语句的应用 博客分类: 数据库 OraclePL/SQL循环
-
基于Mybatis封装的增删改查实现通用自动化sql 博客分类: JavaEE dao
-
jdbc连接sql server数据库的一些异常 博客分类: Web基础 jspsqlserver
-
SQLITE使用 博客分类: SQL sqlite
-
sqlserver查看数据库编码 博客分类: SQL Server SQL Server数据库编码
-
sqlserver查看数据库编码 博客分类: SQL Server SQL Server数据库编码
-
MySQL 增删改查 博客分类: 境-MySql SQL增删改查MYSQL增删改查增删改查
-
C# 事件 EventHanlder 博客分类: 编程技术 CC++C#