0

我刚开始构建一个 Windows 窗体应用程序。登录后,执行以下代码。但问题是,每次用户登录表时都会被覆盖。怎样才能做到数据库表只创建一次呢?

   SQLiteConnection sqlite_conn;
   SQLiteCommand sqlite_cmd;
   SQLiteDataReader sqlite_datareader;

   // create a new database connection:
   sqlite_conn = new SQLiteConnection("Data Source=database.db;Version=3;New=True;Compress=True;");

   sqlite_conn.Open(); 

   sqlite_cmd = sqlite_conn.CreateCommand(); 

   sqlite_cmd.CommandText = "CREATE TABLE test (id integer primary key, text varchar(100));";

   sqlite_cmd.ExecuteNonQuery();

或者有更好的方法吗?

4

3 回答 3

1

你需要像这样改变你的代码

  sqlite_cmd.CommandText = "create table if not exists test (id integer primary key, text varchar(100));";

仅当该表尚不存在时才会创建该表。

于 2013-07-16T15:13:34.460 回答
0

首先检查表是否存在如果表不存在则创建表

IF OBJECT_ID('test', 'U') IS NULL 
BEGIN 
   CREATE TABLE test (id integer primary key, text varchar(100));
END

对于 SQL 精简版

create table if not exists test (id integer primary key, text varchar(100))
于 2013-07-16T15:01:03.653 回答
0

You are on wrong way. I don't really understand your design, but here is a walkthrough, how to create a winform app, which uses SqlLite: C# Tutorial 36: How to use and connect Sqlite in a C# project

Try to modify your approach. Don't create table everytime in your code, as David mentioned.

于 2013-07-16T15:08:36.010 回答