Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/313.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# SqlCommand查询和迭代,使用给定程序将数据执行到多个XML文件中_C#_Sql_Xml_Sql Server 2008_Download - Fatal编程技术网

C# SqlCommand查询和迭代,使用给定程序将数据执行到多个XML文件中

C# SqlCommand查询和迭代,使用给定程序将数据执行到多个XML文件中,c#,sql,xml,sql-server-2008,download,C#,Sql,Xml,Sql Server 2008,Download,我正在编写一个原型,以编程方式从数据库获取视频数据,并使用C将这些数据放入XML清单文件中。每个视频都是XML中的一个资产元素,我正在从SQLCommand查询我需要的所有数据 问题:XML文件每个文件最多只能容纳100个视频资源,因此我需要找到一个迭代器,在到达数据库的最后一行之前,每个文件可以保存100个资源。我正在使用whiler.Read,一个SqlDataReader,使用SqlCommand进入数据库 我想知道如何使用特定的SQLCommand和读取器内部的迭代来处理所有需要的文件,

我正在编写一个原型,以编程方式从数据库获取视频数据,并使用C将这些数据放入XML清单文件中。每个视频都是XML中的一个资产元素,我正在从SQLCommand查询我需要的所有数据

问题:XML文件每个文件最多只能容纳100个视频资源,因此我需要找到一个迭代器,在到达数据库的最后一行之前,每个文件可以保存100个资源。我正在使用whiler.Read,一个SqlDataReader,使用SqlCommand进入数据库

我想知道如何使用特定的SQLCommand和读取器内部的迭代来处理所有需要的文件,为每个文件添加100个资产

下面是迄今为止我的代码!显然,我必须改变一些东西,比如XML文件的全局元素,这些元素需要放在每个创建的XML文件中

        protected void btnExecute_Click(object sender, EventArgs e)
    {
        //On btn click, call method to execute program and save all files in local path
        GetVideoData();
    }

    protected void GetVideoData()
    {
        string preparer = "AgencyOasis";
        string type = "VIDEO_FULL";

        XmlDocument doc = new XmlDocument();
        XmlNode docNode = doc.CreateXmlDeclaration("1.0", "UTF-8", null);
        doc.AppendChild(docNode);

        //add global elements:
        //create parameters for each attribute in root to be replaced, and append the new root tag to empty XML doc
        XmlElement root = doc.CreateElement("publisher-upload-manifest");
        root.SetAttribute("publisher-id", tbPublisherID.Text);
        root.SetAttribute("preparer", preparer);
        doc.AppendChild(root);

        //append notify as child to root, already in doc
        if (!string.IsNullOrEmpty(tbEmail.Text)) {
            XmlElement notify = doc.CreateElement("notify");
            notify.SetAttribute("email", tbEmail.Text);
            root.AppendChild(notify);
        }

        //THE REST OF THE ELEMENTS ARE A UNIQUE CASE FOR EACH VIDEO, THEREFORE SHOULD LOOP INSIDE THE QUERY RESULT SET, PER VIDEO.

        string sql100 = "SELECT TOP 100 Location, VideoLibraryId, Title, Keywords, IsActive, Description FROM VideoLibrary";
        const string _connStringName = "SanfordVideosConnectionString";
        string dsn = ConfigurationManager.ConnectionStrings[_connStringName].ConnectionString;
        using (SqlConnection conn = new SqlConnection(dsn))
        using (SqlCommand cmd = new SqlCommand(sql100, conn))
        {
            conn.Open();
            SqlDataReader r = cmd.ExecuteReader();
            while (r.Read())
            {
                //while going through each row with above SQL command, set data to element attributes in doc for each asset
                XmlElement asset = doc.CreateElement("asset");
                asset.SetAttribute("filename", r["Location"].ToString());
                asset.SetAttribute("refid", r["VideoLibraryId"].ToString());

                // TODO: NEED ACTUAL FILE LOCATION BEFORE I CAN EXTRACT THE SIZE OF THE FILE ITSELF
                /*create new FileInfo object to get length of existing video file
                FileInfo f = new FileInfo(r["Location"].ToString());
                long f1 = f.Length;         */

                //asset.SetAttribute("size", "10");      // TODO: f1.ToString() for static value of 2nd @
                // TODO: NEED ACTUAL FILE LOCATION AGAIN FOR HASH-CODE
                //asset.SetAttribute("hash-code", "10");  //TODO: GetMD5Hash(r["Location"].ToString())
                //setting the type globally for all videos to VIDEO_FULL ensures FLV and MP4 formats
                asset.SetAttribute("type", type);
                root.AppendChild(asset);

                XmlElement title = doc.CreateElement("title");
                title.SetAttribute("name", r["Title"].ToString());
                title.SetAttribute("refid", r["VideoLibraryId"].ToString());
                title.SetAttribute("active", r["IsActive"].ToString().ToUpper());
                // TODO: CHECK TO SEE IF VIDEO-FULL-REFID IS CORRECT
                //title.SetAttribute("video-full-refid", r["Location"].ToString() + "-" + r["VideoLibraryId"].ToString());

                XmlElement shortDesc = doc.CreateElement("short-description");
                shortDesc.InnerText = GetTrimmedDescription(250, r["Description"].ToString());
                title.AppendChild(shortDesc);
                root.AppendChild(title);

                XmlElement longDesc = doc.CreateElement("long-description");
                longDesc.InnerText = GetTrimmedDescription(5000, r["Description"].ToString());
                title.AppendChild(longDesc);
                root.AppendChild(title);
            }

        }

        //TEMPORARY FILE SAVE LOCATION  TODO: SAVE MULTIPLE FILES IN LOCAL FOLDER
        //returns the directory from where the current application domain was loaded 
        //string xmlPath = System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, inputFileName1);
        string xmlPath = Server.MapPath(txtBoxInput.Text);
        XmlTextWriter writer = new XmlTextWriter(xmlPath, null);
        writer.Formatting = Formatting.Indented;
        doc.Save(xmlPath);
    }

    //Trims long and short descriptions to max size of chars depending on max size (250 for short and 5000 for long)
    public string GetTrimmedDescription(int maxLength, string desc) {

        if (desc.Length > maxLength)
        {
            return desc.Substring(0, (maxLength - 4)) + " ...";
        }
        else
        {
            return desc;
        }

    }

