Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Text file rows to database records?

Hi all,

I have got a text file in comma delimited format , I have to read that file and enter records into the database.

How is it possible?

Comments

  • raylouwraylouw Member Posts: 30
    : Hi all,
    :
    : I have got a text file in comma delimited format , I have to read that file and enter records into the database.
    :
    : How is it possible?
    :
    :


    Try this:

    public DataSet ConnectCSV(string filetable)
    {
    DataSet ds = new DataSet();
    try
    {
    conn_excel_str = "Driver={Microsoft Text Driver (*.txt; *.csv)};Dbq="
    + HttpContext.Current.Server.MapPath(".//" + csv_store_read);
    //Server.MapPath(".//" + csv_store_read)
    //Path mapped to csv file temp stored on the server

    string sql_select;
    System.Data.Odbc.OdbcConnection obj_oledb_con;
    System.Data.Odbc.OdbcDataAdapter obj_oledb_da;
    //Create connection to CSV file
    obj_oledb_con = new System.Data.Odbc.OdbcConnection(conn_excel_str);
    //Open the connection
    obj_oledb_con.Open();
    //Fetch records from CSV
    sql_select = "select * from ["+ filetable +"]";
    obj_oledb_da = new System.Data.Odbc.OdbcDataAdapter(sql_select,
    obj_oledb_con);
    //Fill dataset with the records from CSV file
    obj_oledb_da.Fill(ds);
    //Close Connection to CSV file
    obj_oledb_con.Close();
    }
    catch(Exception e) //Error
    {}
    return ds;
    }
Sign In or Register to comment.