Click here to Skip to main content
15,892,746 members
Please Sign up or sign in to vote.
5.00/5 (2 votes)
See more:
My question is "How can I load my sqlite database in memory with C#?"
Posted

create a dataset and Select * from each table
 
Share this answer
 
public static SQLiteConnection mymdbConn = new SQLiteConnection("Data Source = :memory:");
public static String mFilename = "Q:\\cap\\Candle.db3";//Data Source=
public static void Mdb()
{
mymdbConn.Open();

if (mFilename != null)
{
Devart.Data.SQLite.SQLiteCommand cmd = new Devart.Data.SQLite.SQLiteCommand(@"ATTACH " + mFilename + " AS disk") { Connection = mymdbConn }; //, myQConn;

try
{//Note:verify query
cmd.ExecuteNonQuery();
}
catch (Exception e)
{
Debug.WriteLine(string.Format("exception{0}", e));
}
cmd = new SQLiteCommand("CREATE TABLE NIFTYNBNM AS SELECT * FROM disk.MAIN.NIFTYNBNM;", mymdbConn);


try
{//Note:verify query
cmd.ExecuteNonQuery();
}
catch (Exception e)
{
Debug.WriteLine(string.Format("exception{0}", e));
}
}
/*.SQLiteConnection.
_sqlConn = new SQLiteConnection("Data Source=:memory:");
_sqlConn.Open();
SQLiteCommand cmd = new SQLiteCommand(@"ATTACH " + sldbFilename + " AS disk", _sqlConn);
cmd.ExecuteNonQuery();
cmd = new SQLiteCommand("CREATE TABLE SampleInfo AS SELECT * FROM disk.SampleInfo;", _sqlConn);
cmd.ExecuteNonQuery();
cmd = new SQLiteCommand("CREATE TABLE Results AS SELECT * FROM disk.Results;", _sqlConn);
cmd.ExecuteNonQuery();
cmd = new SQLiteCommand("DETACH disk", _sqlConn);
cmd.ExecuteNonQuery();
*/
/* if (mymdbConn.State != 0)
{ //Console.WriteLine("true State:{0} ", myConn.State);
}
else
{ //Console.WriteLine("false State:{0} ", myConn.State);
mymdbConn.Open();
}
// String myIQ =String.Format("INSERT INTO MAIN.CDBM ([DT],[PIDS],[M])values({0},{1},{2})",":DT", 7119, 1 );
myIQ = String.Format("INSERT INTO [main].[NIFTYNB](Time,Rate,OI,Qty,PID,BQ,SQ,TBQty,TSQty,BQty,BRate,AQty,ARate,pRate,pQty,pTime,pBQty,pBRate,pAQty,pARate,pTBQty,pTSQty,pOI,CUMC,UMC) Values('{0:G}',{1:F},{2},{3},{4},{5},{6},{7},{8},{9},{10:F},{11},{12:F},{13:F},{14},'{15:G}',{16},{17:F},{18},{19:F},{20},{21},{22},'{23}','{24}')", ":DT", LP, toi, LQ, PIDS, BQ, SQ, ttbq, ttsq, tbq, tbp, toq, top, tpltp, tpltq, ":DT", tpbq, tpbp, tpoq, tpop, tptbq, tptsq, tpoi, parts[20], parts[21]); // pTime,pBQty,pBRate,pAQty,pARate,pTBQty,pTSQty,pOI,CUMC,UMC
// Debug.WriteLine(String.Format("{0}", myIQ));
sqminsert(myIQ, LTD);*/
}
public static void sqminsert(String myIQ, DateTime dt)
{//Note what can be done for date
// String myIQ =String.Format("INSERT INTO MAIN.CDBM ([DT],[PIDS],[M])values({0},{1},{2})",":DT", 7119, 1 );
SQLiteCommand sqCommand = new SQLiteCommand(myIQ) { Connection = mymdbConn };
sqCommand.Parameters.Add("DT", dt);


// SQLiteCommand sqCommand = new SQLiteCommand(myIQ) { Connection = mymdbConn };
if (mymdbConn.State != 0)
{ //Console.WriteLine("true State:{0} ", myConn.State);
}
else
{ //Console.WriteLine("false State:{0} ", myConn.State);
// mymdbConn.Open();
}
{
try
{//Note:verify query
sqCommand.ExecuteNonQuery();
// int r = Console.WriteLine(r + " rows affected.");
}
catch (Exception e)
{
Debug.WriteLine(string.Format("exception{0}", e));
}
}
{
// mymdbConn.Close();
}

}
public static void cmdb()
{
Devart.Data.SQLite.SQLiteCommand cmd = new SQLiteCommand("DETACH disk", mymdbConn);
cmd.ExecuteNonQuery();
}
 
Share this answer
 
Comments
Sandeep Mewara 6-Aug-10 11:54am    
Reason for my vote of 1
Why popping up 8 months old question?

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