查询函数
时间:2010-09-09 来源:zhang448630412
#region 查询函数
// 返回DataSet
public override DataSet ExcuteDataSet(string cmdText)
{
DataSet dataSet = new DataSet();
SqlDataAdapter sqlDataAdapter = new SqlDataAdapter(cmdText, _sqlConnection);
sqlDataAdapter.Fill(dataSet);
return dataSet;
}
public override DataSet ExcuteDataSet(DbCommand sqlCommand)
{
DataSet dataSet = new DataSet();
SqlDataAdapter sqlDataAdapter = new SqlDataAdapter(((SqlCommand)sqlCommand));
sqlDataAdapter.Fill(dataSet);
return dataSet;
}
// 返回带有空数据行的DataSet
public override DataSet GetEmptyRowStyleDataSet(string cmdText)
{
cmdText += "where 1=2";
DataSet dataSet = new DataSet();
SqlDataAdapter sqlDataAdapter = new SqlDataAdapter(cmdText, _sqlConnection);
sqlDataAdapter.Fill(dataSet);
DataTable dt = new DataTable();
dt = dataSet.Tables[0];
DataRow dataRow = dt.NewRow();
foreach (DataColumn dataColumn in dt.Columns)
{
dataRow[dataColumn] = DBNull.Value;
}
dt.Rows.Add(dataRow);
return dataSet;
}
// 传入参数组
public override DataSet ExcuteDataSetWithParaArray(string parmArray)
{
string cmdText = "select * from City where RegionID in (" + parmArray + ")";
DataSet dataSet = new DataSet();
SqlDataAdapter sqlDataAdapter = new SqlDataAdapter(cmdText, _sqlConnection);
sqlDataAdapter.Fill(dataSet);
return dataSet;
}
// 传入参数组防止注入改良版
public override DataSet ExcuteDataSetWithParaList(string parmArray)
{
string[] array = parmArray.Split(',');
int paraCount = array.Length;
StringBuilder cmdText = new StringBuilder("select * from City where RegionID in ()");
SqlParameter[] sps = new SqlParameter[array.Length];
for (int i = 0; i < paraCount; i++)
{
string para = "@regionID"+i;
if (i == 0)
{
cmdText.Insert(cmdText.Length - 1, para);
}
else
{
cmdText.Insert(cmdText.Length - 1, ("," + para));
}
sps[i] = new SqlParameter(para, array[i]);
}
SqlCommand sqlCommand = new SqlCommand();
sqlCommand.Parameters.AddRange(sps);
sqlCommand.Connection = _sqlConnection;
sqlCommand.CommandText = cmdText.ToString();
DataSet dataSet = new DataSet();
SqlDataAdapter sqlDataAdapter = new SqlDataAdapter();
sqlDataAdapter.SelectCommand = sqlCommand;
sqlDataAdapter.Fill(dataSet);
return dataSet;
}
// 返回影响行数
public override int ExcuteNonQuery(string cmdText)
{
SqlCommand sqlCommand = new SqlCommand(cmdText, _sqlConnection);
return ExcuteNonQuery(sqlCommand);
}
public override int ExcuteNonQuery(DbCommand sqlCommand)
{
return ((SqlCommand)sqlCommand).ExecuteNonQuery();
}
// 返回SqlDataReader
public override DbDataReader ExcuteDataReader(string cmdText)
{
SqlCommand sqlCommand = new SqlCommand(cmdText, _sqlConnection);
return ExcuteDataReader(sqlCommand);
}
public override DbDataReader ExcuteDataReader(DbCommand sqlCommand)
{
return ((SqlCommand)sqlCommand).ExecuteReader();
}
// 关闭DataReader
public override void CloseDataReader(DbDataReader dbDataReader)
{
if (dbDataReader == null || dbDataReader.IsClosed == true)
return;
dbDataReader.Close();
}
// 返回Scalar(第一行第一列)
public override object ExcuteDataScaler(string cmdText)
{
SqlCommand sqlCommand = new SqlCommand(cmdText, _sqlConnection);
return ExcuteDataScaler(sqlCommand);
}
public override object ExcuteDataScaler(DbCommand sqlCommand)
{
return ((SqlCommand)sqlCommand).ExecuteScalar();
}
#endregion