我想插入一些數據到一個SQLite文件,但當我嘗試這樣做沒有東西被添加到文件,這是我第一次與SQLite處理,所以不太確定我在做它對。請詢問是否需要其他信息。C#SQLite插入不工作
這裏是源代碼。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Data.SQLite;
namespace WpfApplication1
{
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
Readlines();
}
public void Readlines()
{
string[] lines = System.IO.File.ReadAllLines(@"D:\Projects\GFKMVC\WpfApplication1\WpfApplication1\Opgave001.txt");
Shift shift = new Shift();
SQLiteConnection.CreateFile("TimePlan.sqlite");
SQLiteConnection dbcon;
dbcon = new SQLiteConnection("Data Source=TimePlan.sqlite;Version=3;");
dbcon.Open();
//string DropTable = "DROP DATABASE TimePlane.sqlite";
//SQLiteCommand dropCom = new SQLiteCommand(DropTable, dbcon);
//dropCom.ExecuteNonQuery();
string CreateTable = "CREATE TABLE Shifts (EmployeeID INT, Date DATETIME, StartTime DATETIME, EndTime DATETIME, Break INT)";
SQLiteCommand createCom = new SQLiteCommand(CreateTable, dbcon);
createCom.ExecuteNonQuery();
foreach (string line in lines)
{
string[] fields = line.Split(';');
shift.EmployeeID = Int32.Parse(fields[0]);
DateTime dt = DateTime.ParseExact(fields[1], "yyyyMMdd", System.Globalization.CultureInfo.InvariantCulture);
shift.Date = dt;
DateTime st = DateTime.ParseExact(fields[2], "HHmm", System.Globalization.CultureInfo.InvariantCulture);
shift.StartTime = st;
DateTime et = DateTime.ParseExact(fields[3], "HHmm", System.Globalization.CultureInfo.InvariantCulture);
shift.EndTime = et;
shift.Break = Int32.Parse(fields[4]);
string InsertSql = "INSERT INTO Shifts (EmployeeID, Date, StartTime, EndTime, Break) values ("+shift.EmployeeID+","+shift.StartTime+","+shift.EndTime+","+shift.Break+")";
SQLiteCommand InsertCom = new SQLiteCommand(InsertSql, dbcon);
InsertCom.ExecuteNonQuery();
}
dbcon.Close();
}
}
}
你得到任何錯誤信息中插入的另一種方式?例外?你如何檢查表是否爲空? – Steve
檢查,看看是否有任何參數的值要傳遞到SQL字符串,如shift.EmployeeID,shift.StartTime等都是NULL – Harsh
@Steve我得到這樣的:SQL邏輯錯誤或丟失的數據庫 近「06」:語法錯誤。 我通過查找我的系統中的db文件並通過sqlite瀏覽器檢查它來檢查。 –