C#實(shí)現(xiàn)Excel導(dǎo)入sqlite的方法
本文實(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ù)庫(kù)
/// </summary>
/// <param name="strSqlitePath">數(shù)據(jù)庫(kù)文件路徑</param>
SQLiteConnection SQLCon;
public Sqlite(string dataName)
{
SQLCon = new SQLiteConnection(string.Format("Data Source={0}{1}", System.AppDomain.CurrentDomain.BaseDirectory, dataName));
}
/// <summary>
/// 執(zhí)行sql語(yǔ)句
/// </summary>
/// <param name="strSql">sql語(yǔ)句</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ù)庫(kù)
/// </summary>
/// <param name="outFile">文件</param>
/// <param name="sql">數(shù)據(jù)庫(kù)操作對(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#利用XML創(chuàng)建Excel文檔的實(shí)現(xiàn)方法
這篇文章主要介紹了C#利用XML創(chuàng)建Excel文檔的實(shí)現(xiàn)方法,需要的朋友可以參考下2014-08-08
WPF實(shí)現(xiàn)動(dòng)畫(huà)效果
這篇文章介紹了WPF實(shí)現(xiàn)動(dòng)畫(huà)效果的方法,對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2022-01-01
在C#中處理時(shí)間戳和時(shí)區(qū)的解決方法
處理時(shí)間戳和不同的時(shí)區(qū)可能是軟件開(kāi)發(fā)中的一個(gè)棘手問(wèn)題,尤其是當(dāng)系統(tǒng)不確定給定的日期時(shí)間是UTC還是本地時(shí)間時(shí),通常會(huì)遇到與時(shí)間轉(zhuǎn)換相關(guān)的問(wèn)題,在這篇文章中,我將分享我們?nèi)绾卧贑#中處理時(shí)間戳和時(shí)區(qū),需要的朋友可以參考下2024-06-06
C#實(shí)現(xiàn)Bitmap類(lèi)型與Byte[]類(lèi)型相互轉(zhuǎn)化的示例詳解
在C#編程中,Bitmap類(lèi)型和Byte[]類(lèi)型之間的相互轉(zhuǎn)化是圖像處理和數(shù)據(jù)傳輸中常見(jiàn)的需求,Bitmap類(lèi)型表示一個(gè)位圖圖像,而B(niǎo)yte[]類(lèi)型則是一個(gè)字節(jié)數(shù)組,本文將詳細(xì)介紹如何在這兩種類(lèi)型之間進(jìn)行相互轉(zhuǎn)化,需要的朋友可以參考下2024-07-07
深入分析C#連接Oracle數(shù)據(jù)庫(kù)的連接字符串詳解
本篇文章是對(duì)C#連接Oracle數(shù)據(jù)庫(kù)的連接字符串進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下2013-05-05

