SQLite运行不正常C#

时间:2018-09-18 08:17:05

标签: c# sqlite uwp

using System;
using System.Collections.Generic;
using Microsoft.Data.Sqlite;

namespace DataAccessLibrary
{
    public static class DataAccess
    {
        public static void InitializeDatabase()
        {
            using (SqliteConnection db =
                new SqliteConnection("Filename=Crab.db"))
            {
                db.Open();
                String tableCommand = "create table if not exists MyTable (Crab integer primary key AutoIncrement," + " Box varchar(20)," + " Data varchar(20)," + "Data1 varchar(20)," + " Data2 varchar(20)," + "Data3 varchar(20)," + " Data4 varchar(20)," + " Data5 varchar(20))";
                SqliteCommand createTable = new SqliteCommand(tableCommand, db);
                createTable.ExecuteReader();
            }
        }

        public static void AddData(string inputText,string inputText1,string inputText2)
        {
            using (SqliteConnection db =
                new SqliteConnection("Filename=Crab.db"))
            {
                db.Open();
                SqliteCommand insertCommand = new SqliteCommand();
                insertCommand.Connection = db;
                insertCommand.CommandText = "insert into MyTable (Data,Data1,Data2) values (@Data,@Data1,@Data2)";            
                insertCommand.Parameters.AddWithValue("@Data", inputText);
                insertCommand.Parameters.AddWithValue("@Data1", inputText1);
                insertCommand.Parameters.AddWithValue("@Data2", inputText2);
                insertCommand.ExecuteReader();
                db.Close();
            }
        }

        public static void Add(string inputText3,string inputText4,string inputText5)
        {
            using (SqliteConnection db =
                new SqliteConnection("Filename=Crab.db"))
            {
                db.Open();
                SqliteCommand insertCommand = new SqliteCommand();
                insertCommand.Connection = db;
                insertCommand.CommandText = "insert into MyTable (Data3,Data4,Data5) values (@Data3,@Data4,@Data5)";
                insertCommand.Parameters.AddWithValue("@Data3", inputText3);
                insertCommand.Parameters.AddWithValue("@Data4", inputText4);
                insertCommand.Parameters.AddWithValue("@Data5", inputText5);
                insertCommand.ExecuteReader();
                db.Close();
            }
        }

        public static List<String> GetData()
        {
            List<String> entries = new List<string>();
            using (SqliteConnection db =
                new SqliteConnection("Filename=Crab.db"))
            {
                db.Open();
                SqliteCommand selectCommand = new SqliteCommand
                  ("select Crab from MyTable", db);
                selectCommand = new SqliteCommand
                   ("select Box from MyTable", db);
                selectCommand = new SqliteCommand
                   ("select Data from MyTable", db);
                selectCommand = new SqliteCommand              
                    ("select Data1 from MyTable", db);
                selectCommand = new SqliteCommand
                    ("select Data2 from MyTable", db);
                selectCommand = new SqliteCommand
                    ("select Data3 from MyTable", db);
                selectCommand = new SqliteCommand
                    ("select Data4 from MyTable", db);
                selectCommand = new SqliteCommand
                    ("select Data5 from MyTable", db);
                SqliteDataReader query = selectCommand.ExecuteReader();
                while (query.Read())
                {
                    entries.Add(query.GetString(0));
                }

                db.Close();
            }

            return entries;
        }
    }
}

This is the data for my code, here's the provided sql data

我可以知道为什么以这种方式插入数据吗?我试图将其连续6条记录。请帮忙!有没有办法使它填充第一行而不是新行?

<Page
    x:Class="UWPDataTest.BlankPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:UWPDataTest"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d"
    Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
    <Grid>
        <StackPanel>
        <TextBox x:Name="Data3" Text="" VerticalAlignment="Top" Width="1300"/>
        <TextBox x:Name="Data4" Text="" VerticalAlignment="Top" Width="1300"/>
        <TextBox x:Name="Data5" Text="" VerticalAlignment="Top" Width="1300"/>
            <Button x:Name="button" Content="Done" Click="Add" HorizontalAlignment="Left" Margin="694,190,0,0" VerticalAlignment="Top" RenderTransformOrigin="0.684,0.293"/>
        </StackPanel>
    </Grid>

</Page>

这是设计的布局。共有2个页面,名称为Data-Data5。

1 个答案:

答案 0 :(得分:1)

每次执行命令都会创建一个新行,您将使用2个单独的方法将其执行2次,这就是为什么要创建2行的原因,因此,如果您需要在1行中使用所有文本框数据,则必须一起执行它们只需一种方法,如下所示。

public static void AddData(string inputText,string inputText1,string inputText2,string inputText3,string inputText4,string inputText5)
{
    using (SqliteConnection db =
        new SqliteConnection("Filename=Crab.db"))
    {
        db.Open();
        SqliteCommand insertCommand = new SqliteCommand();
        insertCommand.Connection = db;
        insertCommand.CommandText = "insert into MyTable (Data,Data1,Data2,Data3,Data4,Data5) values (@Data,@Data1,@Data2,@Data3,@Data4,@Data5)";            
        insertCommand.Parameters.AddWithValue("@Data", inputText);
        insertCommand.Parameters.AddWithValue("@Data1", inputText1);
        insertCommand.Parameters.AddWithValue("@Data2", inputText2);
        insertCommand.Parameters.AddWithValue("@Data3", inputText3);
        insertCommand.Parameters.AddWithValue("@Data4", inputText4);
        insertCommand.Parameters.AddWithValue("@Data5", inputText5);
        insertCommand.ExecuteReader();
        db.Close();
    }
}

您只需要此 AddData 方法,并且您可以删除 Add 方法,因为它只是一个额外的方法。