我是使用c#和sqlite的新手。
为什么这段代码不起作用? 我尝试了一点点不同。但没有成功。
private void button1_Click(object sender, EventArgs e)
{
using (SQLiteConnection con = new SQLiteConnection(connectionString))
{
con.Open();
try
{
SQLiteCommand cmd = new SQLiteCommand();
const string createTableQuery = @"CREATE TABLE User userName NOT NULL varchar(40), passWord NOT NULL varchar(40))";
cmd.CommandText = createTableQuery;
cmd.ExecuteNonQuery();
int i = cmd.ExecuteNonQuery();
if (i == 1)
{
MessageBox.Show("Table created");
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
}
答案 0 :(得分:2)
你应该执行命令
private void button1_Click(object sender, EventArgs e)
{
using (SQLiteConnection con = new SQLiteConnection(connectionString))
{
try
{
string createTableQuery = @"CREATE TABLE User (userName NOTNULL varchar(40), passWord NOTNULL varchar(40))";
SQLiteCommand cmd = new SQLiteCommand();
cmd.CommandText = createTableQuery;
cmd.ExecuteNonQuery();//Here
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
}
答案 1 :(得分:2)
您的查询错误:
string createTableQuery = @"CREATE TABLE User (userName NOTNULL
varchar(40), passWord NOTNULL varchar(40))";
它应该是NOT NULL
而不是NOTNULL
:
string createTableQuery = @"CREATE TABLE User (userName NOT NULL
varchar(40), passWord NOT NULL varchar(40))";
然后致电:
cmd.ExecuteNonQuery();
后:
cmd.CommandText = createTableQuery;
答案 2 :(得分:0)
执行你的命令:
try
{
string createTableQuery = @"CREATE TABLE User (userName NOTNULL varchar(40), passWord NOTNULL varchar(40))";
SQLiteCommand cmd = new SQLiteCommand();
cmd.CommandText = createTableQuery;
cmd.ExecuteNonQuery();
}