Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7056351
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T03:51:25+00:00 2026-05-28T03:51:25+00:00

I have a problem, when I want get some data from CSV file to

  • 0

I have a problem, when I want get some data from CSV file to Database, my setp as fllow:

  1. I get this CSV file from special path
  2. insert to a datatable
  3. transfer data from datatable to database

now. when I access to no.2, i find 2 coulmns value is blank, who can help me??

Thanks

my code:

DataTable dt = new DataTable();

        string _filePath, _fileName;
        _filePath = strFileName.Substring(0, strFileName.LastIndexOf(@"\") + 1);
        _fileName = strFileName.Substring(strFileName.LastIndexOf(@"\") + 1);

        string excelConnectionString;
        //string strFileName = @"D:\TestFile\Prelim\USG\aaa.xlsx";            
        //string excelConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source= " + _filePath + @"\" + ";Extended Properties=\"Text;HDR=Yes;FMT=Delimited\"";
        try
        {                
            excelConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;" + "Data Source=" + _filePath + @"\" + ";Extended Properties=\"Text;HDR=Yes;FMT=Delimited\"";
        }
        catch
        {
            excelConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;" + "Data Source=" + _filePath + @"\" + ";Extended Properties=\"Text;HDR=Yes;FMT=Delimited\"";
        }

        try
        {
            OleDbConnection excelConnection = null;
            using (excelConnection = new OleDbConnection(excelConnectionString))
            {
                OleDbDataAdapter apter = new OleDbDataAdapter("Select * from " + _fileName, excelConnection);

                apter.Fill(dt);
            }
        }
        catch (Exception ex)
        {
            throw;
        }

        return dt;
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T03:51:26+00:00Added an answer on May 28, 2026 at 3:51 am

    first step is to remove the noise. both try/catches are useless. the first one will never throw because you are simply concatenating strings. the second will provides no value it’s the same as not catching at all.

    i believe you want to use the name of the worksheet for the tablename, not the filename. and once the datatable is filled you then need to save those records to the database.

    var connectionString = string.Format("Provider=Microsoft.ACE.OLEDB.12.0;Data Source={0};Extended Properties=""Text;HDR=Yes;FMT=Delimited""");
    using(var connection = new OleDbConnection(connectionString))
    using(var command = connection.CreateCommand())
    {
        connection.Open();
        command.CommandText = "select * from [Sheet1]";
    
        var table = new DataTable();
        using(var reader = command.ExecuteReader())
        {
             table.Load(reader);
             return table;
        }
    }
    

    then you can use this table to populate the database

    var connectionString = "sql connection string";
    using(var connection = new SqlConnection(connectionString))
    {
        connection.Open();
        using(var tx = connection.BeginTransaction())
        {
            try
            {
                foreach(DataRow row in table.Rows)
                {
                    using(var command = connection.CreateCommand())
                    {
                       command.CommandText = "insert into table (columns...) values (@p1,...)";
                       //add parameters to command
                       command.ExecuteNonQuery();
                    } 
                 }
                 tx.Commit();
            }
            catch
            {
               tx.RollBack();
               throw;
            }
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have one problem , I want to get some data from XML file
I have a PHP server script that SELECTs some data from a MySQL database.
I Have activity that get some data from the internet, and shows it to
i have a problem. i want to get info about my string have other
I'm having a problem with fetching some data. The tables I have (for testing
i'm trying to migrate some data from two tables in an OLD database, to
Today I needed to parse some data out from an xlsx file (Office open
I want to get images from a flickr rss but this code won't get
I want to make a page that displays some data from a DB, so
I've got a situation which I want to fetch data from a database, and

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.