Click here to Skip to main content
15,891,529 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
how can we import excel data to SQLite database.
I can import to data table but unable to do for sqlite

CODE ADDED

C#
if (browsefile == true)
 {
 txtFilePath.Text = openfile.FileName;

 Microsoft.Office.Interop.Excel.Application excelApp = new Microsoft.Office.Interop.Excel.Application();
 Microsoft.Office.Interop.Excel.Workbook excelBook = excelApp.Workbooks.Open(txtFilePath.Text.ToString(), 0, true, 5, "", "", true, Microsoft.Office.Interop.Excel.XlPlatform.xlWindows, "\t", false, false, 0, true, 1, 0);
 Microsoft.Office.Interop.Excel.Worksheet excelSheet = (Microsoft.Office.Interop.Excel.Worksheet)excelBook.Worksheets.get_Item(1); ;
 Microsoft.Office.Interop.Excel.Range excelRange = excelSheet.UsedRange;

 string strCellData = "";
 double douCellData;
 int rowCnt = 0;
 int colCnt = 0;


 DataTable dt = new DataTable();

 for (colCnt = 1; colCnt <= excelRange.Columns.Count; colCnt++)
 {
 string strColumn = "";
 strColumn = (string)(excelRange.Cells[1, colCnt] as Microsoft.Office.Interop.Excel.Range).Value2;
 dt.Columns.Add(strColumn, typeof(string));
 }

 for (rowCnt = 2; rowCnt <= excelRange.Rows.Count; rowCnt++)
 {
 string strData = "";
 for (colCnt = 1; colCnt <= excelRange.Columns.Count; colCnt++)
 {
 try
 {
 strCellData = (string)(excelRange.Cells[rowCnt, colCnt] as Microsoft.Office.Interop.Excel.Range).Value2;
 strData += strCellData + "|";
 }
 catch (Exception ex)
 {
 douCellData = (excelRange.Cells[rowCnt, colCnt] as Microsoft.Office.Interop.Excel.Range).Value2;
 strData += douCellData.ToString() + "|";
 }
 }
 strData = strData.Remove(strData.Length - 1, 1);
 dt.Rows.Add(strData.Split('|'));
 }
 SQLiteConnection.CreateFile("MyDatabase.sqlite");
 SQLiteConnection m_dbConnection;
 m_dbConnection = new SQLiteConnection("Data Source=MyDatabase.sqlite;Version=3;");
 m_dbConnection.Open();
 string sql = "create table signals(name varchar(20), date datetime)";
 SQLiteCommand command = new SQLiteCommand(sql, m_dbConnection);
 command.ExecuteNonQuery();
 
//I wast stuck here to insert data pls help me

string sql = "insert into signals (name, date) values

 excelBook.Close(true, null, null);
 excelApp.Quit();
 }
Posted
Updated 12-Jul-15 21:42pm
v3

1 solution

If you can already import the data from an Excel file to a data table in your program, you're half way there.

The next step is that you:
- create a connection to SQLite
- create a command which will be used for inserting the data into the database
- create a loop which will loop through the rows in your data table
- for each row you set the parameters for the insert statement and execute it.

To get to the actual coding, have a look at Getting started with SQLite in C#[^]

Pseudo example added
C#
string sql = "insert into signals (name, date) values (:name, :date)"
SQLiteCommand insertCommand = new SQLiteCommand(sql, m_dbConnection);
insertCommand.Parameters.Add(new SQLiteParameter("name"));
insertCommand.Parameters.Add(new SQLiteParameter("date"));
foreach(DataRow originalRow in dt.Rows) {
   insertCommand.Parameters["name"].Value = originalRow[0].ToString();
   insertCommand.Parameters["date"].Value = originalRow[1].ToString();
   try {
      insertCommand.ExecuteNonQuery();
   } catch {
      ...
   }
}
 
Share this answer
 
v2
Comments
jayanthik 13-Jul-15 3:24am    
hi mika, tx for replying
I am stuck at inserting

if (browsefile == true)
{
txtFilePath.Text = openfile.FileName;

Microsoft.Office.Interop.Excel.Application excelApp = new Microsoft.Office.Interop.Excel.Application();
Microsoft.Office.Interop.Excel.Workbook excelBook = excelApp.Workbooks.Open(txtFilePath.Text.ToString(), 0, true, 5, "", "", true, Microsoft.Office.Interop.Excel.XlPlatform.xlWindows, "\t", false, false, 0, true, 1, 0);
Microsoft.Office.Interop.Excel.Worksheet excelSheet = (Microsoft.Office.Interop.Excel.Worksheet)excelBook.Worksheets.get_Item(1); ;
Microsoft.Office.Interop.Excel.Range excelRange = excelSheet.UsedRange;

string strCellData = "";
double douCellData;
int rowCnt = 0;
int colCnt = 0;


DataTable dt = new DataTable();

for (colCnt = 1; colCnt <= excelRange.Columns.Count; colCnt++)
{
string strColumn = "";
strColumn = (string)(excelRange.Cells[1, colCnt] as Microsoft.Office.Interop.Excel.Range).Value2;
dt.Columns.Add(strColumn, typeof(string));
}

for (rowCnt = 2; rowCnt <= excelRange.Rows.Count; rowCnt++)
{
string strData = "";
for (colCnt = 1; colCnt <= excelRange.Columns.Count; colCnt++)
{
try
{
strCellData = (string)(excelRange.Cells[rowCnt, colCnt] as Microsoft.Office.Interop.Excel.Range).Value2;
strData += strCellData + "|";
}
catch (Exception ex)
{
douCellData = (excelRange.Cells[rowCnt, colCnt] as Microsoft.Office.Interop.Excel.Range).Value2;
strData += douCellData.ToString() + "|";
}
}
strData = strData.Remove(strData.Length - 1, 1);
dt.Rows.Add(strData.Split('|'));
}
SQLiteConnection.CreateFile("MyDatabase.sqlite");
SQLiteConnection m_dbConnection;
m_dbConnection = new SQLiteConnection("Data Source=MyDatabase.sqlite;Version=3;");
m_dbConnection.Open();
string sql = "create table signals(name varchar(20), date datetime)";
SQLiteCommand command = new SQLiteCommand(sql, m_dbConnection);
command.ExecuteNonQuery();
//stuck here string sql = "insert into signals (name, date) values


excelBook.Close(true, null, null);
excelApp.Quit();
}
pls let me know hw can I instert in table
Wendelius 13-Jul-15 3:27am    
Can you modify the question so that the INSERT portion of your code is shown properly. Now there's only
string sql = "insert into signals (name, date) values
Use the "Improve question" link to make the modifications. I already added the code you posted in the comment.
jayanthik 13-Jul-15 3:34am    
I don't see any changes in my code. pls let me know hw to insert data in table I was stuck to insert data in table from data table.
Wendelius 13-Jul-15 3:39am    
Is this the code you curently have for the INSERT?
string sql = "insert into signals (name, date) values


excelBook.Close(true, null, null);
excelApp.Quit();
jayanthik 13-Jul-15 3:44am    
mika I am unable to insert data from data table to signals table
string sql = "insert into signals (name, date) values.................... fill dis pls .
I tried with "string sql = "insert into signals (name, date) values from dt"
its not working

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900