C#操做sqlite

用NuGet包管理器,能够很方便安装sqlite到项目中,而后引用python

using System.Data.SQLite;sql

第一步用SQLiteConnection创建链接。数据库

第二步用SQLiteCommand操做数据库,增删改等。code

第三步用SQLiteDataReader,查询数据库。sqlite

完整代码以下rem

using (SQLiteConnection conn = new SQLiteConnection())
            {
                string dbPath = "Data Source=" + Environment.CurrentDirectory + @"\Actress.db";
                conn.ConnectionString = dbPath;                
                conn.Open();
                using (SQLiteCommand cmd = new SQLiteCommand(conn))
                {
                    cmd.CommandText = @"create table if not exists Actress
                                                    (ID integer primary key autoincrement,
                                                     Name text not null,
                                                     Age integer not null)";
                    cmd.ExecuteNonQuery();
                    cmd.CommandText = @"insert into Actress values
                                                            (null,'王菲',47),
                                                            (null,'范冰冰',37),

                                                            (null,'柳岩',36)";
                    cmd.ExecuteNonQuery();
                    cmd.CommandText = @"select * from Actress";
                    using (SQLiteDataReader reader = cmd.ExecuteReader())
                    {
                        if (reader.HasRows)
                        {
                            while(reader.Read())
                            {
                                Console.WriteLine("ID: " + reader.GetInt64(0));
                                Console.WriteLine("Name " + reader.GetString(1));
                                Console.WriteLine("Age " + reader.GetInt32(2));
                            }
                        }
                    }
                }
                
            }
            Console.ReadKey();

操做上彷佛比pythoncmd