我正在创建一个应用程序,我只是将数据存储在 DB 中并从 DB 获取数据。我正在将 SQLite 用于数据库。
MainWindows.xaml.cs 文件
using System.Data.SQLite;
namespace SQLiteSample
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
private SQLiteCommand sql_cmd;
private DataSet DS = new DataSet();
private DataTable DT = new DataTable();
private SQLiteDataAdapter DB;
string myConnectionString = "Data Source=C:\\Documents and Settings\\20024509\\My Documents\\SQLite\\Enum.db";
SQLiteConnection myConnection = new SQLiteConnection();
public MainWindow()
{
InitializeComponent();
}
private void LoadData()
{
try
{
myConnection.ConnectionString = myConnectionString;
myConnection.Open();
sql_cmd = myConnection.CreateCommand();
string CommandText = "select EmpID,EmpName from Employee_Info";
DB = new SQLiteDataAdapter(CommandText, myConnection);
DS.Reset();
DB.Fill(DS);
DT = DS.Tables[0];
// grid1.DataSource = DT;
myConnection.Close();
}
catch (Exception e)
{
myConnection.Close();
}
}
private void SaveData()
{
SQLiteConnection myConn = new SQLiteConnection(myConnectionString);
string myInsertQuery = "INSERT INTO Employee_Info(EmpID,EmpName,EmpAge,EmpSalary) Values(503453535, 'DEVELOPMENT',32987654)";
SQLiteCommand sqCommand = new SQLiteCommand(myInsertQuery);
sqCommand.Connection = myConn;
myConn.Open();
try
{
sqCommand.ExecuteNonQuery();
}
finally
{
myConn.Close();
}
}
private void FromDB_Click(object sender, RoutedEventArgs e)
{
LoadData();
}
private void ToDB_Click(object sender, RoutedEventArgs e)
{
SaveData();
}
}
}
我从“http://system.data.sqlite.org/index.html/doc/trunk/www/downloads.wiki”(Setups for 32-bit Windows (.NET Framework 4.0)) 下载了 Binary for .net 并安装它。
然后在我的 wpf 应用程序中添加参考。但它仍然给出异常
"{"'The invocation of the constructor on type 'SQLiteSample.MainWindow'
that matches the specified binding constraints threw an exception.'
Line number '3' and line position '9'."}"