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,讨论单片机、嵌入式软硬件技术
*/