java mvc ef_MVC-EF数据操作(一)

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 }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值