private ReportingCommand ReadQueryFromSharedDataSet(string source, string path, string reference) { using (var conn = new SqlConnection()) { //create connection and define sql query conn.ConnectionString = source; var cmd = new SqlCommand(); cmd.Connection = conn; cmd.CommandText = ReadQueryFromContent("QueryFromSharedDataSet"); //create the two parameters for the sql query var paramPath = new SqlParameter("SharedDataSetPath", System.Data.SqlDbType.NVarChar, 425); paramPath.Value = path; cmd.Parameters.Add(paramPath); var paramReference = new SqlParameter("SharedDataSetName", System.Data.SqlDbType.NVarChar, 425); paramReference.Value = reference; cmd.Parameters.Add(paramReference); //execute the command conn.Open(); var dr = cmd.ExecuteReader(); if (dr.Read()) { var command = new ReportingCommand(); command.CommandType = (CommandType)Enum.Parse(typeof(CommandType), dr.GetString(2)); //CommandType command.Text = dr.GetString(3); //CommandText return(command); } } return(null); }
public ReportingCommand ExtractCommand(SharedDatasetRequest request) { var reportName = request.SharedDatasetName.EndsWith(".rsd") ? request.SharedDatasetName : request.SharedDatasetName + ".rsd"; var fullPath = string.Format("{0}{1}{2}", request.Source, request.Path, reportName); if (!File.Exists(fullPath)) { throw new ArgumentException(string.Format("No shared dataset found on path '{0}{1}' with name '{2}'", request.Source, request.Path, reportName)); } //If the file is found then we need to select the query inside the file var docXml = new XmlDocument(); docXml.Load(fullPath); var root = docXml.FirstChild; if (root.NodeType == XmlNodeType.XmlDeclaration) { root = root.NextSibling; } var xpath = string.Format("//rd:SharedDataSet/rd:DataSet[@Name=\"\"]/rd:Query/rd:CommandText"); var nsmgr = new XmlNamespaceManager(docXml.NameTable); nsmgr.AddNamespace("rd", root.GetNamespaceOfPrefix(string.Empty)); var node = docXml.SelectSingleNode(xpath, nsmgr); if (node != null) { var text = node.InnerText; // We've found the query var reportCommand = new ReportingCommand() { Text = text }; xpath = string.Format("//rd:SharedDataSet/rd:DataSet[@Name=\"\"]/rd:Query/rd:CommandType"); node = docXml.SelectSingleNode(xpath, nsmgr); if (node == null) { reportCommand.CommandType = CommandType.Text; } else { reportCommand.CommandType = (CommandType)Enum.Parse(typeof(CommandType), node.InnerText); } return(reportCommand); } throw new ArgumentException(string.Format("Cannot find the command text in the shared dataSet at '{0}'", fullPath)); }
private ReportingCommand SearchDataSet(string source, string reportPath, string reportName, string dataSetName, ref List <string> otherDataSets) { using (var conn = new SqlConnection()) { //create connection and define sql query conn.ConnectionString = source; var cmd = new SqlCommand(); cmd.Connection = conn; cmd.CommandText = ReadQueryFromContent("ListDataSet"); //create the three parameters for the sql query var paramReportPath = new SqlParameter("ReportPath", System.Data.SqlDbType.NVarChar, 425); paramReportPath.Value = reportPath; cmd.Parameters.Add(paramReportPath); var paramReportName = new SqlParameter("ReportName", System.Data.SqlDbType.NVarChar, 425); paramReportName.Value = reportName; cmd.Parameters.Add(paramReportName); //execute the command conn.Open(); var dr = cmd.ExecuteReader(); while (dr.Read()) { if (dr.GetString(2) == dataSetName) { var command = new ReportingCommand(); command.CommandType = (CommandType)Enum.Parse(typeof(CommandType), dr.GetString(4)); //CommandType command.Text = dr.GetString(5); //CommandText return(command); } else { otherDataSets.Add(dr.GetString(2)); } } } return(null); }
public ReportingCommand ExtractCommand(ReportDataSetRequest request) { var reportName = request.ReportName.EndsWith(".rdl") ? request.ReportName : request.ReportName + ".rdl"; var fullPath = string.Format("{0}{1}{2}", request.Source, request.Path, reportName); if (!File.Exists(fullPath)) { throw new ArgumentException(string.Format("No report found on path '{0}{1}' with name '{2}'", request.Source, request.Path, request.ReportName)); } //Load the xml var docXml = new XmlDocument(); docXml.Load(fullPath); var root = docXml.FirstChild; if (root.NodeType == XmlNodeType.XmlDeclaration) { root = root.NextSibling; } //Check that the data set exist var xpath = string.Format("//rd:Report/rd:DataSets/rd:DataSet[@Name=\"{0}\"]", request.DataSetName); //var xpath = "//Report"; var nsmgr = new XmlNamespaceManager(docXml.NameTable); nsmgr.AddNamespace("rd", root.GetNamespaceOfPrefix(string.Empty)); var node = docXml.SelectSingleNode(xpath, nsmgr); if (node == null) { throw BuildDataSetNotFoundException(request, docXml, "//rd:Report/rd:DataSets/rd:DataSet", nsmgr); } //Search in the xml the DataSet and especially the CommandText within this dataset xpath = string.Format("//rd:Report/rd:DataSets/rd:DataSet[@Name=\"{0}\"]/rd:Query/rd:CommandText", request.DataSetName); node = docXml.SelectSingleNode(xpath, nsmgr); if (node != null) { var text = node.InnerText; // Weve fond the query var reportCommand = new ReportingCommand() { Text = text }; xpath = string.Format("//rd:Report/rd:DataSets/rd:DataSet[@Name=\"{0}\"]/rd:Query/rd:CommandType", request.DataSetName); node = docXml.SelectSingleNode(xpath, nsmgr); if (node == null) { reportCommand.CommandType = CommandType.Text; } else { reportCommand.CommandType = (CommandType)Enum.Parse(typeof(CommandType), node.InnerText); } return(reportCommand); } //If not found then we'll check if it's not a shared dataset xpath = string.Format("//rd:Report/rd:DataSets/rd:DataSet[@Name=\"{0}\"]/rd:SharedDataSet/rd:SharedDataSetReference", request.DataSetName); node = docXml.SelectSingleNode(xpath, nsmgr); if (node == null) { throw new ArgumentException(string.Format("The data set named '{0}' has been found but no command text or shared dataset reference has been found", request.DataSetName)); } var sharedDataSetName = node.InnerText + ".rsd"; var subRequest = new SharedDatasetRequest ( request.Source, request.Path, sharedDataSetName ); return(ExtractCommand(subRequest)); }