namespace TagTools
{
using System;
using System.Data;
using System.Data.SqlClient;
public class SqlHelper
{
public static string DBString = string.Empty;
public static int ExecuteNonQuery(string sql, CommandType commandType, params SqlParameter[] sqlp)
{
using (SqlConnection connection = new SqlConnection(DBString))
{
SqlCommand command = new SqlCommand(sql, connection) {
CommandType = commandType
};
if ((sqlp != null) && (sqlp.Length > 0))
{
command.Parameters.AddRange(sqlp);
}
connection.Open();
int num = command.ExecuteNonQuery();
if (connection.State == ConnectionState.Open)
{
connection.Close();
connection.Dispose();
}
return num;
}
}
public static SqlDataReader ExecuteReader(SqlConnection sqlConnection, string sql, CommandType commandType, params SqlParameter[] sqlp)
{
if (sqlConnection == null)
{
throw new ArgumentNullException("sqlConnection");
}
sqlConnection = new SqlConnection(DBString);
SqlCommand command = new SqlCommand(sql, sqlConnection) {
CommandType = commandType
};
if ((sqlp != null) && (sqlp.Length > 0))
{
command.Parameters.AddRange(sqlp);
}
sqlConnection.Open();
return command.ExecuteReader(CommandBehavior.CloseConnection);
}
public static object ExecuteScalar(string sql, CommandType commandType, params SqlParameter[] sqlp)
{
using (SqlConnection connection = new SqlConnection(DBString))
{
object obj2;
SqlCommand command = new SqlCommand(sql, connection) {
CommandType = commandType
};
if ((sqlp != null) && (sqlp.Length > 0))
{
command.Parameters.AddRange(sqlp);
}
try
{
connection.Open();
obj2 = command.ExecuteScalar();
}
catch (Exception)
{
obj2 = 0;
}
finally
{
if (connection.State == ConnectionState.Open)
{
connection.Close();
connection.Dispose();
}
}
return obj2;
}
}
public static DataTable GetDataTable(string sql, CommandType commandType, params SqlParameter[] sqlp)
{
using (SqlConnection connection = new SqlConnection(DBString))
{
DataTable dataTable = new DataTable();
SqlCommand selectCommand = new SqlCommand(sql, connection) {
CommandType = commandType
};
if ((sqlp != null) && (sqlp.Length > 0))
{
selectCommand.Parameters.AddRange(sqlp);
}
connection.Open();
new SqlDataAdapter(selectCommand).Fill(dataTable);
if (connection.State == ConnectionState.Open)
{
connection.Close();
connection.Dispose();
}
return dataTable;
}
}
}
}
2019-03-25
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。
推荐阅读更多精彩内容
- 本周是第10周。 1.本周回顾 本周主要是在作挑战杯的准备工作。前几天在做论文的重新,后几天在做视频、文档、附加材...
- 上海经济发达,高校云集,来源于历史和国家布局。 那么为什么上海的互联网巨头这个时代的弄潮儿,远没有深圳、北京发达呢...