我有一个名为 ID 的文本框和一个保存按钮。单击保存按钮后,我想简单地将我在文本框中写入的任何内容存储在 SQLite 数据库文件中。
代码正在编译,程序运行没有任何错误,但我没有看到我在同一应用程序下创建的数据库文件 (file1.db) 中保存的任何记录。
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
private SQLiteConnection sqlcon;
private SQLiteCommand sqlcmd;
private SQLiteDataAdapter dataBase;
private DataSet dataSet = new DataSet();
private DataTable dataTable = new DataTable();
public Form1()
{
InitializeComponent();
}
private void createDataBase()
{
dataBase = new SQLiteDataAdapter();
}
private void ExecuteQuery(string txtQuery)
{
using (SQLiteConnection sqlcon = new SQLiteConnection("Data Source=file1.db"))
{
using (SQLiteCommand sqlcmd = sqlcon.CreateCommand())
{
sqlcon.Open();
sqlcmd.CommandText = txtQuery;
sqlcmd.ExecuteNonQuery();
}
}
}
private void createDataTable()
{
dataTable = new DataTable();
string txtQuery1 = "CREATE TABLE RECORDS ( ID varchar(255))";
ExecuteQuery(txtQuery1);
}
private void button1_Click(object sender, EventArgs e)
{
string txtQuery2 = "INSERT INTO RECORDS (ID) VALUES ('" + textBox1.Text + "')";
ExecuteQuery(txtQuery2);
}