Ivonne Aspilcueta

Ivonne Aspilcueta

  • 1.1k
  • 601
  • 8.4k

A way to save massive amount of lines from text file to database in C#

Jul 22 2024 8:24 PM

Hello,

I am able to extract each line and save into database from a .txt file, but it is only saving 1000 lines, is there a way to save all the lines if they are more than 15,000 lines? 

protected void Button1_Click(object sender, EventArgs e)
{
    try
    { 

        if (!FileUpload1.HasFile) //Validation
        {
            Response.Write("No file Selected"); return;
        }
        else
        {
            // Read and Extract Information from the Index.txt File:

            string path = ConfigurationManager.AppSettings["ImageBasePath"];
            var files = Directory.GetFiles(path, "index.txt", SearchOption.AllDirectories);
     
            // Read and populate textboxes from Index.txt        
            string filePath = Path.Combine(files);
                             

            var lines = File.ReadAllLines(Path.Combine(filePath));

            //var lines = File.ReadAllLines(filePath);
            foreach (var line in lines)

            {
                var parts = line.Split('|');
                if (parts.Length >= 9)
                {
                    // Extract required information
                    string acctNumber = parts[0];
                    string chkNumber = parts[1];
                    string chkAmount = parts[2];
                    string imgDate = parts[5];
                    string imagePath = parts[8];

                    // Populate the textboxes
                    txtAcctNumber.Text = acctNumber.ToString();
                    txtChkNumber.Text = chkNumber.ToString();
                    txtChkAmount.Text = chkAmount.ToString();
                    txtDate.Text = imgDate;

                    //Optional, store the imagePath if needed
                    ViewState["ImagePath"] = imagePath;

                    // break; // Remove this if you want to handle multiple lines
                }


                // Extract values from textboxes
                string acctNumbers = txtAcctNumber.Text;
                string chkNumbers = txtChkNumber.Text;
                string chksAmount = txtChkAmount.Text;
                string imgDates = txtDate.Text;
                string imagePaths = ViewState["ImagePath"].ToString();

                // Retrieve the base path from the configuration
                string basePath = ConfigurationManager.AppSettings["ImageBasePath"];

                // Construct the full path to the image file
                string fullImagePath = Path.Combine(basePath, imagePaths);


                // Convert the image to byte array
                byte[] imageData = File.ReadAllBytes(fullImagePath);
                //string imageBase64 = Convert.ToBase64String(imageData);

                //Console.WriteLine(imageBase64);

                // Insert the data into the database
                using (SqlConnection connection = new SqlConnection(ConfigurationManager.ConnectionStrings["DBCU"].ToString()))
                {
                    connection.Open();
                    SqlCommand cmd = new SqlCommand("INSERT INTO chkImages (acctNumber, chkNumber,chkAmount, imgDate, imageData) VALUES (@acctNumber, @chkNumber, @chkAmount, @imgDate, @imageData)", connection);

                    cmd.Parameters.AddWithValue("@acctNumber", acctNumbers);
                    cmd.Parameters.AddWithValue("@chkNumber", chkNumbers);
                    cmd.Parameters.AddWithValue("@chkAmount", chksAmount);
                    cmd.Parameters.AddWithValue("@imgDate", imgDates);
                    cmd.Parameters.AddWithValue("@imageData", imageData);

                    cmd.ExecuteNonQuery();
                    connection.Close();

                    Response.Write("Image has been Added");
                }
            }
        }
    }
    catch (Exception ex)
    {
        // Handle any exceptions that occurred
        Console.WriteLine("An error occurred while reading the file: " + ex.Message);
    }
}

 


Answers (3)