From 2874bdc7765129acba61714bc4e83bdc1a87618a Mon Sep 17 00:00:00 2001 From: Xusheng Date: Sun, 13 Nov 2016 00:43:11 +0800 Subject: [PATCH] formating code --- .../Properties/AssemblyInfo.cs | 1 - Xus.SQLServerHelper/SQLServerHelper.cs | 590 +++++++++--------- .../Xus.SQLServerHelper.csproj | 2 + test/Program.cs | 4 - test/Properties/AssemblyInfo.cs | 1 - 5 files changed, 297 insertions(+), 301 deletions(-) mode change 100755 => 100644 Xus.SQLServerHelper/Xus.SQLServerHelper.csproj diff --git a/Xus.SQLServerHelper/Properties/AssemblyInfo.cs b/Xus.SQLServerHelper/Properties/AssemblyInfo.cs index c95c907..2cf1a2c 100755 --- a/Xus.SQLServerHelper/Properties/AssemblyInfo.cs +++ b/Xus.SQLServerHelper/Properties/AssemblyInfo.cs @@ -1,5 +1,4 @@ using System.Reflection; -using System.Runtime.CompilerServices; using System.Runtime.InteropServices; // 有关程序集的常规信息通过以下 diff --git a/Xus.SQLServerHelper/SQLServerHelper.cs b/Xus.SQLServerHelper/SQLServerHelper.cs index e882d2e..66f0837 100755 --- a/Xus.SQLServerHelper/SQLServerHelper.cs +++ b/Xus.SQLServerHelper/SQLServerHelper.cs @@ -9,320 +9,320 @@ namespace Xus.SQLServerHelper /// /// SQL 连接助手类 /// - public class SqlHelper - { + public class SqlHelper + { /// /// Sql连接对象 /// /// Sql连接对象 private SqlConnection SqlCnt { get; set; } //Sql连接对象 - /// - /// 构造函数 - /// (使用用户名、密码验证) - /// - /// 数据源 - /// 数据库 - /// 用户名 - /// 密码 - /// 连接超时(秒),默认5秒 - public SqlHelper(string dataSource, string dataBase, string user, string pwd, int timeout = 5) - { - string connectionString = "Data Source=" + dataSource + ";Initial Catalog=" + dataBase + ";User ID=" + user + ";Password=" + pwd + ";Connection Timeout=" + timeout + ";"; - SqlCnt = new SqlConnection(connectionString); - } + /// + /// 构造函数 + /// (使用用户名、密码验证) + /// + /// 数据源 + /// 数据库 + /// 用户名 + /// 密码 + /// 连接超时(秒),默认5秒 + public SqlHelper(string dataSource, string dataBase, string user, string pwd, int timeout = 5) + { + string connectionString = "Data Source=" + dataSource + ";Initial Catalog=" + dataBase + ";User ID=" + user + ";Password=" + pwd + ";Connection Timeout=" + timeout + ";"; + SqlCnt = new SqlConnection(connectionString); + } - /// - /// 构造函数 - /// (使用Windows身份验证) - /// - /// 数据源 - /// 数据库 - /// 连接超时(秒),默认5秒 - public SqlHelper(string dataSource, string dataBase, int timeout = 5) - { - string connectionString = "Data Source=" + dataSource + ";Initial Catalog=" + dataBase + ";Integrated Security=True;Connection Timeout=" + timeout + ";"; - SqlCnt = new SqlConnection(connectionString); - } + /// + /// 构造函数 + /// (使用Windows身份验证) + /// + /// 数据源 + /// 数据库 + /// 连接超时(秒),默认5秒 + public SqlHelper(string dataSource, string dataBase, int timeout = 5) + { + string connectionString = "Data Source=" + dataSource + ";Initial Catalog=" + dataBase + ";Integrated Security=True;Connection Timeout=" + timeout + ";"; + SqlCnt = new SqlConnection(connectionString); + } - /// - /// 构造函数 - /// (传入连接字符串) - /// - /// - public SqlHelper(string connectionString) - { - SqlCnt = new SqlConnection(connectionString); - } + /// + /// 构造函数 + /// (传入连接字符串) + /// + /// + public SqlHelper(string connectionString) + { + SqlCnt = new SqlConnection(connectionString); + } - /// - /// 打开连接 - /// - private void OpenConnection() - { - if (SqlCnt.State == ConnectionState.Closed) //连接关闭 - { - try - { - SqlCnt.Open(); - } + /// + /// 打开连接 + /// + private void OpenConnection() + { + if (SqlCnt.State == ConnectionState.Closed) //连接关闭 + { + try + { + SqlCnt.Open(); + } + catch (Exception e) + { + throw new Exception("服务器连接失败:" + e); + } + } + else if (SqlCnt.State == ConnectionState.Broken) //连接中断 + { + try + { + CloseConnection(); + SqlCnt.Open(); + } catch (Exception e) - { - throw new Exception("服务器连接失败:" + e); - } - } - else if (SqlCnt.State == ConnectionState.Broken) //连接中断 - { - try - { - CloseConnection(); - SqlCnt.Open(); - } - catch (Exception e) - { - throw new Exception("服务器连接失败:" + e); - } - } - } + { + throw new Exception("服务器连接失败:" + e); + } + } + } - /// - /// 关闭连接 - /// - public void CloseConnection() - { - try - { - SqlCnt.Close(); - } - catch (Exception e) - { - throw new Exception("关闭数据库连接失败:" + e); - } - } + /// + /// 关闭连接 + /// + public void CloseConnection() + { + try + { + SqlCnt.Close(); + } + catch (Exception e) + { + throw new Exception("关闭数据库连接失败:" + e); + } + } - /// - /// 执行一条SQL语句 - /// - /// 要执行的SQL语句 - /// 是否关闭连接,默认关闭 - /// 执行SQL语句受影响的行数 - public int ExecuteSqlCommand(string sqlCommand, bool closeConnection = true) - { - if (sqlCommand == null || sqlCommand == string.Empty) - throw new Exception("要执行的SQL语句不能为空"); - OpenConnection(); - SqlCommand sqlCmd = new SqlCommand(sqlCommand, SqlCnt); - try - { - int changeRows = sqlCmd.ExecuteNonQuery(); //执行SQL语句 - if (closeConnection) //关闭连接 - CloseConnection(); - return changeRows; - } - catch (Exception e) - { - throw new Exception("SQL语句存在错误:" + e); - } - } + /// + /// 执行一条SQL语句 + /// + /// 要执行的SQL语句 + /// 是否关闭连接,默认关闭 + /// 执行SQL语句受影响的行数 + public int ExecuteSqlCommand(string sqlCommand, bool closeConnection = true) + { + if (sqlCommand == null || sqlCommand == string.Empty) + throw new Exception("要执行的SQL语句不能为空"); + OpenConnection(); + SqlCommand sqlCmd = new SqlCommand(sqlCommand, SqlCnt); + try + { + int changeRows = sqlCmd.ExecuteNonQuery(); //执行SQL语句 + if (closeConnection) //关闭连接 + CloseConnection(); + return changeRows; + } + catch (Exception e) + { + throw new Exception("SQL语句存在错误:" + e); + } + } - /// - /// 通过sql语句获取数据表 - /// - /// 获取表的select语句 - /// 获取到的数据表 - public DataTable GetTable(string selectSqlCommand) - { - if (selectSqlCommand == null || selectSqlCommand == string.Empty) - throw new Exception("要执行的select语句不能为空"); - OpenConnection(); - SqlDataAdapter sqlDataAdapter = new SqlDataAdapter(selectSqlCommand, SqlCnt); - DataTable dataTable = new DataTable(); - try - { - sqlDataAdapter.Fill(dataTable); //通过SqlDataAdapter填充DataTable对象 - } - catch (Exception e) - { - throw new Exception("select语句有错或者数据表不存在:" + e); - } - finally - { - CloseConnection(); - } - return dataTable; - } + /// + /// 通过sql语句获取数据表 + /// + /// 获取表的select语句 + /// 获取到的数据表 + public DataTable GetTable(string selectSqlCommand) + { + if (selectSqlCommand == null || selectSqlCommand == string.Empty) + throw new Exception("要执行的select语句不能为空"); + OpenConnection(); + SqlDataAdapter sqlDataAdapter = new SqlDataAdapter(selectSqlCommand, SqlCnt); + DataTable dataTable = new DataTable(); + try + { + sqlDataAdapter.Fill(dataTable); //通过SqlDataAdapter填充DataTable对象 + } + catch (Exception e) + { + throw new Exception("select语句有错或者数据表不存在:" + e); + } + finally + { + CloseConnection(); + } + return dataTable; + } - /// - /// 通过表名获取数据表 - /// - /// 获取数据表的名称 - /// 查询的数据行数 - /// 获取到的数据表 - public DataTable GetTable(string tableName, int rows) - { - if (tableName == null || tableName == string.Empty) - throw new Exception("要获取的数据表名称不能为空"); - OpenConnection(); - SqlDataAdapter sqlDataAdapter = new SqlDataAdapter("select top " + rows + " * from " + tableName, SqlCnt); - DataTable dataTable = new DataTable(); - try - { - sqlDataAdapter.Fill(dataTable); //通过SqlDataAdapter填充DataTable对象 - CloseConnection(); - return dataTable; - } - catch (Exception e) - { - throw new Exception("数据表不存在:" + e); - } - } + /// + /// 通过表名获取数据表 + /// + /// 获取数据表的名称 + /// 查询的数据行数 + /// 获取到的数据表 + public DataTable GetTable(string tableName, int rows) + { + if (tableName == null || tableName == string.Empty) + throw new Exception("要获取的数据表名称不能为空"); + OpenConnection(); + SqlDataAdapter sqlDataAdapter = new SqlDataAdapter("select top " + rows + " * from " + tableName, SqlCnt); + DataTable dataTable = new DataTable(); + try + { + sqlDataAdapter.Fill(dataTable); //通过SqlDataAdapter填充DataTable对象 + CloseConnection(); + return dataTable; + } + catch (Exception e) + { + throw new Exception("数据表不存在:" + e); + } + } - /// - /// 按流的方式单向读取数据 - /// (使用SqlDataReader) - /// - /// 获取数据的select语句 - /// SqlDataReader对象 - public SqlDataReader GetDataStream(string selectSqlCommand) - { - if (selectSqlCommand == null || selectSqlCommand == string.Empty) - throw new Exception("要执行的select语句不能为空"); - OpenConnection(); - SqlCommand sqlCmd = new SqlCommand(selectSqlCommand, SqlCnt); - try - { - SqlDataReader reader = sqlCmd.ExecuteReader(); //建立SqlDataReader对象 - return reader; - } - catch (Exception e) - { - throw new Exception("select语句存在错误或者数据表不存在:" + e); - } - } + /// + /// 按流的方式单向读取数据 + /// (使用SqlDataReader) + /// + /// 获取数据的select语句 + /// SqlDataReader对象 + public SqlDataReader GetDataStream(string selectSqlCommand) + { + if (selectSqlCommand == null || selectSqlCommand == string.Empty) + throw new Exception("要执行的select语句不能为空"); + OpenConnection(); + SqlCommand sqlCmd = new SqlCommand(selectSqlCommand, SqlCnt); + try + { + SqlDataReader reader = sqlCmd.ExecuteReader(); //建立SqlDataReader对象 + return reader; + } + catch (Exception e) + { + throw new Exception("select语句存在错误或者数据表不存在:" + e); + } + } - /// - /// 添加数据到指定DataSet中 - /// (添加到一张表) - /// - /// 被填充的DataSet - /// 获取数据的select语句 - /// 插入数据表的表名 - public void AddDataToDataSet(DataSet dataSet, string selectSqlCommands, string insertTableName) - { - if (dataSet == null) - throw new Exception("要填充数据的DataSet不能为null"); - if (selectSqlCommands == null || selectSqlCommands == string.Empty) - throw new Exception("获取数据的select语句不能为空"); - if (insertTableName == null || insertTableName == string.Empty) - throw new Exception("插入的表名不能为空"); - SqlDataAdapter sqlDataAdapter = new SqlDataAdapter(selectSqlCommands, SqlCnt); - try - { - sqlDataAdapter.Fill(dataSet, insertTableName); //通过SqlDataAdapter向DataSet中填充数据 - } - catch (Exception e) - { - throw new Exception("select语句存在错误:" + e); - } - finally - { - CloseConnection(); - } - } + /// + /// 添加数据到指定DataSet中 + /// (添加到一张表) + /// + /// 被填充的DataSet + /// 获取数据的select语句 + /// 插入数据表的表名 + public void AddDataToDataSet(DataSet dataSet, string selectSqlCommands, string insertTableName) + { + if (dataSet == null) + throw new Exception("要填充数据的DataSet不能为null"); + if (selectSqlCommands == null || selectSqlCommands == string.Empty) + throw new Exception("获取数据的select语句不能为空"); + if (insertTableName == null || insertTableName == string.Empty) + throw new Exception("插入的表名不能为空"); + SqlDataAdapter sqlDataAdapter = new SqlDataAdapter(selectSqlCommands, SqlCnt); + try + { + sqlDataAdapter.Fill(dataSet, insertTableName); //通过SqlDataAdapter向DataSet中填充数据 + } + catch (Exception e) + { + throw new Exception("select语句存在错误:" + e); + } + finally + { + CloseConnection(); + } + } - /// - /// 添加数据到指定DataSet中 - /// (添加到多张表) - /// - /// 被填充的DataSet - /// 获取数据的select语句列表 - /// 对应sql语句列表的插入表名列表 - public void AddDataToDataSet(DataSet dataSet, List selectSqlCommands, List insertTableNames) - { - if (dataSet == null) - throw new Exception("要填充数据的DataSet不能为null"); - if (selectSqlCommands == null || selectSqlCommands.Count == 0) - throw new Exception("获取数据的select语句列表不能为空"); - if (insertTableNames == null || insertTableNames.Count == 0) - throw new Exception("插入表名列表不能为空"); - if (selectSqlCommands.Count != insertTableNames.Count) - throw new Exception("select语句列表与插入表名列表长度不一致"); - //拼接select语句列表,获取最终执行的select语句 - string selectCommand = string.Empty; - foreach (string cmd in selectSqlCommands) - if (cmd.Last() == ';') - selectCommand += cmd; - else - selectCommand += (cmd + ";"); - SqlDataAdapter sqlDataAdapter = new SqlDataAdapter(selectCommand, SqlCnt); - //通过插入表名列表,指定数据插入的数据表名称 - sqlDataAdapter.TableMappings.Add("Table", insertTableNames.ElementAt(0)); - for (int i = 1; i < insertTableNames.Count; i++) - sqlDataAdapter.TableMappings.Add("Table" + i, insertTableNames.ElementAt(i)); - try - { - sqlDataAdapter.Fill(dataSet); //通过SqlDataAdapter向DataSet中填充数据 - } - catch (Exception e) - { - throw new Exception("select语句列表中存在错误的sql语句:" + e); - } - finally - { - CloseConnection(); - } - } + /// + /// 添加数据到指定DataSet中 + /// (添加到多张表) + /// + /// 被填充的DataSet + /// 获取数据的select语句列表 + /// 对应sql语句列表的插入表名列表 + public void AddDataToDataSet(DataSet dataSet, List selectSqlCommands, List insertTableNames) + { + if (dataSet == null) + throw new Exception("要填充数据的DataSet不能为null"); + if (selectSqlCommands == null || selectSqlCommands.Count == 0) + throw new Exception("获取数据的select语句列表不能为空"); + if (insertTableNames == null || insertTableNames.Count == 0) + throw new Exception("插入表名列表不能为空"); + if (selectSqlCommands.Count != insertTableNames.Count) + throw new Exception("select语句列表与插入表名列表长度不一致"); + //拼接select语句列表,获取最终执行的select语句 + string selectCommand = string.Empty; + foreach (string cmd in selectSqlCommands) + if (cmd.Last() == ';') + selectCommand += cmd; + else + selectCommand += (cmd + ";"); + SqlDataAdapter sqlDataAdapter = new SqlDataAdapter(selectCommand, SqlCnt); + //通过插入表名列表,指定数据插入的数据表名称 + sqlDataAdapter.TableMappings.Add("Table", insertTableNames.ElementAt(0)); + for (int i = 1; i < insertTableNames.Count; i++) + sqlDataAdapter.TableMappings.Add("Table" + i, insertTableNames.ElementAt(i)); + try + { + sqlDataAdapter.Fill(dataSet); //通过SqlDataAdapter向DataSet中填充数据 + } + catch (Exception e) + { + throw new Exception("select语句列表中存在错误的sql语句:" + e); + } + finally + { + CloseConnection(); + } + } - /// - /// 提交对数据表进行的修改 - /// - /// 修改的数据表 - /// 创建数据表的sql语句 - public void UpdateTable(DataTable dataTable, string createTableSqlCommand) - { - if (dataTable == null) - throw new Exception("修改的数据表不能为空"); - if (createTableSqlCommand == null || createTableSqlCommand == string.Empty) - throw new Exception("创建数据表的sql语句不能为空"); - SqlDataAdapter sqlDataAdapter = new SqlDataAdapter(createTableSqlCommand, SqlCnt); - //为SqlDataAdapter赋予SqlCommandBuilder功能 - SqlCommandBuilder sqlCommandBuilder = new SqlCommandBuilder(sqlDataAdapter); - try - { - sqlDataAdapter.Update(dataTable); //批量提交表中的所有修改 - } - catch (Exception e) - { - throw new Exception("向数据库批量提交修改失败:" + e); - } - } + /// + /// 提交对数据表进行的修改 + /// + /// 修改的数据表 + /// 创建数据表的sql语句 + public void UpdateTable(DataTable dataTable, string createTableSqlCommand) + { + if (dataTable == null) + throw new Exception("修改的数据表不能为空"); + if (createTableSqlCommand == null || createTableSqlCommand == string.Empty) + throw new Exception("创建数据表的sql语句不能为空"); + SqlDataAdapter sqlDataAdapter = new SqlDataAdapter(createTableSqlCommand, SqlCnt); + //为SqlDataAdapter赋予SqlCommandBuilder功能 + SqlCommandBuilder sqlCommandBuilder = new SqlCommandBuilder(sqlDataAdapter); + try + { + sqlDataAdapter.Update(dataTable); //批量提交表中的所有修改 + } + catch (Exception e) + { + throw new Exception("向数据库批量提交修改失败:" + e); + } + } - /// - /// 提交对数据表进行的修改 - /// (在DataSet中的数据表) - /// - /// 修改的数据表所在的DataSet + /// + /// 提交对数据表进行的修改 + /// (在DataSet中的数据表) + /// + /// 修改的数据表所在的DataSet /// 被修改的数据表名 - /// 创建数据表的sql语句 - public void UpdateTable(DataSet dataset, string TableName, string createTableSqlCommand) - { - if (dataset == null) - throw new Exception("修改过的DataSet不能为null"); - if (TableName == null || TableName == string.Empty) - throw new Exception("数据表名不能为空"); - if (createTableSqlCommand == null || createTableSqlCommand == string.Empty) - throw new Exception("创建数据表的select语句不能为空"); - SqlDataAdapter sqlDataAdapter = new SqlDataAdapter(createTableSqlCommand, SqlCnt); - //为SqlDataAdapter赋予SqlCommandBuilder功能 - SqlCommandBuilder sqlCommandBuilder = new SqlCommandBuilder(sqlDataAdapter); - try - { - sqlDataAdapter.Update(dataset, TableName); //批量提交表中的所有修改 - } + /// 创建数据表的sql语句 + public void UpdateTable(DataSet dataset, string TableName, string createTableSqlCommand) + { + if (dataset == null) + throw new Exception("修改过的DataSet不能为null"); + if (TableName == null || TableName == string.Empty) + throw new Exception("数据表名不能为空"); + if (createTableSqlCommand == null || createTableSqlCommand == string.Empty) + throw new Exception("创建数据表的select语句不能为空"); + SqlDataAdapter sqlDataAdapter = new SqlDataAdapter(createTableSqlCommand, SqlCnt); + //为SqlDataAdapter赋予SqlCommandBuilder功能 + SqlCommandBuilder sqlCommandBuilder = new SqlCommandBuilder(sqlDataAdapter); + try + { + sqlDataAdapter.Update(dataset, TableName); //批量提交表中的所有修改 + } catch (Exception e) - { - throw new Exception("向数据库批量提交修改失败:" + e); - } - } - } + { + throw new Exception("向数据库批量提交修改失败:" + e); + } + } + } } \ No newline at end of file diff --git a/Xus.SQLServerHelper/Xus.SQLServerHelper.csproj b/Xus.SQLServerHelper/Xus.SQLServerHelper.csproj old mode 100755 new mode 100644 index 04b16fc..8e974a5 --- a/Xus.SQLServerHelper/Xus.SQLServerHelper.csproj +++ b/Xus.SQLServerHelper/Xus.SQLServerHelper.csproj @@ -11,6 +11,8 @@ Xus.SQLServerHelper v4.5 512 + 1.0 + false true diff --git a/test/Program.cs b/test/Program.cs index 8553619..460fb8d 100755 --- a/test/Program.cs +++ b/test/Program.cs @@ -1,8 +1,4 @@ using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; using System.Data; using System.Data.SqlClient; using Xus.SQLServerHelper; diff --git a/test/Properties/AssemblyInfo.cs b/test/Properties/AssemblyInfo.cs index 6216ba2..45a3b66 100755 --- a/test/Properties/AssemblyInfo.cs +++ b/test/Properties/AssemblyInfo.cs @@ -1,5 +1,4 @@ using System.Reflection; -using System.Runtime.CompilerServices; using System.Runtime.InteropServices; // 有关程序集的常规信息通过以下