发布网友 发布时间:2022-05-01 03:49
共2个回答
懂视网 时间:2022-05-01 08:11
bool Insert(string topic, string key, string value, out int id) { DbProviderFactory factory = SQLiteFactory.Instance; using (DbConnection conn = factory.CreateConnection()) { conn.ConnectionString = _connectionString; conn.Open(); DbCommand cmdInsert = conn.CreateCommand(); cmdInsert.Parameters.Add(cmdInsert.CreateParameter()); cmdInsert.Parameters.Add(cmdInsert.CreateParameter()); cmdInsert.Parameters.Add(cmdInsert.CreateParameter()); DbTransaction trans = conn.BeginTransaction(); try { cmdInsert.CommandText = "INSERT INTO [{0}] ([Topic],[Key],[Value]) VALUES (?,?,?);SELECT LAST_INSERT_ROWID() FROM [{0}]"; cmdInsert.CommandText = string.Format(cmdInsert.CommandText, _messageTableName); cmdInsert.Parameters[0].Value = topic; cmdInsert.Parameters[1].Value = key; cmdInsert.Parameters[2].Value = value; DbDataReader reader = cmdInsert.ExecuteReader(); trans.Commit(); if (reader.Read()) { id = int.Parse(reader[0].ToString()); reader.Close(); return true; } else { SAEC_Log4net.Log.Error("insert message to db fail"); id = 0; return false; } } catch (Exception e) { trans.Rollback(); SAEC_Log4net.Log.Error(e.ToString()); id = 0; return false; } } }
SQLite数据库中获取新插入数据的自增长ID
标签:
热心网友 时间:2022-05-01 05:19
图形工具中应该有这个选项的。 把你要自动serial mysql,sqlite:autoincrement 自动编号要(id autoincrement(1,1),[学号] integer DEFAULT 0