C#操作sqlite

用NuGet包管理器,可以很方便安装sqlite到项目中,然后引用

using System.Data.SQLite;

第一步用SQLiteConnection建立连接。

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

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

完整代码如下

 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();

操作上似乎比python

转载于:https://my.oschina.net/u/3243928/blog/900995

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值