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"]
阅读(669) | 评论(0) | 转发(0) |