1 下载SQLITE .NET
2 COPE SQLITE.data.dll 到可执行程序目录下,
3 添加引用,copy local属性为true;
4 编写代码:
-
using System;
-
using System.Collections.Generic;
-
using System.ComponentModel;
-
using System.Data;
-
using System.Drawing;
-
using System.Linq;
-
using System.Text;
-
using System.Windows.Forms;
-
using System.Data;
-
using System.Data.SQLite;
-
-
namespace MAC_AND_DEVICEID
-
{
-
-
public partial class Form1 : Form
-
{
-
public Form1()
-
{
-
InitializeComponent();
-
}
-
-
private void label1_Click(object sender, EventArgs e)
-
{
-
-
}
-
-
private void buttonCreate_Click(object sender, EventArgs e)
-
{
-
SQLiteConnection.ClearAllPools();
-
SQLiteConnection.CreateFile("UserData.db");
-
SQLiteConnection sqlcon = new SQLiteConnection("Data Source=UserData.db");
-
sqlcon.Open();
-
SQLiteCommand cmd = new SQLiteCommand();
-
cmd.Connection = sqlcon;
-
//-------
-
cmd.CommandText = "create table Users (UserID int primary key,UserName varchar(100) not null,UserPassword varchar(100) not null)";
-
-
cmd.ExecuteNonQuery();
-
//--------
-
for (int i = 0; i < 100; i++)
-
{
-
cmd.CommandText = "insert into Users (UserID,UserName,UserPassword) values (" + i + ",'TestUser_" + i + "','" + DateTime.Now.ToString().Replace(" ", "-").Replace(":", "-") + "')";
-
cmd.ExecuteNonQuery();
-
}
-
MessageBox.Show("Create Table and insert data");
-
//--------
-
cmd.CommandText = "select Username from Users where UserID=1";
-
cmd.Connection = sqlcon;
-
string tempUserName = tempUserName = cmd.ExecuteScalar().ToString();
-
textBoxGUID.Text = tempUserName;
-
MessageBox.Show("Get User name:{0}", tempUserName);
-
//Message("tempUserName={0}", tempUserName);
-
-
//
-
cmd.CommandText = "select * from Users ";
-
cmd.Connection = sqlcon;
-
SQLiteDataReader sdrinfo = cmd.ExecuteReader();
-
if (sdrinfo != null)
-
{
-
int userid = 0;
-
string username = string.Empty;
-
string userPassword = string.Empty;
-
while (sdrinfo.Read())
-
{
-
userid = Convert.ToInt32(sdrinfo["UserID"]);
-
username = sdrinfo["UserName"].ToString();
-
userPassword = sdrinfo["UserPassWord"].ToString();
-
}
-
}
-
sdrinfo.Close();
-
sdrinfo.Dispose();
-
-
-
cmd.CommandText = "update Users set UserPassword='linxiang'";
-
cmd.Connection = sqlcon;
-
cmd.ExecuteNonQuery();
-
-
-
//
-
sqlcon.Close();
-
sqlcon.Dispose();
-
-
textBoxMAC.Text = "12-34-56-78-96";
-
}
-
}
-
}
编译成功,然后执行会报错:
在项目中增加app.config:
如果没有用,设置项目属性的设置项,增加字符串_connectionString 内容 ConfigurationManager.AppSettings["ConnectionString"]
阅读(872) | 评论(0) | 转发(0) |