1 ///
2 /// 返回DataTable3 ///
4 ///
5 ///
6 public DataTable GetDataTable(string strSql)7 {8 try9 {10 using (SqlConnection con = new SqlConnection(conStr))11 {12 DataSet ds = new DataSet();13 SqlDataAdapter da = new SqlDataAdapter(strSql, con);14 da.Fill(ds);15 return ds.Tables[0];16 }17 }18 catch (Exception ex)19 {20 return null;21 throw ex;22 }23 }24
25 ///
26 /// 执行sql,返回受影响的行数27 ///
28 ///
29 ///
30 public bool ExecuteSql(string strSql)31 {32 try33 {34 using (SqlConnection con = new SqlConnection(conStr))35 {36 SqlCommand command = new SqlCommand(strSql, con);37 con.Open();38 return command.ExecuteNonQuery() > 0;39 }40 }41 catch42 {43 return false;44 }45 }46
47 ///
48 /// 执行SQL命令49 ///
50 ///
51 ///
52 public int ExecuteCommand(SqlCommand cmd)53 {54 try55 {56 using (SqlConnection con = new SqlConnection(conStr))57 {58 cmd.Connection = con;59 con.Open();60 return cmd.ExecuteNonQuery();61 }62 }63 catch64 {65 return -1;66 }67 }68
69 ///
70 /// 执行sql,返回首行首列71 ///
72 ///
73 ///
74 public object ExecuteScalar(string strSql)75 {76 try77 {78 using (SqlConnection con = new SqlConnection(conStr))79 {80 SqlCommand command = new SqlCommand(strSql, con);81 con.Open();82 return command.ExecuteScalar();83 }84 }85 catch (Exception ex)86 {87 throw ex;88 }89 }90
91 ///
92 /// 执行事务93 ///
94 ///
95 ///
96 public bool ExecuteSqlTran(ListsqlStrList)97 {98 try99 {100 using (SqlConnection con = new SqlConnection(conStr))101 {102 con.Open();103 using (SqlTransaction sqlTransaction = con.BeginTransaction())104 {105 try106 {107 foreach (string sqlStr in sqlStrList)108 {109 SqlCommand command = new SqlCommand(sqlStr, con);110 command.Transaction = sqlTransaction;111 command.ExecuteNonQuery();112 }113 sqlTransaction.Commit();114 return true;115 }116 catch (Exception transEx)117 {118 sqlTransaction.Rollback();119 return false;120 throw transEx;121 }122 }123 }124 }125 catch (Exception ex)126 {127 return false;128 throw ex;129 }130 }