C # reads the data and displays the Dataggrideview. Use Excel first, then use Sqlite3.

Source: Internet
Author: User
Tags sqlite try catch

Recently in an order management software, consider reading Excel directly and then displaying the data on the DataGridView. , and then use Npoi to manipulate Excel.

1 stringStrcon ="Provider=microsoft.ace.oledb.12.0;data source="+ Path +"; Extended properties= ' Excel 12.0; Hdr=yes;imex=0 '";2 //string Strcon = "Provider=microsoft.ace.oledb.12.0;data source=d:/score table 2013.xlsx; Extended properties= ' Excel 12.0; Hdr=yes;imex=0 ' ";3OleDbConnection myconn =NewOleDbConnection (Strcon);4 stringSselect ="SELECT * FROM [sheet1$]";5 MyConn.Open ();6OleDbDataAdapter MyDataAdapter =NewOleDbDataAdapter (Sselect, myconn);7DataSet myDataSet =NewDataSet ();8Mydataadapter.fill (myDataSet);//, "[sheet1$]");9 myconn.close ();TenDatagridview_items. DataSource = mydataset.tables[0]. DefaultView;//Show to DataGridView

This method is very inefficient and shows that the data is brushed out one line at a time and is not available. Then choose to use the lightweight sqlite3:

Download the relevant dll:https://www.sqlite.org/download.html downloads on the website below

. NET tools:

Load SQLite

using System.Data.SQLite; using System.Data.SqlClient;

Use:

Pay attention to the use of try,catch, otherwise it will not show the data and do not hold the wrong. After try catch, you will find that you cannot load SQLite.Interop.dll. You need to place the DLL under the project path, and then set the property to copy to the output value: Always copy:

if(file.exists (path)) {Try{Sqliteconnection conn=NewSqliteconnection ("Data Source ="+path); Conn.                    Open (); Sqlitedataadapter DataAdapter=NewSqlitedataadapter ("Select * FROM Product", conn); DataSet DS=NewDataSet ();                    DataAdapter.Fill (DS); Datagridview_items. DataSource= ds. tables[0]; Conn.                Close (); }                Catch(System.Exception ex) {MessageBox.Show (ex).                Message); }            }            Else{MessageBox.Show ("No related database found"); }

C # reads the data and displays the Dataggrideview. Use Excel first, then use Sqlite3.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.