本文整理汇总了C#中SQLiteConnection.BeginTransaction方法的典型用法代码示例。如果您正苦于以下问题:C# SQLiteConnection.BeginTransaction方法的具体用法?C# SQLiteConnection.BeginTransaction怎么用?C# SQLiteConnection.BeginTransaction使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类SQLiteConnection
的用法示例。
在下文中一共展示了SQLiteConnection.BeginTransaction方法的11个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: init
private void init(string connString)
{
connection = new SQLiteConnection(connString);
connection.Open();
//connection.ChangeDatabase(Server.MySQLDatabaseName);
transaction = connection.BeginTransaction();
}
示例2: Dispose
public void Dispose()
{
using (var conn = new SQLiteConnection(m_csb.ConnectionString))
{
conn.Open();
conn.Execute("create table Test (Id int primary key);");
using (var trans = conn.BeginTransaction())
{
conn.Execute("insert into Test(Id) values(1)", transaction: trans);
}
using (var cmd = new SQLiteCommand(@"select count(Id) from Test", conn))
Assert.AreEqual(0L, (long) cmd.ExecuteScalar());
}
}
示例3: PostDbParams
private void PostDbParams(string inName, SQLiteConnection updConn)
#endif
{
_dbParams["version"] = _dbParams["version.major"] + "." + _dbParams["version.minor"];
_dbParams["ot"] = inName.EndsWith(".ont") ? "1" : "0";
_dbParams["nt"] = "1";
_dbParams["strong"] = "0";
if (!_dbParams.ContainsKey("rtl"))
{
_dbParams["rtl"] = "0";
}
var detailTrx = updConn.BeginTransaction();
const string map = "Description=description,Abbreviation=short.title,Comments=about,Version=version,VersionDate=version.date,PublishDate=publish.date,RightToLeft=rtl,OT=ot,NT=nt,Strong=strong";
var mapPat = new Regex(@"([A-Za-z]+)=([\.a-z]+)");
foreach (Match match in mapPat.Matches(map))
{
var key = match.Groups[2].Value;
if (_dbParams.ContainsKey(key))
{
var detailCmd = updConn.CreateCommand();
var newValue = string.Format(@"""{0}""", _dbParams[key]);
newValue = ParseDateIfNecessary(key, newValue);
detailCmd.CommandText = string.Format("update Details SET {0} = {1}", match.Groups[1].Value, newValue);
int result = detailCmd.ExecuteNonQuery();
Debug.Assert(result == 1, match.Groups[0].Value);
}
}
detailTrx.Commit();
}
示例4: UpdateScripture
private void UpdateScripture(SQLiteConnection updConn, bool ot)
#endif
{
var books = _vrs.SelectNodes("//bk");
Debug.Assert(books != null);
for (var bkn = ot ? 0 : 39; bkn < 66; )
{
var bk = books[bkn];
bkn += 1;
var matches = _vrsPat.Matches(bk.InnerText);
for (var chn = 1; chn <= matches.Count; chn++)
{
var trx = updConn.BeginTransaction();
var vrsCount = int.Parse(matches[chn - 1].Groups[1].Value);
for (var vrsn = 1; vrsn <= vrsCount; vrsn++)
{
var line = _sr.ReadLine();
Debug.Assert(line != null);
var cmd = updConn.CreateCommand();
line = line.Replace("\"", "\"\"");
cmd.CommandText = string.Format(@"update Bible SET Scripture = ""{0}"" WHERE Book = {1} AND Chapter = {2} AND Verse = {3};", line, bkn, chn, vrsn);
int result = cmd.ExecuteNonQuery();
Debug.Assert(result == 1);
}
trx.Commit();
}
}
}
示例5: ThreadProc1
private void ThreadProc1(object state)
{
var barrier = (Barrier) state;
using (var conn = new SQLiteConnection(m_csb.ConnectionString))
{
conn.Open();
using (var trans = conn.BeginTransaction())
{
conn.Execute("select Id from Test", transaction: trans);
barrier.SignalAndWait();
conn.Execute("update Test set Id = 2;", transaction: trans);
barrier.SignalAndWait();
// give the other thread time to attempt begin the transaction, which will hang if both threads
// try to write concurrently
Thread.Sleep(TimeSpan.FromSeconds(2));
trans.Commit();
}
}
}
示例6: ThreadProc2
private void ThreadProc2(object state)
{
var barrier = (Barrier) state;
using (var conn = new SQLiteConnection(m_csb.ConnectionString))
{
barrier.SignalAndWait();
conn.Open();
barrier.SignalAndWait();
using (var trans = conn.BeginTransaction())
{
conn.Execute("select Id from Test", transaction: trans);
conn.Execute("update Test set Id = 3;", transaction: trans);
trans.Commit();
}
}
}
示例7: myEventLog_EntryWrittenDb
//: db change
void myEventLog_EntryWrittenDb(object sender, EntryWrittenEventArgs e)
{
int count = 0;
EventEntry ee = null;
if(e.Entry.Message.ToLower().Contains("registry"))
{
ee = new EventEntry(e.Entry);
}
else
{
// open or access attempt
if ((e.Entry.InstanceId == 560) || (e.Entry.InstanceId == 567))
{
int handle = GetHandle(e.Entry);
if (handle != 0)
{
EventLogEntry counterPart = CheckIfHandleExists(handle);
if (counterPart != null)
{
ee = CombineEvents(handle, e.Entry, counterPart);
}
else
{
if (e.Entry.InstanceId == 560 && !"".Equals(getFileName(e.Entry.Message)))
{
handleToEvent.Add(handle, e.Entry);
//make the action as blank as this is the place where handle was granted. no action has taken place.
handleToAction.Add(handle, "");
//increment count
handleToCount.TryGetValue(handle, out count);
count++;
handleToCount[handle] = count;
//TODO: removed for removing client user
//NEW addition for client side file access:
//since 567 is not logged we put 560 event caused by client in the tree
/*if (fromClient(e.Entry))
{
ee = new EventEntry(e.Entry);
}*/
}
}
}
}
}
//remove event from dictonary
if (e.Entry.InstanceId == 562)
{
count = 0;
int handle = GetHandle(e.Entry);
if (handle != 0)
{
try
{
if (handleToCount[handle] == 0)
{
handleToEvent.Remove(handle);
handleToCount.Remove(handle);
handleToAction.Remove(handle);
}
else
{
count--;
handleToCount[handle] = count;
}
}
catch (KeyNotFoundException)
{
}
}
}
if (ee != null)
{
//also check if the db table is present first time the app is run. If not we need to create it.
using (SQLiteConnection conn = new SQLiteConnection(@"Data Source=" + System.Environment.GetEnvironmentVariable("windir") + "\\system\\dpfam.db"))
{
conn.Open();
using (SQLiteCommand comm = new SQLiteCommand())
{
comm.Connection = conn;
//comm.CommandText = "CREATE TABLE IF NOT EXISTS FAM (time DATETIME, machine TEXT, filename TEXT, user TEXT, clientUser TEXT, action TEXT, dataindex INTEGER, category TEXT, result TEXT, msg TEXT, msgSrc TEXT, instanceId INTEGER, handleId INTEGER);";
try
{
//create db if not present
// comm.ExecuteNonQuery();
//Check if the event is to be inserted.
//insert the passed event in the db.
using (SQLiteTransaction tran = conn.BeginTransaction())
//TODO: using transactions dont help performance as only one entry is being inserted each time.
{
insertData(ee, comm);
tran.Commit();
}
//.........这里部分代码省略.........
示例8: stubInsertData
public void stubInsertData(EventEntry e)
{
using (SQLiteConnection conn = new SQLiteConnection(@"Data Source=" + System.Environment.GetEnvironmentVariable("windir") + "\\system\\dpfam.db"))
{
conn.Open();
using (SQLiteCommand comm = new SQLiteCommand())
{
comm.Connection = conn;
//comm.CommandText = "CREATE TABLE IF NOT EXISTS FAM (time DATETIME, machine TEXT, filename TEXT, user TEXT, clientUser TEXT, action TEXT, dataindex INTEGER, category TEXT, result TEXT, msg TEXT, msgSrc TEXT, instanceId INTEGER, handleId INTEGER);";
try
{
//create db if not present
// comm.ExecuteNonQuery();
//Check if the event is to be inserted.
//insert the passed event in the db.
using (SQLiteTransaction tran = conn.BeginTransaction())
//TODO: using transactions dont help performance as only one entry is being inserted each time.
{
insertData(e, comm);
tran.Commit();
}
}
catch (SQLiteException)
{
//MessageBox.Show("Insert Data Failed", "", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
}
}
conn.Close();
}
}
示例9: setRequestedDataType
/*
public void setRequestedDataType(byte dataType)
{
this.requestedDataType = dataType;
}
*/
public BinarySearchTree<EventEntry> startup()
{
if (runningAsServer == false)
return null;
//get the latest time date entry from the db.
DateTime latestDate = GetLatestEntryFromDb();
int count = 0;
EventLog myEventLog = new EventLog("Security", ".");
EventLogEntryCollection myLogEntryCollection = myEventLog.Entries;
events = new BinarySearchTree<EventEntry>(new CompareFile()); //new List<EventEntry>();
using (SQLiteConnection conn = new SQLiteConnection(@"Data Source=" + System.Environment.GetEnvironmentVariable("windir") + "\\system\\dpfam.db"))
{
conn.Open();
using (SQLiteCommand comm = new SQLiteCommand())
{
comm.Connection = conn;
//comm.CommandText = "CREATE TABLE IF NOT EXISTS FAM (time DATETIME, machine TEXT, filename TEXT, user TEXT, clientUser TEXT, action TEXT, dataindex INTEGER, category TEXT, result TEXT, msg TEXT, msgSrc TEXT, instanceId INTEGER, handleId INTEGER);";
try
{
//create db if not present
// comm.ExecuteNonQuery();
//Check if the event is to be inserted.
//insert the passed event in the db.
using (SQLiteTransaction tran = conn.BeginTransaction())
//TODO: using transactions dont help performance as only one entry is being inserted each time.
{
foreach (EventLogEntry entry in myLogEntryCollection)
{
//check if time of entry greater than the max time we got from db.
//if yes: consider this event to be shown. insert in db and update grid.
if (latestDate.CompareTo(entry.TimeWritten) < 0)
{
EventEntry ee = null;
if (entry.Message.ToLower().Contains("\\registry"))
{
ee = new EventEntry(entry);
ee.setAction("Set Key Value");
}
else if ((entry.InstanceId == 560) || (entry.InstanceId == 567))
{
int handle = GetHandle(entry);
if (handle != 0)
{
EventLogEntry counterPart = CheckIfHandleExists(handle);
// if handle exists combine the two events.
if (counterPart != null)
{
ee = CombineEvents(handle, entry, counterPart);
}
// else add handle to the handles list.
//(irrespective of whether a 560 or 567 event occurs 1st record it)
else
{
if (entry.InstanceId == 560 && !"".Equals(getFileName(entry.Message)))
{
handleToEvent.Add(handle, entry);
//make the action as blank as this is the place where handle was granted. no action has taken place.
handleToAction.Add(handle, "");
// increment count
handleToCount.TryGetValue(handle,out count);
count++;
//handleToCount[handle] = count;
handleToCount.Add(handle, count);
//TODO: removed for removing client user
//NEW addition for client side file access:
//since 567 is not logged we put 560 event caused by client in the tree
/*if (fromClient(entry))
{
ee = new EventEntry(entry);
}*/
}
}
}
}
//TODO: can see some 562 entries without their 560 in the log. Handle them by ignore them ?
// if it is a remove
else if (entry.InstanceId == 562)
{
count = 0;
int handle = GetHandle(entry);
if (handle != 0)
{
try
//.........这里部分代码省略.........
示例10: CancelExecuteReader
public void CancelExecuteReader(int milliseconds)
{
using (SQLiteConnection conn = new SQLiteConnection(m_csb.ConnectionString))
{
conn.Open();
conn.Execute(@"create table Test (Id integer primary key);");
using (var trans = conn.BeginTransaction())
{
for (long value = 0; value < 1000; value++)
conn.Execute(@"insert into Test(Id) values(@value)", new { value }, trans);
trans.Commit();
}
CancellationTokenSource source = new CancellationTokenSource(TimeSpan.FromMilliseconds(milliseconds));
using (var cmd = new SQLiteCommand(@"select a.Id, b.Id, c.Id, d.Id from Test a inner join Test b inner join Test c inner join Test d", conn))
using (var reader = cmd.ExecuteReaderAsync(source.Token).Result)
{
Task<bool> task;
do
{
task = reader.ReadAsync(source.Token);
Assert.IsTrue(task.IsCanceled || task.Result);
} while (!task.IsCanceled);
Assert.IsTrue(task.IsCanceled);
}
}
}
示例11: DapperTransaction
public void DapperTransaction()
{
using (SQLiteConnection conn = new SQLiteConnection(m_csb.ConnectionString))
{
conn.Open();
using (var trans = conn.BeginTransaction())
{
conn.Execute(@"create table Test (Id integer primary key, String text); insert into Test(Id, String) values(1, 'one'), (2, 'two'), (3, 'three');", transaction: trans);
trans.Commit();
}
var results = conn.Query<long>("select Id from Test where length(String) = @len", new { len = 3 }).ToList();
CollectionAssert.AreEqual(new long[] { 1, 2 }, results);
}
}