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

你可能感兴趣的:(sql,数据库,String,sqlite,cmd,asp.net)