wince5.0 +SQlite 的基本操作





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,TEL,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 + "\r\n";
                }
                reader.Close();
                reader.Dispose();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
        }
    }
}




  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值