protected void ReturnDataSetTest()
{
string sql = @"select * from user where id=@user_id;select * from role where id=@role_id;
;select * from permission where id=123;";
DataSet dataSet= SqlHelper.ReturnDataSet(SqlHelper.getConDB(), sql, CommandType.Text,new SqlParameter[] { new SqlParameter ("@user_id", 1) }, new SqlParameter ("@role_id", 2) });
List<string> logs = new List<string>();
foreach (DataTable table in dataSet.Tables)
{
logs.Add("--------------------------------");
logs.Add("--------------------------------");
logs.Add(table.TableName);
string columnNames="";
foreach (DataColumn column in table.Columns)
{
columnNames += column.ColumnName + " ";
}
logs.Add(columnNames);
logs.Add("--------------------------------");
foreach (DataRow row in table.Rows)
{
string rowsContent = "";
foreach (DataColumn column in table.Columns)
{
Object temp_cell = row.Field<Object>(column.ColumnName);
rowsContent += temp_cell==null?"": temp_cell.ToString() + " ";
}
logs.Add(rowsContent);
}
}
Logs.Write("Test",logs);
}
/// <summary>
/// 获取连库语句
/// </summary>
/// <returns></returns>
public static string getConDB()
{
return ConfigurationManager.ConnectionStrings["Dental_Patient_DBConnectionString"].ToString();
}
public static DataSet ReturnDataSet(string ConnStr, string cmdtext, CommandType ct, SqlParameter[] para)
{
SqlConnection conn = null;
using (conn = new SqlConnection(ConnStr))
{
conn.Open();
var cmd = new SqlCommand(cmdtext, conn);
//类型
cmd.CommandType = ct;
//参数数组
if (para != null) cmd.Parameters.AddRange(para);
var da = new SqlDataAdapter(cmd);
var ds = new DataSet();
try
{
da.Fill(ds);
}
catch (Exception err)
{
throw err;
}
finally
{
if (conn.State != ConnectionState.Closed)
{
conn.Close();
}
}
return ds;
}
}