Exemple #1
0
        /// <summary>
        /// 初始化
        /// </summary>
        public void Read(string dbFile)
        {
            SQLiteConnectionStringBuilder sb = new SQLiteConnectionStringBuilder();

            sb.DataSource = dbFile;
            SqliteSingleConnection connection = new SqliteSingleConnection(sb);

            connection.Open();
            using (SQLiteCommand cmd = connection.CreateCommand())
            {
                try
                {
                    while (true)
                    {
                        //lock (_readWriteLocker)
                        {
                            cmd.CommandText = "Select * from MyTable";
                            using (SQLiteDataReader reader = cmd.ExecuteReader())
                            {
                                while (reader.Read())
                                {
                                    string name  = reader["name"].ToString();
                                    string score = reader["score"].ToString();
                                    Console.WriteLine($"name:{name}  Score:{score}");
                                }
                            }
                        }
                    }
                }
                catch (Exception ex)
                {
                    throw;
                }
            }
        }
Exemple #2
0
        public void Write(string dbFile)
        {
            SQLiteConnectionStringBuilder sb = new SQLiteConnectionStringBuilder();

            sb.DataSource = dbFile;
            //if (connection == null)
            {
                connection = new SqliteSingleConnection($"Data Source={dbFile};");
                connection.Open();
            }

            using (SQLiteCommand cmd = connection.CreateCommand())
            {
                try
                {
                    while (true)
                    {
                        // lock (_readWriteLocker)
                        {
                            cmd.CommandText = "Update MyTable set score = 0 where score = 4000;" +
                                              "Update MyTable set score = 4000 where score = 3000;" +
                                              "Update MyTable set score = 3000 where score = 0;";
                            cmd.ExecuteNonQuery();
                        }
                    }
                }
                catch (Exception ex)
                {
                    throw;
                }
            }
        }
Exemple #3
0
        public void Initialize(string dbFile)
        {
            IsInitialized = false;
            SqliteSingleConnection.CreateFile(dbFile);

            SQLiteConnectionStringBuilder sb = new SQLiteConnectionStringBuilder();

            sb.DataSource = dbFile;
            using (SqliteSingleConnection connection = new SqliteSingleConnection(sb))
            {
                connection.Open();
                using (SQLiteCommand cmd = connection.CreateCommand())
                {
                    cmd.CommandText = "Create Table MyTable(name varchar(20), score int)";
                    cmd.ExecuteNonQuery();
                    cmd.CommandText = "insert into MyTable (name, score) values ('Me', 3000)";
                    cmd.ExecuteNonQuery();
                    cmd.CommandText = "insert into MyTable (name, score) values ('You', 4000)";
                    cmd.ExecuteNonQuery();
                }
            }

            IsInitialized = true;
        }