请随时问我有关该计划的任何问题,我会尽力解释

如果我让你的代码保持原样,这里有一个可能的解决方案。我在一些注释中添加了重构片段的内容

总体思路是获取所有信息,而不仅仅是100条记录,并引入一个跟踪您所在位置的计数器。每100条记录你吐出文件并重置。读完所有的东西后,你必须把剩下的吐出来。由于创建了多个文件,您还必须跟踪这些文件并使用不同的文件名。我刚刚添加了一个文件后缀。我认为下面的代码应该适合您

    protected void btnExecute_Click(object sender, EventArgs e)
{
    //On btn click, call method to execute program and save all files in local path
    GetVideoData();
}

protected void GetVideoData()
    {
        string type = "VIDEO_FULL";

        XmlDocument doc;
        XmlElement root;
        // Refactor document creation to its own function
        doc = CreateANewDoc();
        // Refactor root creation to its own function
        root = CreateANewRoot(doc);

        //THE REST OF THE ELEMENTS ARE A UNIQUE CASE FOR EACH VIDEO, THEREFORE SHOULD LOOP INSIDE THE QUERY RESULT SET, PER VIDEO.

        // remove the top 100 and fetch everything  
        string sql100 = "SELECT Location, VideoLibraryId, Title, Keywords, IsActive, Description FROM VideoLibrary";
        const string _connStringName = "SanfordVideosConnectionString";
        string dsn = ConfigurationManager.ConnectionStrings[_connStringName].ConnectionString;
        using (SqlConnection conn = new SqlConnection(dsn))
        using (SqlCommand cmd = new SqlCommand(sql100, conn))
        {
            conn.Open();
            SqlDataReader r = cmd.ExecuteReader();

            // declare some counters
            int counter = 0;
            int filecounter = 0;

            while (r.Read())
            {
                counter++; // Increment counter

                // when you hit 100, write stuff out and reset
                if (counter == 100 )
                {
                    filecounter++; // Increment filecounter
                    DumpOutFile(doc, filecounter);
                    // Reset counter
                    counter = 0;
                    // Reset doc
                    doc = CreateANewDoc();
                    root = CreateANewRoot(doc);
                }

                //while going through each row with above SQL command, set data to element attributes in doc for each asset
                XmlElement asset = doc.CreateElement("asset");
                asset.SetAttribute("filename", r["Location"].ToString());
                asset.SetAttribute("refid", r["VideoLibraryId"].ToString());

                // TODO: NEED ACTUAL FILE LOCATION BEFORE I CAN EXTRACT THE SIZE OF THE FILE ITSELF
                /*create new FileInfo object to get length of existing video file
                FileInfo f = new FileInfo(r["Location"].ToString());
                long f1 = f.Length;         */

                //asset.SetAttribute("size", "10");      // TODO: f1.ToString() for static value of 2nd @
                // TODO: NEED ACTUAL FILE LOCATION AGAIN FOR HASH-CODE
                //asset.SetAttribute("hash-code", "10");  //TODO: GetMD5Hash(r["Location"].ToString())
                //setting the type globally for all videos to VIDEO_FULL ensures FLV and MP4 formats
                asset.SetAttribute("type", type);
                root.AppendChild(asset);

                XmlElement title = doc.CreateElement("title");
                title.SetAttribute("name", r["Title"].ToString());
                title.SetAttribute("refid", r["VideoLibraryId"].ToString());
                title.SetAttribute("active", r["IsActive"].ToString().ToUpper());
                // TODO: CHECK TO SEE IF VIDEO-FULL-REFID IS CORRECT
                //title.SetAttribute("video-full-refid", r["Location"].ToString() + "-" + r["VideoLibraryId"].ToString());

                XmlElement shortDesc = doc.CreateElement("short-description");
                shortDesc.InnerText = GetTrimmedDescription(250, r["Description"].ToString());
                title.AppendChild(shortDesc);
                root.AppendChild(title);

                XmlElement longDesc = doc.CreateElement("long-description");
                longDesc.InnerText = GetTrimmedDescription(5000, r["Description"].ToString());
                title.AppendChild(longDesc);
                root.AppendChild(title);

            }

            // Dump out whatever is left (handles the remainder after division by 100
            DumpOutFile(doc, filecounter + 1);
        }

    }

