/// <summary> /// 执行带参数的SQL语句--静态方法 /// </summary> /// <param name="cmdText">sql语句</param> /// <param name="sp">SqlParameter数组</param> public static bool SRunSQL(string cmdText, SqlParameter[] sp) { DBSql dbSql = new DBSql(); try { dbSql.command = dbSql.CreateCommand(cmdText); if (sp != null) { for (int i = 0; i < sp.Length; i++) { dbSql.command.Parameters.Add(sp[i]);//将参数加到Command中 } } dbSql.command.ExecuteNonQuery(); return(true); } catch (SqlException ex) { return(false); throw ex; } finally { dbSql.CloseDataReader(); dbSql.CloseConnection(); } }
/// <summary> /// 创建结果集对象--静态方法 /// </summary> /// <param name="sql">sql语句</param> /// <param name="sp">参数数组</param> /// <returns>DataSet结果集</returns> public static DataSet SCreateDataSet(string sql, SqlParameter[] sp) { DBSql dsSql = new DBSql(); try { dsSql.dataadapter = dsSql.CreateDataAdapter(sql); if (sp != null) { for (int i = 0; i < sp.Length; i++) { dsSql.dataadapter.SelectCommand.Parameters.Add(sp[i]);//将参数加到Command中 } } dsSql.dataset = new DataSet(); dsSql.dataadapter.Fill(dsSql.dataset); } catch (Exception ex) { throw ex; } finally { dsSql.CloseDataReader(); dsSql.CloseConnection(); } return(dsSql.dataset); }
/// <summary> /// 执行更新操作--静态方法 /// </summary> /// <param name="sql">sql语句</param> /// <returns>bool</returns> public static bool STRunSQL(string sql) { DBSql dbSql = new DBSql(); try { dbSql.command = dbSql.CreateCommand(sql); dbSql.command.ExecuteNonQuery(); return(true); } catch (Exception ex) { return(false); throw ex; } finally { dbSql.CloseConnection(); } }
/// <summary> /// 创建结果集对象--静态方法 /// </summary> /// <param name="sql">sql语句</param> /// <returns>DataSet对象</returns> public static DataSet SCreateDataSet(string sql) { DBSql dsSql = new DBSql(); try { dsSql.dataadapter = dsSql.CreateDataAdapter(sql); dsSql.dataset = new DataSet(); dsSql.dataadapter.Fill(dsSql.dataset); } catch { return(null); } finally { dsSql.CloseDataReader(); dsSql.CloseConnection(); } return(dsSql.dataset); }