gpt4 book ai didi

c# - Windows窗体的SQLite只能插入一个元素

转载 作者:太空宇宙 更新时间:2023-11-03 23:04:22 24 4
gpt4 key购买 nike

我尝试用 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')";

以不同的方式命名它们(sqlsql1)并执行插入命令两次。

关于c# - Windows窗体的SQLite只能插入一个元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41852941/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com