C#ADO.NET帮助类
using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Data;using System.Data.SqlClient;namespace DBComm{ static class DBCommand { public class DBParameters { private SqlCommand m_owner = null; publi
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Data; using System.Data.SqlClient; namespace DBComm { static class DBCommand { public class DBParameters { private SqlCommand m_owner = null; public DBParameters(SqlCommand owner) { m_owner = owner; } public SqlParameterCollection P() { return m_owner.Parameters; } }; public static bool BulkToDB(string tabname, DataTable dt, params string[] destColumnNames) { bool bRet = false; do { if (dt == null) break; if (dt.Rows.Count == 0) break; using (SqlConnection conn = DBConn.GetConn()) { if (conn == null) break; SqlBulkCopy bulkcopy = new SqlBulkCopy(conn); if (bulkcopy == null) break; bulkcopy.DestinationTableName = tabname; bulkcopy.BulkCopyTimeout = 30; if (destColumnNames.Length == 0) { foreach (DataColumn col in dt.Columns) bulkcopy.ColumnMappings.Add(col.ColumnName, col.ColumnName); } else { if (destColumnNames.Length == dt.Columns.Count) { for (int i = 0; i < destColumnNames.Length; ++i) { bulkcopy.ColumnMappings.Add(dt.Columns[i].ColumnName, destColumnNames[i]); } } } bulkcopy.BatchSize = dt.Rows.Count; try { bulkcopy.WriteToServer(dt); } catch (System.Exception e) { string err = e.Message; break; } finally { bulkcopy.Close(); } } bRet = true; } while (false); return bRet; } public static DBParameters ExecProcNonQuery(string proc_name, object[] paraValues) { using (SqlConnection conn = DBConn.GetConn()) { SqlCommand cmd = new SqlCommand(); cmd.Connection = conn; cmd.CommandType = CommandType.StoredProcedure; cmd.CommandText = proc_name; AddInParaValues(cmd, paraValues); cmd.ExecuteNonQuery(); return new DBParameters(cmd); } } public delegate T[] FillValues<T>(SqlDataReader reader); public static T[] QuerySomes<T>(string sql, FillValues<T> fill) { using (SqlConnection conn = DBConn.GetConn()) { T[] result = null; SqlCommand cmd = new SqlCommand(); cmd.Connection = conn; cmd.CommandText = sql; SqlDataReader reader = null; lock (reader = cmd.ExecuteReader()) { try { result = fill(reader); } catch (Exception e) { throw new Exception(e.StackTrace); } finally { reader.Close(); } } return result; } } public delegate object FillValue(SqlDataReader reader); public static object QuerySome(string sql, FillValue fill) { using (SqlConnection conn = DBConn.GetConn()) { object result = null; SqlCommand cmd = new SqlCommand(); cmd.Connection = conn; cmd.CommandText = sql; SqlDataReader reader = null; lock (reader = cmd.ExecuteReader()) { try { result = fill(reader); } catch (Exception e) { throw new Exception(e.StackTrace); } finally { reader.Close(); } } return result; } } public static object FillResultValue(SqlDataReader reader) { object o = null; if (reader.Read()) { o = reader.GetValue(0); } return o; } public static bool QueryBoolean(string sql) { return Convert.ToBoolean(QuerySome(sql, new FillValue(FillResultValue))); } public static byte[] QueryBytes(string sql) { return (byte[])(QuerySome(sql, new FillValue(FillResultValue))); } public static int QueryInteger(string sql) { return Convert.ToInt32(QuerySome(sql, new FillValue(FillResultValue))); } public static string QueryStr(string sql) { return QuerySome(sql, new FillValue(FillResultValue)) as string; } private static string[] FillStrsValue(SqlDataReader reader) { List<string> lststr = new List<string>(); while (reader.Read()) { lststr.Add(reader.GetString(0)); } return lststr.ToArray(); } public static string[] QueryStrs(string sql) { return QuerySomes(sql, new FillValues<string>(FillStrsValue)); } private static bool[] FillBooleansValue(SqlDataReader reader) { List<bool> lstbool = new List<bool>(); while (reader.Read()) { lstbool.Add(reader.GetBoolean(0)); } return lstbool.ToArray(); } public static bool[] QueryBooleans(string sql) { return QuerySomes(sql, new FillValues<bool>(FillBooleansValue)); } public static void ExecCmd(string sql) { using (SqlConnection conn = DBConn.GetConn()) { SqlCommand cmd = new SqlCommand(); cmd.Connection = conn; cmd.CommandText = sql; cmd.ExecuteNonQuery(); } } /// <summary> /// 获取存储过程的参数列表 /// </summary> /// <param name="proc_Name">存储过程名称</param> /// <returns>DataTable</returns> private static DataTable GetParameters(SqlConnection conn, string proc_Name) { SqlCommand comm = new SqlCommand("dbo.sp_sproc_columns", conn); comm.CommandType = CommandType.StoredProcedure; comm.Parameters.AddWithValue("@procedure_name", (object)proc_Name); SqlDataAdapter sda = new SqlDataAdapter(comm); DataTable dt = new DataTable(); sda.Fill(dt); return dt; } /// <summary> /// 为 SqlCommand 添加参数及赋值 /// </summary> /// <param name="comm">SqlCommand</param> /// <param name="paraValues">参数数组(必须遵循存储过程参数列表的顺序)</param> private static void AddInParaValues(SqlCommand comm, params object[] paraValues) { using (SqlConnection conn = DBConn.GetConn()) { comm.Parameters.Add(new SqlParameter("@RETURN_VALUE", SqlDbType.Int)); comm.Parameters["@RETURN_VALUE"].Direction = ParameterDirection.ReturnValue; if (paraValues != null) { DataTable dt = GetParameters(conn, comm.CommandText); int i = 0; foreach (DataRow row in dt.Rows) { string key = row[3].ToString(); if (key != "@RETURN_VALUE") { int value = int.Parse(row[4].ToString()); if (value == 1) { comm.Parameters.AddWithValue(key, paraValues[i]); } else if (value == 2)//value为2则是输出参数 { comm.Parameters.AddWithValue(key, paraValues[i]).Direction = ParameterDirection.Output; //comm.Parameters[key].Direction = ParameterDirection.Output; } comm.Parameters[key].Size = Convert.ToInt32(row[7].ToString()); i++; } } } } } } }
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Data.SqlClient; namespace DBComm { class DBConn { private static string m_connstr; public static string ConnString { get { return m_connstr; } private set { m_connstr = value; } } static DBConn() { SqlConnectionStringBuilder connStr = new SqlConnectionStringBuilder(); connStr.DataSource = "."; connStr.InitialCatalog = "test"; connStr.IntegratedSecurity = true; connStr.Pooling = true; //开启连接池 connStr.MinPoolSize = 0; //设置最小连接数为0 connStr.MaxPoolSize = 100; //设置最大连接数为100 connStr.ConnectTimeout = 10; //设置超时时间为10秒 ConnString = connStr.ConnectionString; //ConnectDB(ConnString); } public static SqlConnection GetConn() { SqlConnection conn = new SqlConnection(ConnString); conn.Open(); return conn; } } }
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Data.SqlClient; using System.Data; namespace DBComm { static class DBTableSource { public static DataTable GetSource(SqlConnection conn, string strsql) { DataTable dt = null; SqlCommand cmd = null; SqlDataAdapter ad = null; try { lock (dt = new DataTable()) { if (conn is SqlConnection) { cmd = new SqlCommand(strsql, conn); ad = new SqlDataAdapter((SqlCommand)cmd); } dt.Clear(); ad.Fill(dt); } } catch (Exception e) { throw e; } return dt; } public static DataTable Source(string strsql) { using (SqlConnection conn = DBConn.GetConn()) { return GetSource(conn, strsql); } } } }

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



Java document interpretation: Usage analysis of the currentTimeMillis() method of the System class, specific code examples are required. In Java programming, the System class is a very important class, which encapsulates some properties and operations related to the system. Among them, the currentTimeMillis method is a very commonly used method in the System class. This article will explain the method in detail and provide code examples. 1. Overview of currentTimeMillis method

The win10 system is a very easy-to-use and highly intelligent system. Its strong compatibility can ensure that the system will basically not have any problems during normal use. However, as people continue to use the win10 system, sometimes the system will also have problems. The problem of Win10 booting blue screen termination code SystemServiceException. Today I will bring you the solution to Win10 booting Blue Screen termination code SystemServiceException. If you need it, please download it quickly. Solution to win10systemserviceexception blue screen: Method 1: 1. Use Windows key + R to open Run and enter “contr

The computer system is a relatively common system process. You will often see system when viewing the process. This process simply means the computer system; however, if the system.exe process appears on the computer, it needs to be deleted in time. , this is a file generated by a Trojan horse virus. There is no exe suffix behind the real system.

I believe that netizens are very familiar with the Windows 7 system. Have you heard of the Windows 7 English version system? I believe that many netizens have heard about the Windows 7 English version system. However, some friends are looking for the Windows 7 English version system to download. Today I will The editor is going to share the introduction of the original version of win7 in English with everyone, so that netizens can understand the original version of win7 in English. The following is to tell you where to download the English version of Windows 7 system. The original English system of win7 has been released to MSDN for subscription download. The official English integrated version was first released, Windows7WithSP1, which is the Windows7 CD image with integrated SP1. Includes SP1 standalone for multiple languages

Microsoft has announced the availability of System Center 2022. The latest version brings System Center Operations Manager (SCOM), Virtual Machine Manager (VMM), System Center Orchestrator (SCORCH), Service Manager (SM) and Data Protection Manager

Object is the base class of all Java classes, the top of the entire class inheritance structure, and the most abstract class. Everyone uses toString(), equals(), hashCode(), wait(), notify(), getClass() and other methods every day. Maybe they don’t realize that they are methods of Object, and they don’t look at what other methods Object has. And think about why these methods should be placed in Object. 1. Introduction to JavaObject class - the super class of all classes Object is a special class in the Java class library and is also the parent class of all classes. In other words, Java allows any type of object to be assigned to the Object type

Step back in time to the Macintosh of the 1990s and run complete virtual installations of System 7 and MacOS 8 in a browser window. One flaw with new virtual versions of 1990s Mac software is that they run at the speed of a 2020s Mac. You're looking at a MacSE/30 or Quadra700, but everything is as fast as Apple Silicon. You can actually work in these simulated operating systems, and they can even drag documents or files in and out of macOS Monterey. But whether for some practical purposes or more likely just for pure fun, here's how

Example In normal times, when we do related table queries, it is usually like this: select*from table 1 innerjoin table 2 on table 1. Same column = table 2. Same column; then it can be changed to this, which has the same effect. Select the columns of table 1 from table 1 innerjoin table 2 on table 1. same column = table 2. same column. Then it can also be changed to select*from table 1 innerjoin table 2 using (same column); the first SELECT*FROMtype, articlewheretype.id=article. type_id;Second SELECT*FROMtypeinnerjoi
