SQLite_test

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;
using System.IO;

namespace SQLite_test
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            //Program();
        }

        //数据库连接
        SQLiteConnection m_dbConnection;
        //string FilePath;

        //作废
        public void Program()
        {
            createNewDatabase();
            connectToDatabase();
            createTable();
            fillTable();
            printHighscores();
        }

        //创建一个空的数据库
        void createNewDatabase()
        {
            string FilePath = Application.StartupPath + "\\" + txb_souce.Text + ".sqlite";
            if (!File.Exists(FilePath))
            {
                SQLiteConnection.CreateFile(FilePath);
            }
            else
            {
                MessageBox.Show("数据库已存在");
            }

            //SQLiteConnection.CreateFile("MyDatabase.sqlite");
        }

        //创建一个连接到指定数据库
        void connectToDatabase()
        {
            string FilePath = Application.StartupPath + "\\" + txb_souce.Text + ".sqlite";

            try
            {
                m_dbConnection = new SQLiteConnection("Data Source=" + FilePath + ";Version=3;");
                m_dbConnection.Open();
                lab_status.Text = "连接成功";
            }
            catch (Exception ex)
            {
                //MessageBox.Show(ex.Message);
                throw new Exception("打开数据库:" + FilePath + "的连接失败:" + ex.Message);
            }
            //m_dbConnection = new SQLiteConnection("Data Source=MyDatabase.sqlite;Version=3;");
            //m_dbConnection.Open();

        }

        //在指定数据库中创建一个table
        void createTable()
        {

            string sql = "create table " + txb_lable.Text + " (name varchar(20), score int)";
            //string sql = "create table highscores (name varchar(20), score int)";
            SQLiteCommand command = new SQLiteCommand(sql, m_dbConnection);
            command.ExecuteNonQuery();

        }

        //插入一些数据
        void fillTable()
        {
            string name = txb_tag1.Text;
            int score = Convert.ToInt32(txb_tag2.Text);
            string sql = "insert into highscores (name, score) values ('" + name + "'," + score + ")";  //highscores为数据表名称
            SQLiteCommand command = new SQLiteCommand(sql, m_dbConnection);  //同creattable中一样,可以共用
            command.ExecuteNonQuery();  //每个命令需要触发一次
            lab_status.Text = "插入成功";
        }

        //使用sql查询语句,并显示结果
        void printHighscores()
        {
            txb_result.Clear();
            string sql = "select * from highscores order by score desc";  //desc降序,*不限,即读取所有项按score降序排列
            SQLiteCommand command = new SQLiteCommand(sql, m_dbConnection);
            SQLiteDataReader reader = command.ExecuteReader();
            while (reader.Read())
            {
                Console.WriteLine("Name: " + reader["name"] + "\tScore: " + reader["score"]);
                txb_result.Text = "Name: " + reader["name"] + "\tScore: " + reader["score"] + "\r\n" + txb_result.Text;
            }
            /*
            SQLiteCommand command = new SQLiteCommand(m_dbConnection); //重载2,单参数,只有连接对象
            command.CommandText = "select name from highscores where score =123";  //另一种写法
            var name = command.ExecuteScalar();
            Console.WriteLine(Name.ToString());
            */
        }

        private void btn_con_Click(object sender, EventArgs e)
        {
            connectToDatabase();
        }

        private void btn_dis_Click(object sender, EventArgs e)
        {
            printHighscores();
        }

        private void btn_lab_Click(object sender, EventArgs e)
        {
            createTable();
        }

        private void btn_newdb_Click(object sender, EventArgs e)
        {
            createNewDatabase();
        }

        private void btn_write_Click(object sender, EventArgs e)
        {
            fillTable();
        }

        //删除数据
        private void btn_delete_Click(object sender, EventArgs e)
        {
            try
            {
                string sql = "delete from " + txb_lable.Text + " where name='" + txb_tag1.Text + "'";
                SQLiteCommand command = new SQLiteCommand(sql, m_dbConnection);
                command.ExecuteNonQuery();
                lab_status.Text = "删除成功";
            }
            catch (Exception ex)
            {
                throw new Exception("删除失败" + ex.Message);
            }
        }

        //查询数据表名
        private void btn_dis_table_Click(object sender, EventArgs e)
        {
            // 获取数据库中的所有表名
            string sqlTableNames = "select name from sqlite_master where type='table' order by name;";
            // 创建命令对象
            SQLiteCommand cmd = new SQLiteCommand(sqlTableNames, m_dbConnection);
            using (SQLiteDataReader dr = cmd.ExecuteReader())
            {
                while (dr.Read())
                {
                    // 表名
                    txb_result.Text = dr["Name"] +"\r\n";
                }
            }
        }

        //修改数据
        private void btn_modify_Click(object sender, EventArgs e)
        {
            try
            {
                string sql = "update " + txb_lable.Text + " set score = " + Convert.ToInt32(txb_tag2.Text) + " where name='" + txb_tag1.Text + "'";
                SQLiteCommand command = new SQLiteCommand(sql, m_dbConnection);
                command.ExecuteNonQuery();
            }
            catch (Exception ex)
            {
                throw new Exception("更新数据失败" + ex.Message);
            }
        }
    }
}

 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值