SoFunction
Updated on 2025-03-06

C# method to implement Excel import sqlite

This article describes the method of importing SQLite in C#, which is a very practical technique. Share it for your reference. The specific methods are as follows:

First, citation is needed

The specific implementation code is as follows:


 
//Import--Excel import sqliteprivate void button2_Click(object sender, EventArgs e)
{
   da = new ("");
  if (chk_sfzj.Checked==false)
  {
 //Delete all data if (("delete from sqllitebyexcel"))
 {

 }
 else
 {
   ("Delete the original failed, please contact the administrator!");
 }
  }
  OpenFileDialog ofg = new OpenFileDialog();
   = "*.xls|*.xls";
  if (() == )
  {
 string sName = ;
 if (new ().OutExcel(sName, da))
 {
   ("Imported successfully");
   //bdData("");
 }
 else
 {
   ("Import failed");
 }
  }
}

/// <summary>
/// Initialize the database/// </summary>
/// <param name="strSqlitePath">Database file path</param>
 SQLiteConnection SQLCon;
public Sqlite(string dataName)
{
    SQLCon = new SQLiteConnection(("Data Source={0}{1}", , dataName));
}

 /// &lt;summary&gt;
/// Execute sql statement/// &lt;/summary&gt;
/// <param name="strSql">sql statement</param>/// <returns> Whether the execution is successful</returns>public bool SqlExSQLiteCommand(string strSql)
{
  SqlOpen();
  SQLiteCommand cmd = new SQLiteCommand();
   = SQLCon;
   = strSql;
  try
  {
 int i = ();
 return true;
  }
  catch (Exception ex)
  {
 return false;
  }
}

/// &lt;summary&gt;
/// Import data to the database/// &lt;/summary&gt;    
/// <param name="outFile">file</param>/// <param name="sql">Database operation object</param>/// &lt;returns&gt;&lt;/returns&gt;
public bool OutExcel(string outFile, sql)
{
  DataTable dt = (outFile, "Sheet1").Tables[0];
  try
  {
 foreach (DataRow item in )
 {

   string strSql = @"insert into sqllitebyexcel
  (No,BUSINESS_NO,BUSINESS_TYPE_NAME,VESSEL_NAME_C,VOYAGE,BILL_NO,CTNW1,CTNW2,
    CTNW3,TXDD,XXDD,CTN_NO,CTN_TYPE,NAME1,NAME2,NAME3,IN_DATE,JFJSSJ,JFSC,DYPCD,TXPCSJ,
TXPCSC,JCSJ,TXSC,H986JJYCSJ,YFYXSJ,LXSJ,LXSC,CCJFSJ,TXJCSJ,TXCCSJ,DCTXSC,TimeNow,DDTXSC)
    values('{0}','{1}','{2}','{3}','{4}','{5}','{6}',
'{7}','{8}','{9}','{10}','{11}','{12}','{13}','{14}',
'{15}','{16}','{17}','{18}','{19}','{20}','{21}','{22}','{23}','{24}','{25}','{26}','{27}','{28}','{29}','{30}','{31}','{32}','{33}')";
   string strEnd = (strSql, item[0], item[1], item[2], item[3], item[4], item[5],
 item[6], item[7], item[8], item[9], item[10], item[11], item[12],
 item[13], item[14], item[15], item[16].ToDate(), item[17].ToDate(), item[18], item[19].ToDate(),
 item[20].ToDate(), item[21], item[22].ToDate(), item[23], item[24].ToDate(), item[25].ToDate(), item[26].ToDate(),
 item[27], item[28].ToDate(), item[29].ToDate(), item[30].ToDate(), item[31], (), "");
   (strEnd);
 }
    return true;
  }
  catch (Exception ex)
  {
    // ("");
 string aa = ;
 return false;
  }
}

public static string ToDate(this object obj)
{
  // if (obj == null || (()))
  if((().Trim()))
  {
 return "null";
  }
  return ((DateTime)obj).ToString("yyyy-MM-dd HH:mm:ss");
}
/// &lt;summary&gt;
/// Get the excel table data/// &lt;/summary&gt;
/// <param name="excelFile">excel file path</param>/// <param name="sheetName">excel worksheet name</param>/// &lt;returns&gt;&lt;/returns&gt;
public static DataSet TransferData(string excelFile, string sheetName)
{
  DataSet ds = new DataSet();
  //Get all data  string strConn = "Provider=.4.0;" + "Data Source=" + excelFile + ";" + "Extended Properties=Excel 8.0;";
  OleDbConnection conn = new OleDbConnection(strConn);
  try
  {
    
 ();
 string strExcel = "";
 OleDbDataAdapter myCommand = null;
 strExcel = ("select * from [{0}$]", sheetName);
 myCommand = new OleDbDataAdapter(strExcel, strConn);
 (ds);
  }
  catch (Exception ex)
  {
 throw new Exception();
  }
  finally 
  {
 ();
  }
  return ds;
}

I believe that the description in this article has certain reference value for everyone's C# programming.