wince5.0 +SQlite 的基本操作
发布时间:2020-12-12 20:27:36 所属栏目:百科 来源:网络整理
导读:using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Text;using System.Windows.Forms;using System.IO;using System.Diagnostics;using System.Data.SQLite;using System.Da
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; using System.IO; using System.Diagnostics; using System.Data.SQLite; using System.Data.Common; using System.Data.SqlTypes; namespace sqltest { public partial class Form1 : Form { public String strConn; SQLiteConnection connection = new SQLiteConnection(); SQLiteCommand command = new SQLiteCommand(); public Form1() { InitializeComponent(); strConn = "test.db3"; File.Delete("test.db3"); try { SQLiteConnection.CreateFile("test.db3");//创建数据库 // SQLiteConnection connection = new SQLiteConnection("Data //Source=test.db3");//创建一个对test.db3的连接 connection.ConnectionString = "Data Source=" + strConn; } catch(Exception ex) { MessageBox.Show(ex.ToString()); } } private void button1_Click(object sender,EventArgs e) { if (button1.Text == "连接数据库") { try { connection.Open();//连接数据库(无法找到066.dll出错的解决方法:将066.dll//拷到wince中sqlite程序目录下) MessageBox.Show("数据库连接成功"); } catch (Exception ep) { MessageBox.Show(ep.ToString()); } button1.Text = "关闭数据库"; } else { try { connection.Close();//连接数据库(无法找到066.dll出错的解决方法:将 //066.dll拷到wince中sqlite程序目录下) MessageBox.Show("数据库断开连接"); } catch (Exception ep) { MessageBox.Show(ep.ToString()); } button1.Text = "连接数据库"; } } private void button2_Click(object sender,EventArgs e) { try { command.Connection = connection; command.CommandText = "CREATE TABLE [admin] ([ID] VARCHAR(50),[TEL] VARCHAR(50),[Password] VARCHAR(50));"; int x = command.ExecuteNonQuery(); } catch (Exception ex) { MessageBox.Show(ex.ToString()); } } private void button3_Click(object sender,EventArgs e) { SQLiteTransaction ta = connection.BeginTransaction(); try { for (int i = 0; i < 100; i++) { command.CommandText = "insert into admin(ID,TEL,Password) VALUES('3','4','5')"; command.ExecuteNonQuery(); } /* command.CommandText = "insert into admin (ID,Password) VALUES('333','444','555')"; int x = command.ExecuteNonQuery();*/ ta.Commit(); } catch (Exception ex) { ta.Rollback(); MessageBox.Show(ex.ToString()); } } private void button4_Click(object sender,EventArgs e) { string id,tel,pword = ""; textBox1.Text = ""; try { command.CommandText = "select * from admin"; // int x = command.ExecuteNonQuery(); SQLiteDataReader reader = command.ExecuteReader();//把与command的对应的reader对象转递给reader。 while (reader.Read()) { id = reader.GetValue(0).ToString(); tel = reader.GetValue(1).ToString(); pword = reader.GetValue(2).ToString(); textBox1.Text += id + "---" + tel + "---" + pword + "rn"; } reader.Close(); reader.Dispose(); } catch (Exception ex) { MessageBox.Show(ex.ToString()); } } } } (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |