using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Data; using System.Data.SqlClient; namespace ICS.WCF.Base { public class DBhlper { public static DataTable Query(string sql,string sqlconn) { try { if (sqlconn == "") { throw new Exception("连接U9字符串取得失败"); } SqlConnection conn = new SqlConnection(sqlconn); SqlDataAdapter da = new SqlDataAdapter(sql, conn); DataTable dt = new DataTable(); da.Fill(dt); return dt; } catch (Exception ex) { throw ex; } } public static string getDataSource( string sqlconn) { try { if (sqlconn == "") { throw new Exception("连接U9字符串取得失败"); } SqlConnection conn = new SqlConnection(sqlconn); return "["+conn.DataSource+"].["+conn.Database+"]"; } catch (Exception ex) { throw ex; } } public static bool ReaerEXecute(string sql, string sqlconn) { try { SqlConnection conn = new SqlConnection(sqlconn); SqlCommand cmd = new SqlCommand(sql, conn); conn.Open(); SqlDataReader a = cmd.ExecuteReader(); //conn.Close(); return a.HasRows; } catch (Exception ex) { throw ex; } } public static int EXecute(string sql,string sqlconn) { try { SqlConnection conn = new SqlConnection(sqlconn); SqlCommand cmd = new SqlCommand(sql, conn); conn.Open(); int a = cmd.ExecuteNonQuery(); conn.Close(); return a; } catch (Exception ex) { throw ex; } } public static void SQLRunAll(List SqlAll, string sqlconn) { try { if (sqlconn == "") { throw new Exception("连接U9字符串取得失败"); } SqlConnection connetion = new SqlConnection(sqlconn); //创建数据库连接 SqlTransaction transaction = null; //声明事务 SqlCommand command = null; connetion.Open(); //打开数据库连接 transaction = connetion.BeginTransaction(); //使用数据库连接创建事务 command = connetion.CreateCommand(); command.Transaction = transaction; try { for (int i = 0; i < SqlAll.Count; i++) { if (!string.IsNullOrEmpty(SqlAll[i])) //判断当前SQL语句是否为空 { command.CommandText = SqlAll[i]; command.ExecuteNonQuery(); } } transaction.Commit(); //事务提交 } catch (Exception ex) { transaction.Rollback(); //事务撤销 throw new Exception(); } finally { connetion.Close(); } } catch (Exception ex) { throw new Exception(); } } } }