//Trims long and short descriptions to max size of chars depending on max size (250 for short and 5000 for long)
public string GetTrimmedDescription(int maxLength, string desc) {

    if (desc.Length > maxLength)
    {
        return desc.Substring(0, (maxLength - 4)) + " ...";
    }
    else
    {
        return desc;
    }

}

private XmlDocument CreateANewDoc()
{
    XmlDocument doc = new XmlDocument();
    XmlNode docNode = doc.CreateXmlDeclaration("1.0", "UTF-8", null);
    doc.AppendChild(docNode);
    return doc;
}

private XmlElement CreateANewRoot(XmlDocument doc)
{
    string preparer = "AgencyOasis";

    //add global elements:
    //create parameters for each attribute in root to be replaced, and append the new root tag to empty XML doc
    XmlElement root = doc.CreateElement("publisher-upload-manifest");
    //root.SetAttribute("publisher-id", tbPublisherID.Text);
    root.SetAttribute("publisher-id", "tbPublisherID.Text");
    root.SetAttribute("preparer", preparer);
    doc.AppendChild(root);

    //append notify as child to root, already in doc
    if (!string.IsNullOrEmpty(tbEmail.Text)) {
        XmlElement notify = doc.CreateElement("notify");
        notify.SetAttribute("email", tbEmail.Text);
        root.AppendChild(notify);
    }

    return root;
}

private void DumpOutFile(XmlDocument doc, int filenumber)
{
    //TEMPORARY FILE SAVE LOCATION  TODO: SAVE MULTIPLE FILES IN LOCAL FOLDER
    //returns the directory from where the current application domain was loaded 
    //string xmlPath = System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, inputFileName1);
    string xmlPath = Server.MapPath(txtBoxInput.Text + filenumber.ToString());
    XmlTextWriter writer = new XmlTextWriter(xmlPath, null);
    writer.Formatting = Formatting.Indented;
    doc.Save(xmlPath);
}

你能在你的sql中写一个select Top 100并传递一个参数给你想从哪里选择它吗?@DJKRAZE说实话,我甚至不知道它是如何工作的。我只想一次浏览100个资产,将它们加载到一个XML文件中,保存该文件,然后转到下一个100个资产。不需要特定的顺序,只需要一次迭代。读完这篇文章后,如果你认为传递一个参数仍然能最有效地工作,你能详细介绍一下你的发现吗?谢谢Kraze我可以使用SqlDataReader方法来处理这个问题吗?我希望采用两步过程方法,从表直接进入XML文件,而不是采取中间步骤,为每个列填充数组,然后使用迭代器为每个XML文件设置属性。任何建议都很好。我选择这个作为最佳答案,因为您以更有效的方式实现了我自己的代码。我不会创建helper方法,主要是因为我必须从我目前编写的草率编码中改进它,因此我感谢您的组织和愿意亲自处理我的问题。我会让你知道,如果我做了任何小的调整或如果有任何问题!再次感谢史蒂文,你的努力揭示了: