C#通过Ado.NET操作SQLite数据库

发布时间:2018-12-17  栏目:sqlite  评论:0 Comments

要缅怀以ASP.NET项目遭到接纳SQLite数据库,先要下载一个ADO.NET 2.0 SQLite Data
Provider,下载地址也:http://sourceforge.net/project/showfiles.php?group_id=132486&package_id=145568,下载后装置收尾后,该安装程序自动在当网注册(即可在”添加引用”中看出所设置的Provider).    

manbet手机客户端3.0 1

     manbet手机客户端3.0 2

数呈现.jpg

     然后,在列蒙补充加上图所挑选即可.

记忆引入程序集System.Data.SQLite

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
//命名空间
using System.Data.SQLite;

namespace FuHello
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            List<ManagerInfo> list = new List<ManagerInfo>();
            //1创建SQLite数据库连接字符串
            string conStr = @"data source=D:\项目工程文件夹\尚云网络项目开发\尚云网络\SQLiteData\Sudons.db;version=3";
            //2创建SQLite数据库连接对象
            using (SQLiteConnection con = new SQLiteConnection(conStr))
            {
                //3打开数据库
                con.Open();
                //4构建SQL语句
                string cmdStr = "select * from ManagerInfo";
                using (SQLiteCommand cmd = new SQLiteCommand(cmdStr,con))
                {
                    //5执行SQL命令
                    SQLiteDataReader reader = cmd.ExecuteReader();
                    if (reader.HasRows)
                    {
                        //6读取数据
                        while (reader.Read())
                        {
                            list.Add(new ManagerInfo()
                            {
                                Mid = Convert.ToInt32(reader["MId"]),
                                Mname = reader["MName"].ToString(),
                                Mpwd = reader["MPwd"].ToString(),
                                Mtype = Convert.ToInt32(reader["MType"])
                            });
                        }//end while
                    }//end if
                }//end using cmd
            }//end using con

            //7给DataGridView绑定数据源
            this.dataGridViewManagerInfo.DataSource = list;
        }
    }
}

    
aspx页面才包含一论钮btnTest,在页面aspx.cs页面中,引入命名空间,贴入以下类似代码即可.    

复制代码 代码如下:

using System;
using System.Data;
manbet手机客户端3.0,using System.Web.UI.WebControls;
using System.Data.SQLite;

public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void btnTest_Click(object sender, EventArgs e)
{
SQLiteConnection.ClearAllPools();
SQLiteConnection.CreateFile(Server.MapPath(“~”) + “/UserData.dbx”);
SQLiteConnection conn = new SQLiteConnection(“Data Source=” +
Server.MapPath(“~” + “/UserData.dbx”));
conn.Open();
Response.Write(“打开数据库成功<br />”);
SQLiteCommand cmd = new SQLiteCommand();
cmd.CommandText = “create table Users (UserID int primary key,UserName
varchar(100) not null,UserPassword varchar(100) not null)”;
cmd.Connection = conn;
cmd.ExecuteNonQuery();
for (int i = 0; i < 100;i++ )
{
cmd.CommandText = “insert into Users (UserID,UserName,UserPassword)
values (” + i + “,’TestUser_” + i + “‘,'” +
DateTime.Now.ToString().Replace(” “, “-“).Replace(“:”, “-“) + “‘)”;
cmd.ExecuteNonQuery();
}
Response.Write(“插入成功
<br />”);
cmd.CommandText = “select Username from Users where UserID=1”;
cmd.Connection = conn;
string tempUserName = cmd.ExecuteScalar().ToString();
Response.Write(“单个值查询结果吗:” + tempUserName + “<br /><br
/>”);

cmd.CommandText = “select * from Users “;
cmd.Connection = conn;
SQLiteDataReader sdrInfo = cmd.ExecuteReader();
if (sdrInfo!= null)
{
int userID = 0;
string userName = string.Empty;
string userPassword = string.Empty;
while(sdrInfo.Read())
{
userID = Convert.ToInt32(sdrInfo[“UserID”]);
userName = sdrInfo[“UserName”].ToString();
userPassword = sdrInfo[“UserPassword”].ToString();
Response.Write(“UserID:”+userID+”<br />”);
Response.Write(“UserName:” + userName+ “<br />”);
Response.Write(“UserPassword:” + userPassword + “<br />”);
Response.Write(“<br />”);
}
sdrInfo.Close();
sdrInfo.Dispose();
}
cmd.CommandText = “update Users set UserPassword=’linxiang'”;
cmd.Connection = conn;
cmd.ExecuteNonQuery();
Response.Write(“更新数据库中之数据成功.”);
Response.Write(“以下结果为查询从数据库被经编排过后底数码项<br
/><br />”);
cmd.CommandText = “select * from Users “;
cmd.Connection = conn;
sdrInfo = cmd.ExecuteReader();
if (sdrInfo != null)
{
int userID = 0;
string userName = string.Empty;
string userPassword = string.Empty;
while (sdrInfo.Read())
{
userID = Convert.ToInt32(sdrInfo[“UserID”]);
userName = sdrInfo[“UserName”].ToString();
userPassword = sdrInfo[“UserPassword”].ToString();
Response.Write(“UserID:” + userID + “<br />”);
Response.Write(“UserName:” + userName + “<br />”);
Response.Write(“UserPassword:” + userPassword + “<br />”);
Response.Write(“<br />”);
}
sdrInfo.Close();
sdrInfo.Dispose();
}
conn.Clone();
conn.Dispose();
}
}

乃或许感兴趣之稿子:

留下评论

网站地图xml地图