取dataset转多table

 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 logs = new List();
        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(column.ColumnName);
                    rowsContent += temp_cell==null?"": temp_cell.ToString() + "    ";
                }
                logs.Add(rowsContent);
            }
        }
        Logs.Write("Test",logs);
    }
 
 
    /// 
    /// 获取连库语句
    /// 
    /// 
    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;
        }
    }

你可能感兴趣的:(取dataset转多table)