SQLite.Net使用入门(三)【增删改查】

生命之长短殊不重要,只要你活得快乐,在有生之年做些有意义的事,便已足够。


实体类:

  public class clsModel
    {
        protected SQLiteConnection conn;

        public clsModel(SQLiteConnection @conn)
        {
            this.conn = @conn;

            if (this.conn != null && 
                this.conn.State.ToString() == "Closed")
            {
                conn.Open();
            }
        }
    }

   public class clsStudents : clsModel
    {
        public clsStudents(SQLiteConnection @conn) : base(@conn) { }

        public DataTable getAllStudents()
        {
            DataTable dt = new DataTable();
            SQLiteCommand command = conn.CreateCommand();
            command.CommandText = "SELECT * FROM Students";
            SQLiteDataAdapter db = new SQLiteDataAdapter(command);
            db.Fill(dt);
            return dt;
        }

        public DataTable getStudent(String @studentID)
        {
            DataTable dt = new DataTable();
            SQLiteCommand command = conn.CreateCommand();
            command.CommandText = "SELECT * FROM Students WHERE studentID = @studentID";
            //为了防止SQL注入,所有的用户输入将进行参数设置。
            command.Parameters.Add(new SQLiteParameter("@studentID", @studentID));
            SQLiteDataAdapter db = new SQLiteDataAdapter(command);
            db.Fill(dt);
            return dt;
        }

        public void insertStudent(String @firstName, String @lastName)
        {
            SQLiteCommand command = conn.CreateCommand();
            command.CommandText = "INSERT into Students (firstName,lastName) VALUES(@firstName , @lastName)";
            command.Parameters.Add(new SQLiteParameter("@firstName", @firstName));
            command.Parameters.Add(new SQLiteParameter("@lastName", @lastName));
            command.ExecuteNonQuery();
        }

        public void updateStudent(String @studentID, String @firstName, String @lastName)
        {
            SQLiteCommand command = conn.CreateCommand();
            command.CommandText = "UPDATE Students SET firstName = @firstName, lastName = @lastName WHERE studentID = @studentID";
            command.Parameters.Add(new SQLiteParameter("@studentID", @studentID));
            command.Parameters.Add(new SQLiteParameter("@firstName", @firstName));
            command.Parameters.Add(new SQLiteParameter("@lastName" , @lastName));
            command.ExecuteNonQuery();
        }

        public void deleteStudent(String @studentID)
        {
            SQLiteCommand command = conn.CreateCommand();
            command.CommandText = "DELETE FROM Students WHERE studentID = @studentID";
            command.Parameters.Add(new SQLiteParameter("@studentID", @studentID));
            command.ExecuteNonQuery();
        }

    }

Form1窗体后台代码:

ublic partial class Form1 : Form
    {
        SQLiteConnection conn;
        clsStudents students;

        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            conn = new SQLiteConnection(@"data source = ..\\..\\testdb.db");
            students = new clsStudents(conn);
            dataGridView1.DataSource = students.getAllStudents();
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            new Form2(students).ShowDialog();
            loadGridView();
        }

        private void btnEdit_Click(object sender, EventArgs e)
        {
            int index = dataGridView1.CurrentCell.RowIndex;
            if (index > -1)
            {
                String studentID = dataGridView1.Rows[index].Cells["studentID"].Value.ToString();
                new Form2(students, studentID).ShowDialog();
                loadGridView();
            }
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {

            int index = dataGridView1.CurrentCell.RowIndex;
            if (index > -1)
            {
                String studentID = dataGridView1.Rows[index].Cells["studentID"].Value.ToString();
                var results = MessageBox.Show("你确定要删除此记录?",
                                "删除学生", 
                                MessageBoxButtons.YesNo, 
                                MessageBoxIcon.Question);
                if (results == DialogResult.Yes)
                {
                    students.deleteStudent(studentID);
                    loadGridView();
                }
            }
        }

        private void loadGridView()
        {
            dataGridView1.DataSource = students.getAllStudents();
        }

        private void btnExit_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }

Form1窗体后台代码:

 public partial class Form2 : Form
    {
        clsStudents students;
        String studentID = "";

        //Add Mode
        public Form2(clsStudents @students)
        {
            init(@students);
            lblStudentID.Text = "新的学生实体";
        }

        //Edit Mode
        public Form2(clsStudents @students, String studentID)
        {
            init(@students);
            this.studentID = studentID;
            lblStudentID.Text = this.studentID;
            DataTable dt = students.getStudent(studentID);
            txtFirstName.Text = dt.Rows[0]["firstName"].ToString();
            txtLastName.Text  = dt.Rows[0]["lastName"].ToString();
        }

        private void init(clsStudents students)
        {
            InitializeComponent();
            this.students = students;
        }


        private void btnOK_Click(object sender, EventArgs e)
        {
            if (studentID == String.Empty)
            {
                //Add Mode
                students.insertStudent(txtFirstName.Text.Trim(), txtLastName.Text.Trim());
            }
            else
            {
                //Edit Mode
                students.updateStudent(this.studentID, txtFirstName.Text.Trim(), txtLastName.Text.Trim());
            }

            this.Close();
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }

运行结果如图:

这里写图片描述


添加

这里写图片描述


编辑

这里写图片描述


删除

这里写图片描述

  • 3
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
MFC(Microsoft Foundation Class)是一种用于开发Windows应用程序的C++类库。SQLite是一种轻量级的嵌入式数据库系统。 在MFC中使用SQLite进行增删改查操作需要以下步骤: 首先,需要下载并集成SQLite的组件库。可以在SQLite官方网站上找到最新的DLL文件,并将其添加到MFC项目中。 接下来,需要包含SQLite的头文件,并在代码中初始化SQLite库。通过调用sqlite3_open函数,可以打开或创建一个SQLite数据库文件,并获得一个数据库连接对象。 增加数据时,使用sqlite3_exec函数执行SQL语句,完成插入数据的操作。SQL语句可以使用INSERT INTO语句,将数据插入到相应的表中。 删除数据时,使用DELETE FROM语句,并结合WHERE子句来匹配要删除的数据条件。同样,也可以使用sqlite3_exec函数来执行这个SQL语句。 修改数据时,使用UPDATE语句,并结合WHERE子句来指定要修改的数据条件。同样,也可以使用sqlite3_exec函数来执行这个SQL语句。 查询数据时,使用SELECT语句,可以通过sqlite3_exec函数执行SQL语句,并通过回调函数获取查询结果。 对于查询操作,可以使用sqlite3_prepare_v2函数准备SQL语句,然后使用sqlite3_step函数逐行获取查询结果。 总之,使用MFC进行SQLite增删改查操作,首先需要集成SQLite组件库,然后通过SQL语句执行相应的操作,并通过适当的函数获取结果。通过结合MFC和SQLite,可以在Windows平台上轻松地操作SQLite数据库。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值