C#實(shí)現(xiàn)Excel導(dǎo)入sqlite的方法
更新時(shí)間:2014年09月19日 11:25:13 投稿:shichen2014
這篇文章主要介紹了C#實(shí)現(xiàn)Excel導(dǎo)入sqlite的方法,是C#程序設(shè)計(jì)中非常重要的一個(gè)實(shí)用技巧,需要的朋友可以參考下
本文實(shí)例講述了C#實(shí)現(xiàn)Excel導(dǎo)入sqlite的方法,是非常實(shí)用的技巧。分享給大家供大家參考。具體方法如下:
首先需要引用system.date.sqlite
具體實(shí)現(xiàn)代碼如下:
system.date.sqlite
system.date.sqlite.linq
//導(dǎo)入--Excel導(dǎo)入sqlite
private void button2_Click(object sender, EventArgs e)
{
DAL.Sqlite da = new DAL.Sqlite("DataByExcel.db");
if (chk_sfzj.Checked==false)
{
//刪除全部數(shù)據(jù)
if (da.SqlExSQLiteCommand("delete from sqllitebyexcel"))
{
}
else
{
MessageBox.Show("刪除原失敗,請(qǐng)聯(lián)系管理員!");
}
}
OpenFileDialog ofg = new OpenFileDialog();
ofg.Filter = "*.xls|*.xls";
if (ofg.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
string sName = ofg.FileName;
if (new BLL.Excelcs().OutExcel(sName, da))
{
MessageBox.Show("導(dǎo)入成功");
//bdData("");
}
else
{
MessageBox.Show("導(dǎo)入失敗");
}
}
}
/// <summary>
/// 初始化數(shù)據(jù)庫
/// </summary>
/// <param name="strSqlitePath">數(shù)據(jù)庫文件路徑</param>
SQLiteConnection SQLCon;
public Sqlite(string dataName)
{
SQLCon = new SQLiteConnection(string.Format("Data Source={0}{1}", System.AppDomain.CurrentDomain.BaseDirectory, dataName));
}
/// <summary>
/// 執(zhí)行sql語句
/// </summary>
/// <param name="strSql">sql語句</param>
/// <returns>是否執(zhí)行成功</returns>
public bool SqlExSQLiteCommand(string strSql)
{
SqlOpen();
SQLiteCommand cmd = new SQLiteCommand();
cmd.Connection = SQLCon;
cmd.CommandText = strSql;
try
{
int i = cmd.ExecuteNonQuery();
return true;
}
catch (Exception ex)
{
return false;
}
}
/// <summary>
/// 導(dǎo)入數(shù)據(jù)到數(shù)據(jù)庫
/// </summary>
/// <param name="outFile">文件</param>
/// <param name="sql">數(shù)據(jù)庫操作對(duì)象</param>
/// <returns></returns>
public bool OutExcel(string outFile,DAL.Sqlite sql)
{
DataTable dt = DAL.Excel.TransferData(outFile, "Sheet1").Tables[0];
try
{
foreach (DataRow item in dt.Rows)
{
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 = string.Format(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], DateTime.Now.ToDate(), "");
sql.SqlExSQLiteCommand(strEnd);
}
return true;
}
catch (Exception ex)
{
// MessBox.Show("");
string aa = ex.Message;
return false;
}
}
public static string ToDate(this object obj)
{
// if (obj == null || string.IsNullOrEmpty(obj.ToString()))
if(string.IsNullOrEmpty(obj.ToString().Trim()))
{
return "null";
}
return ((DateTime)obj).ToString("yyyy-MM-dd HH:mm:ss");
}
/// <summary>
/// 獲取excel表數(shù)據(jù)
/// </summary>
/// <param name="excelFile">excel文件路徑</param>
/// <param name="sheetName">excel工作表名</param>
/// <returns></returns>
public static DataSet TransferData(string excelFile, string sheetName)
{
DataSet ds = new DataSet();
//獲取全部數(shù)據(jù)
string strConn = "Provider=Microsoft.Jet.OLEDB.4.0;" + "Data Source=" + excelFile + ";" + "Extended Properties=Excel 8.0;";
OleDbConnection conn = new OleDbConnection(strConn);
try
{
conn.Open();
string strExcel = "";
OleDbDataAdapter myCommand = null;
strExcel = string.Format("select * from [{0}$]", sheetName);
myCommand = new OleDbDataAdapter(strExcel, strConn);
myCommand.Fill(ds);
}
catch (Exception ex)
{
throw new Exception(ex.Message);
}
finally
{
conn.Close();
}
return ds;
}
相信本文所述對(duì)大家的C#程序設(shè)計(jì)有一定的借鑒價(jià)值。
相關(guān)文章
C#實(shí)現(xiàn)關(guān)閉其他程序窗口或進(jìn)程代碼分享
這篇文章主要介紹了C#實(shí)現(xiàn)關(guān)閉其他程序窗口或進(jìn)程代碼分享,本文給出了兩種方法,并分別給出示例代碼,需要的朋友可以參考下2015-06-06
C#多線程學(xué)習(xí)之(四)使用線程池進(jìn)行多線程的自動(dòng)管理
這篇文章主要介紹了C#多線程學(xué)習(xí)之使用線程池進(jìn)行多線程的自動(dòng)管理,實(shí)例分析了C#中線程池的概念與相關(guān)的使用技巧,非常具有實(shí)用價(jià)值,需要的朋友可以參考下2015-04-04
c#利用Excel直接讀取數(shù)據(jù)到DataGridView
這個(gè)例子的功能是c#讀取excel文件,大家可以參考使用2013-11-11
C#中實(shí)現(xiàn)PriorityQueue優(yōu)先級(jí)隊(duì)列的代碼
這篇文章主要介紹了C#中PriorityQueue優(yōu)先級(jí)隊(duì)列的實(shí)現(xiàn),構(gòu)造初始化這部分主要介紹關(guān)鍵的字段和方法,比較器的初始化以及堆的初始化,需要的朋友可以參考下2021-12-12

