Chinaunix首页 | 论坛 | 博客
  • 博客访问: 30995402
  • 博文数量: 230
  • 博客积分: 2868
  • 博客等级: 少校
  • 技术积分: 2223
  • 用 户 组: 普通用户
  • 注册时间: 2009-10-08 21:48
个人简介

Live & Learn

文章分类

全部博文(230)

文章存档

2022年(2)

2019年(5)

2018年(15)

2017年(42)

2016年(24)

2015年(13)

2014年(1)

2012年(5)

2011年(58)

2010年(56)

2009年(9)

我的朋友

分类: C#/.net

2017-08-07 09:28:41

1  下载SQLITE  .NET
2  COPE SQLITE.data.dll 到可执行程序目录下,
3 添加引用,copy local属性为true;
4 编写代码:
 

点击(此处)折叠或打开

  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Windows.Forms;
  9. using System.Data;
  10. using System.Data.SQLite;

  11. namespace MAC_AND_DEVICEID
  12. {

  13.     public partial class Form1 : Form
  14.     {
  15.         public Form1()
  16.         {
  17.             InitializeComponent();
  18.         }

  19.         private void label1_Click(object sender, EventArgs e)
  20.         {

  21.         }

  22.         private void buttonCreate_Click(object sender, EventArgs e)
  23.         {
  24.             SQLiteConnection.ClearAllPools();
  25.             SQLiteConnection.CreateFile("UserData.db");
  26.             SQLiteConnection sqlcon = new SQLiteConnection("Data Source=UserData.db");
  27.             sqlcon.Open();
  28.             SQLiteCommand cmd = new SQLiteCommand();
  29.             cmd.Connection = sqlcon;
  30.             //-------
  31.             cmd.CommandText = "create table Users (UserID int primary key,UserName varchar(100) not null,UserPassword varchar(100) not null)";
  32.             
  33.             cmd.ExecuteNonQuery();
  34.             //--------
  35.             for (int i = 0; i < 100; i++)
  36.             {
  37.                 cmd.CommandText = "insert into Users (UserID,UserName,UserPassword) values (" + i + ",'TestUser_" + i + "','" + DateTime.Now.ToString().Replace(" ", "-").Replace(":", "-") + "')";
  38.                 cmd.ExecuteNonQuery();
  39.             }
  40.             MessageBox.Show("Create Table and insert data");
  41.             //--------
  42.             cmd.CommandText = "select Username from Users where UserID=1";
  43.             cmd.Connection = sqlcon;
  44.             string tempUserName = tempUserName = cmd.ExecuteScalar().ToString();
  45.             textBoxGUID.Text = tempUserName;
  46.             MessageBox.Show("Get User name:{0}", tempUserName);
  47.             //Message("tempUserName={0}", tempUserName);

  48.             //
  49.             cmd.CommandText = "select * from Users ";
  50.             cmd.Connection = sqlcon;
  51.             SQLiteDataReader sdrinfo = cmd.ExecuteReader();
  52.             if (sdrinfo != null)
  53.             {
  54.                 int userid = 0;
  55.                 string username = string.Empty;
  56.                 string userPassword = string.Empty;
  57.                 while (sdrinfo.Read())
  58.                 {
  59.                     userid = Convert.ToInt32(sdrinfo["UserID"]);
  60.                     username = sdrinfo["UserName"].ToString();
  61.                     userPassword = sdrinfo["UserPassWord"].ToString();
  62.                 }
  63.             }
  64.             sdrinfo.Close();
  65.             sdrinfo.Dispose();


  66.             cmd.CommandText = "update Users set UserPassword='linxiang'";
  67.             cmd.Connection = sqlcon;
  68.             cmd.ExecuteNonQuery();
  69.             

  70.             //
  71.             sqlcon.Close();
  72.             sqlcon.Dispose();

  73.             textBoxMAC.Text = "12-34-56-78-96";
  74.         }
  75.     }
  76. }
 编译成功,然后执行会报错:

混合模式程序集是针对“v2.0.50727”版的运行时生成的,在没有配置其他信息的情况下,无法在 4.0 运行时中加载该程序集。

 在项目中增加app.config:

 
   
   
 




 如果没有用,设置项目属性的设置项,增加字符串_connectionString 内容 ConfigurationManager.AppSettings["ConnectionString"]

阅读(872) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~