我尝试用 C# 做一个 Todolist
我用文本框填充了一个面板。我使用 SQLite。我在当前目录中创建一个文件。我有一个带有 int 和 VARCHAR 的简单表。但我只能在线写作。当我重新启动时,我无法重新放置这条线。我需要删除 SQLite 文件。
谢谢
开始
namespace Todolist
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
if (!File.Exists("MaBaseDeDonnees.sqlite"))SQLiteConnection.CreateFile("MaBaseDeDonnees.sqlite");
SQLiteConnection maConnexion;
var ConnectionString = "Data Source=|DataDirectory|MaBaseDeDonnees.sqlite";
maConnexion = new SQLiteConnection(ConnectionString);
maConnexion.Open();
var sql = "create table IF NOT EXISTS todo (nb INTEGER PRIMARY KEY , data VARCHAR(255))";
var commande = new SQLiteCommand(sql, maConnexion);
commande.ExecuteNonQuery();
sql = "insert into todo (data) values ('text1')";
sql = "insert into todo (data) values ('text2')";
commande = new SQLiteCommand(sql, maConnexion);
commande.ExecuteNonQuery();
maConnexion.Close();
display_todo();
}
显示:
private void display_todo()
{
var ConnectionString = "Data Source=|DataDirectory|MaBaseDeDonnees.sqlite";
var maConnexion = new SQLiteConnection(ConnectionString);
maConnexion.Open();
var sql = "select * from todo";
var commande = new SQLiteCommand(sql, maConnexion);
var reader = commande.ExecuteReader();
while (reader.Read())
{
var textBox1 = new TextBox();
textBox1.Location = new Point(10, 10);
textBox1.Text = (string) reader["data"];
textBox1.Size = new Size(200, 30);
todoPannel.Controls.Add(textBox1);
}
maConnexion.Close();
只显示text2
抱歉编辑:
sql = "insert into todo (data) values ('text1')";
commande = new SQLiteCommand(sql, maConnexion);
commande.ExecuteNonQuery();
String sql1 = "insert into todo (data) values ('text2')";
commande = new SQLiteCommand(sql1, maConnexion);
commande.ExecuteNonQuery();
我总是出错
您正在使用相同的变量 (sql
) 来存储 2 个不同的插入:
sql = "insert into todo (data) values ('text1')";
sql = "insert into todo (data) values ('text2')";
以不同的方式命名它们(sql
和sql1
)并执行插入命令两次。
我是一名优秀的程序员,十分优秀!