123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157 |
- using System;
- using System.Collections.Generic;
- using System.Configuration;
- using System.Data.SqlClient;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- namespace SinoChemCommonUtilities
- {
- public delegate T ConstructThing<T>(SqlDataReader rdr);
- public class SqlCommandUtility
- {
- private SqlConnection conn;
- private string _connectionString;
- public SqlCommandUtility(string connString)
- {
- conn = new SqlConnection(connString);
- _connectionString = connString;
- }
- public T ReadData<T>(string querySql, ConstructThing<T> constructThing)
- {
- T thing = default(T);
- try
- {
- using (var connection = new SqlConnection(_connectionString))
- {
- connection.Open();
- using (var command = new SqlCommand(querySql, connection))
- using (var sqlDataReader = command.ExecuteReader())
- {
- thing = constructThing(sqlDataReader);
- return thing;
- }
- }
- }
- catch (Exception ex)
- {
- Console.WriteLine(ex.ToString());
- return thing;
- }
- /*
- try
- {
- // Open the connection
- conn.Open();
- // 1. Instantiate a new command with a query and connection
- SqlCommand cmd = new SqlCommand(querySql, conn);
- // 2. Call Execute reader to get query results
- reader = cmd.ExecuteReader();
- // 3.
- thing = constructThing(reader);
- return thing;
- }
- finally
- {
- if (reader != null)
- {
- reader.Close();
- }
- // Close the connection
- if (conn != null)
- {
- conn.Close();
- }
- }
- */
- }
- public object Insertdata(string insertSql, bool toExeScalar)
- {
- object scalarObj = null;
- try
- {
- // Open the connection
- conn.Open();
- // 1. Instantiate a new command with a query and connection
- SqlCommand cmd = new SqlCommand(insertSql, conn);
- // 2. Call to send command
- if (toExeScalar)
- {
- scalarObj = cmd.ExecuteScalar();
- }
- else
- {
- cmd.ExecuteNonQuery();
- }
- return scalarObj;
- }
- finally
- {
- // Close the connection
- if (conn != null)
- {
- conn.Close();
- }
- }
- }
- public void UpdateData(string updateSql)
- {
- try
- {
- // Open the connection
- conn.Open();
- // 1. Instantiate a new command with command text only
- SqlCommand cmd = new SqlCommand(updateSql);
- // 2. Set the Connection property
- cmd.Connection = conn;
- // 3. Call ExecuteNonQuery to send command
- cmd.ExecuteNonQuery();
- }
- finally
- {
- // Close the connection
- if (conn != null)
- {
- conn.Close();
- }
- }
- }
- public void DeleteData(string deleteSql)
- {
- try
- {
- // Open the connection
- conn.Open();
- // 1. Instantiate a new command
- SqlCommand cmd = new SqlCommand();
- // 2. Set the CommandText property
- cmd.CommandText = deleteSql;
- // 3. Set the Connection property
- cmd.Connection = conn;
- // 4. Call ExecuteNonQuery to send command
- cmd.ExecuteNonQuery();
- }
- finally
- {
- // Close the connection
- if (conn != null)
- {
- conn.Close();
- }
- }
- }
- }
- }
|