Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
150 views
in Technique[技术] by (71.8m points)

c# - Why can´t i insert data into my database and change the files directories after?

Im having this small problem where im trying to move the my xml files to a different directory and also insert them into the database i debugged the code and saw that most of my variables are going into the db empty wich is bad, so if you guys could tell me how i can still have my variables filled just like the onesi have before the error′s.

    using System;
    using System.IO;
    using System.Xml;
    using System.Xml.Serialization;

namespace XMLReaderConsoleAPP1
{
class Program
{
    static DataAccess da = new DataAccess();
    public static void Main()
    {
        
        XmlSerializer serializer = new XmlSerializer(typeof(ImportSession));
        foreach (string filename in Directory.EnumerateFiles(@"C:XMLFiles", "*.xml"))
        {
            string fName = "";
            string BatchCName = "";
            string batchName = "";
            string description = "";
            ProcessFile(filename, serializer, fName, BatchCName, batchName, description );
        }
        Console.ReadKey();

    }

    private static void ProcessFile(string filename, XmlSerializer serializer, string fName, string batchName, string description, string BatchCName)
    {
     
        Console.WriteLine("A processar xml: " + filename);
        bool temErro = false;
        
        using (FileStream file = File.OpenRead(filename))
        {
            var session = (ImportSession)serializer.Deserialize(file);

            XmlDocument xml = new XmlDocument();
            xml.Load(filename);

           
            foreach (Batch batch in session.Batches)
            {
               
                foreach (Document doc in batch.Documents)
                {
                    foreach (Page page in doc.Pages)
                    {
                        if (!string.IsNullOrEmpty(batch.Processed.ToString()))
                        {
                            if (page.HasError)
                            {
                                fName = filename;
                                BatchCName = batch.BatchClassName;
                                batchName = batch.Name;
                                description = batch.Description;
                                string Import = page.ImportFileName;
                                Console.WriteLine("Página com erro:" + Import + fName);
                                temErro = true;
                                DataAccess da = new DataAccess();                                   
                                da.SP_Insert(filename,fName,BatchCName,batchName,description,1,Import,0, "");

                            }
                        }
                    }
                }
                

            }

            //Console.WriteLine(filename);
        }
        if (temErro)
            Console.WriteLine("Ficheiro com erro: " + filename);
        else
        {
            
            Console.WriteLine("Ficheiro processado: " + filename);
            string rootFolderPath = @"C:XMLFiles";
            string destinationPath = @"C:XMLFilesDONE";
            //string[] fileList = Directory.GetFiles(rootFolderPath);
            //foreach (string file1 in fileList)
            //{
            //FileInfo fi = new FileInfo(file1);
            string moveFrom = Path.Combine(rootFolderPath, filename);
            string moveTo = Path.Combine(destinationPath, filename);
            File.Move(moveFrom, moveTo);
            da.SP_Insert(filename, fName, BatchCName, batchName, description, 0, "", 1, moveTo);
            //} 

        }

    }
}

public class ImportSession
{
    public Batch[] Batches { get; set; }
}
public class Batch
{
    [XmlAttribute]
    public string Name { get; set; }
    [XmlAttribute]
    public string Description { get; set; }
    [XmlAttribute]
    public string BatchClassName { get; set; }
    [XmlAttribute]
    public bool Processed { get; set; }



    public Document[] Documents { get; set; }
}

public class Document
{
    [XmlAttribute]
    public string FormTypeName { get; set; }
    public IndexField[] IndexFields { get; set; }
    public Page[] Pages { get; set; }
}

public class IndexField
{
    [XmlAttribute]
    public string Name { get; set; }
    [XmlAttribute]
    public string Value { get; set; }
}

public class Page
{
    [XmlAttribute]
    public string ImportFileName { get; set; }
    [XmlAttribute]
    public string ErrorCode { get; set; }
    [XmlAttribute]
    public string ErrorMessage { get; set; }
    [XmlIgnore]
    public bool HasError => !string.IsNullOrWhiteSpace(ErrorMessage);
}

}

As you guys can see this problem is ocorring on the else statment before i start my object classes and the insert that worked was on the start of my using method. Any more information please ask.

question from:https://stackoverflow.com/questions/65935066/why-can%c2%b4t-i-insert-data-into-my-database-and-change-the-files-directories-after

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...