/// <summary> /// 带事务执行SqlCommand /// </summary> /// <param name="M_str_sqlstr">SQL语句</param> /// <param name="pars">参数表</param> public static void ExecuteNonQueryTr(string M_str_sqlstr, SqlParameter[] pars) { SqlConnection sqlcon = MsSqlData.GetConn(); sqlcon.Open(); SqlCommand sqlcom = new SqlCommand(M_str_sqlstr, sqlcon); sqlcom.Parameters.AddRange(pars); SqlTransaction tr = sqlcon.BeginTransaction(); sqlcom.Transaction = tr; try { sqlcom.ExecuteNonQuery(); tr.Commit(); } catch (Exception ex) { tr.Rollback(); throw ex; } sqlcom.Dispose(); sqlcon.Close(); sqlcon.Dispose(); }
/// <summary> /// 创建一个DataSet对象 /// </summary> /// <param name="M_str_sqlstr">SQL语句</param> /// <param name="M_str_table">表名</param> /// <returns>返回DataSet对象</returns> public static DataSet Getds(string M_str_sqlstr, string M_str_table) { SqlConnection sqlcon = MsSqlData.GetConn(); SqlDataAdapter sqlda = new SqlDataAdapter(M_str_sqlstr, sqlcon); DataSet myds = new DataSet(); sqlda.Fill(myds, M_str_table); return(myds); }
/// <summary> /// 创建一个SqlDataReader对象 /// </summary> /// <param name="M_str_sqlstr">SQL语句</param> /// <returns>返回SqlDataReader对象</returns> public static SqlDataReader GetDr(string M_str_sqlstr) { SqlConnection sqlcon = MsSqlData.GetConn(); SqlCommand sqlcom = new SqlCommand(M_str_sqlstr, sqlcon); sqlcon.Open(); SqlDataReader sqlread = sqlcom.ExecuteReader(CommandBehavior.CloseConnection); return(sqlread); }
/// <summary> /// 创建一个DataSet对象 /// </summary> /// <param name="M_str_sqlstr">SQL语句</param> /// <param name="pars">参数表</param> /// <param name="M_str_table">表名</param> /// <returns>返回DataSet对象</returns> public static DataSet Getds(string M_str_sqlstr, SqlParameter[] pars, string M_str_table) { SqlConnection sqlcon = MsSqlData.GetConn(); SqlDataAdapter sqlda = new SqlDataAdapter(M_str_sqlstr, sqlcon); sqlda.SelectCommand.Parameters.AddRange(pars); DataSet myds = new DataSet(); sqlda.Fill(myds, M_str_table); return(myds); }
/// <summary> /// 更新数据表 /// </summary> /// <param name="dt">表名</param> /// <param name="selsql">sql查询语句</param> public static void UpdateDs(DataTable dt, string selsql) { SqlConnection sqconn = MsSqlData.GetConn(); SqlDataAdapter sqld = new SqlDataAdapter(selsql, sqconn); SqlCommandBuilder sqlb_zb = new SqlCommandBuilder(sqld); sqld.MissingSchemaAction = MissingSchemaAction.AddWithKey;//主键约束 sqld.Update(dt); sqld.Dispose(); sqconn.Close(); }
/// <summary> /// 执行SqlCommand,无返回结果, /// </summary> /// <param name="M_str_sqlstr">SQL语句</param> public static void ExecuteNonQuery(string M_str_sqlstr) { SqlConnection sqlcon = MsSqlData.GetConn(); sqlcon.Open(); SqlCommand sqlcom = new SqlCommand(M_str_sqlstr, sqlcon); sqlcom.ExecuteNonQuery(); sqlcom.Dispose(); sqlcon.Close(); sqlcon.Dispose(); }
/// <summary> /// 执行SqlCommand,无返回结果, /// </summary> /// <param name="M_str_sqlstr">SQL语句</param> /// <param name="pars">参数表</param> public static void ExecuteNonQuery(string M_str_sqlstr, SqlParameter[] pars) { SqlConnection sqlcon = MsSqlData.GetConn(); sqlcon.Open(); SqlCommand sqlcom = new SqlCommand(M_str_sqlstr, sqlcon); sqlcom.Parameters.AddRange(pars); sqlcom.ExecuteNonQuery(); sqlcom.Dispose(); sqlcon.Close(); sqlcon.Dispose(); }
/// <summary> /// 执行SqlCommand,返回对象 /// </summary> /// <param name="M_str_sqlstr">SQL语句</param> public static object ExecuteScalar(string M_str_sqlstr) { SqlConnection sqlcon = MsSqlData.GetConn(); sqlcon.Open(); SqlCommand sqlcom = new SqlCommand(M_str_sqlstr, sqlcon); object obj = sqlcom.ExecuteScalar(); //if (obj.ToString() != "") //{ i = (Decimal)obj; } sqlcom.Dispose(); sqlcon.Close(); sqlcon.Dispose(); return(obj); }