/// <summary> /// 获取DataSet /// </summary> /// <param name="sqlCmd">SqlStruct对象</param> /// <param name="strTableName">表名</param> /// <returns>DataSet</returns> public DataSet GetEntity(SqlStruct sqlCmd, string strTableName) { DataSet ds = new DataSet(); FillEntity(sqlCmd, ds, strTableName); return(ds); }
/// <summary> /// 执行只返回一个结果的数据库操作 /// </summary> /// <param name="sqlCmd">SqlStruct对象</param> /// <returns>执行结果</returns> public object ExecuteScalar(SqlStruct sqlCmd) { Open(); IDbCommand cmd = m_conn.CreateCommand(); cmd.CommandType = sqlCmd.CommandType; cmd.CommandText = sqlCmd.SqlString; if (sqlCmd.Param != null) { foreach (string s in sqlCmd.Param.Keys) { IDbDataParameter parameter = cmd.CreateParameter(); parameter.ParameterName = s; parameter.Value = sqlCmd.Param[s]; cmd.Parameters.Add(parameter); } } try { cmd.Transaction = m_trans; object res = null; if (m_trans == null) { try { BeginTrans(); cmd.Transaction = m_trans; res = cmd.ExecuteScalar(); CommitTrans(); } catch { RollbackTrans(); throw; } } else { res = cmd.ExecuteScalar(); } return(res); } catch (Exception e) { throw new Exception(string.Format("执行Sql语句出错——{0}", cmd.CommandText), e); } finally { if (m_trans == null) { Close(); } } }
/// <summary> /// 填充DataSet /// </summary> /// <param name="sqlCmd">SqlStruct对象</param> /// <param name="ds">被填充的DataSet</param> /// <param name="strTableName">表名</param> public void FillEntity(SqlStruct sqlCmd, DataSet ds, string strTableName) { IDbCommand cmd = m_conn.CreateCommand(); cmd.Transaction = m_trans; cmd.CommandType = sqlCmd.CommandType; cmd.CommandText = sqlCmd.SqlString; if (sqlCmd.Param != null) { foreach (string s in sqlCmd.Param.Keys) { IDbDataParameter parameter = cmd.CreateParameter(); parameter.ParameterName = s; parameter.Value = sqlCmd.Param[s]; cmd.Parameters.Add(parameter); } } Fill(cmd, ds, strTableName); }