• C# SQLite操作示列


    1.前台代码

    <Window x:Class="WpfApplication1.MainWindow"
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            Title="MainWindow" Height="350" Width="525">
        <Grid>
            <Button Content="Button" Height="30" HorizontalAlignment="Left" Margin="52,50,0,0" Name="button1" VerticalAlignment="Top" Width="136" Click="button1_Click" />
            <Button Content="Button" Height="30" HorizontalAlignment="Left" Margin="272,50,0,0" Name="button2" VerticalAlignment="Top" Width="120" Click="button2_Click" />
        </Grid>
    </Window>

    2.后台代码

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Windows;
    using System.Windows.Controls;
    using System.Windows.Data;
    using System.Windows.Documents;
    using System.Windows.Input;
    using System.Windows.Media;
    using System.Windows.Media.Imaging;
    using System.Windows.Navigation;
    using System.Windows.Shapes;
    using System.Data.SQLite;
    using Microsoft.Office.Tools.Excel;
    using System.Data;

    namespace WpfApplication1
    {
        /// <summary>
        /// Interaction logic for MainWindow.xaml
        /// </summary>
        public partial class MainWindow : Window
        {
            private string dataSource = "ContactBookDB.sqlite";
            public MainWindow()
            {
                InitializeComponent();
            }

            private void button1_Click(object sender, RoutedEventArgs e)
            {
                SQLiteConnection.CreateFile(dataSource);
                button1.Content = "DatabaseCreated!";
            }

            private void button2_Click(object sender, RoutedEventArgs e)
            {

                SQLiteConnection dbConn = new SQLiteConnection("Data Source=" + dataSource);

                dbConn.Open();
                SQLiteCommand dbCmd = dbConn.CreateCommand();

                dbCmd.CommandText = "CREATE TABLE user(id Integer PRIMARY KEY AUTOINCREMENT,username varchar(40));";
                dbCmd.ExecuteNonQuery();

                dbCmd.CommandText = "INSERT INTO user VALUES(null,'部分火电设备几年级烦恼就开始的呢监控');";
                dbCmd.ExecuteNonQuery();

                dbCmd.CommandText = "INSERT INTO user VALUES(null,'xxxx');";
                dbCmd.ExecuteNonQuery();

                dbCmd.CommandText = "INSERT INTO user VALUES(null,'3333');";
                dbCmd.ExecuteNonQuery();

                dbCmd.CommandText = "SELECT * FROM user;";
                int len = Convert.ToInt32(dbCmd.ExecuteScalar()); //返回0表示没有数据,返回1表示有数据

                SQLiteDataReader dataReader = dbCmd.ExecuteReader();
                /*
                DataTable dataTable = new DataTable();
                if (dataReader.HasRows)
                {
                    dataTable.Load(dataReader);
                }  */
                while (dataReader.Read())
                {//读合法

                    string a = dataReader["username"].ToString();
                    int b = int.Parse(dataReader["id"].ToString());
                }
                dataReader.Close();

                dbConn.Close();

                button2.Content = "TableCreated!";
            }

        }
    }

    /*
    *    电子技术交流群:142282597,讨论单片机、嵌入式软硬件技术
    */

    单片机,嵌入式LINUX技术交流群:142282597
  • 相关阅读:
    【交换】
    【数字对】
    【改造二叉树】
    【Begin】
    100以内所有质数的输出
    位运算符、|和||、&和&&的区别
    linux中vim编辑器三种模式及常用命令的使用
    静态代码块、构造代码块和构造函数的执行顺序
    字符乱码出现的原因及解决办法
    Springcloud-微服务
  • 原文地址:https://www.cnblogs.com/qiujiahong/p/3118912.html
Copyright © 2020-2023  润新知