Asp.net使用Sqlite数据库

 1. 从http://sqlite.phxsoftware.com 下载System.Data.SQLite.dll

 2. 从http://www.sqlite.org/download.html 下载 sqlite3.dll

 3. Copy 以上2个Dll到Asp.net网站的bin目录下,类似  E:/2009work/MyWeb/Bin

 4. 用sqlitespy 工具新建数据库,E:/2009work/MyWeb/App_Data/db.db3 ,并且新建表CREATE TABLE User(name char(50), Pwd char(20));,插入几条测试数据

5. 编码:

(1)添加引用:E:/2009work/MyWeb/Bin/System.Data.SQLite.dll

(2)using System.Data.SQLite;

 

(3)

 

        private string _RootPath;
     /// <summary>
     /// 系统的根目录
     /// </summary>
     public string RootPath
     {
         get
         {

             _RootPath = HttpContext.Current.Server.MapPath(HttpContext.Current.Request.ApplicationPath). ToLower();//当前的绝对路径          
             if (_RootPath.Length == 1)
             {
                 _RootPath = "";
             }
             return _RootPath;
         }
     }

 


    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsCallback)
        {
                        
            
            string _connectionString = string.Format("Data Source={0}//App_Data//db.db3", RootPath);
            string _sql;

            SQLiteConnection conn = new SQLiteConnection();
            SQLiteCommand cmd;
            SQLiteDataReader dr;
            conn.ConnectionString = _connectionString;
            conn.Open();

            _sql = "select * from User";
            cmd = new SQLiteCommand(_sql, conn);
            dr = cmd.ExecuteReader();
            while (dr.Read())
            {
                string strToPrint;

                strToPrint = string.Format("{0}||{1}<br/>", dr[0].ToString(), dr[1].ToString());

                this.Response.Write(strToPrint);
            }
           
        }
    }

5.测试结果:

ken||123456
jane||123456
robbie||123456
ken||123456

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值