我正在寻找这个问题的答案。我不知道问题出在哪里。Visual Studio 没有发现任何问题或错误,但是当我运行程序并尝试向数据库添加内容时,应用程序崩溃并且 VS 告诉我我的表不存在。该事件是为了添加一些东西。
这是我的代码:
private void addBtn_Click(object sender, RoutedEventArgs e)
{
using (WpisyDataContext BazaDanych = new WpisyDataContext(strConnectionString))
{
if (!BazaDanych.DatabaseExists())
{
BazaDanych.CreateDatabase();
MessageBox.Show("BazaDanych Database Created Successfully!!!");
}
Wpis newWpis = new Wpis
{
RecordID = index_box.Text,
NameRec = name_box.Text.ToString(),
BeneficiaryRec = beneficiary_box.Text.ToString(),
PriceRec = price_box.Text.ToString(),
DeadlineRec = deadline_box.Text.ToString(),
DescriptionRec = description_box.Text.ToString()
};
BazaDanych.Wpisy.InsertOnSubmit(newWpis);
BazaDanych.SubmitChanges();
MessageBox.Show("Recoed Added Successfully!!!");
if (NavigationService.CanGoBack)
{
NavigationService.GoBack();
}
}
}
}
这是表格代码(Wpis.cs
):
using System.Data.Linq.Mapping;
namespace ********
{
[Table]
public class Wpis
{
[Column(IsPrimaryKey = true, IsDbGenerated = true)]
public string RecordID
{
get;
set;
}
[Column(CanBeNull = true)]
public string NameRec
{
get;
set;
}
[Column(CanBeNull = true)]
public string BeneficiaryRec
{
get;
set;
}
[Column(CanBeNull = true)]
public string PriceRec
{
get;
set;
}
[Column(CanBeNull = true)]
public string DeadlineRec
{
get;
set;
}
[Column(CanBeNull = true)]
public string DescriptionRec
{
get;
set;
}
}
}
这是数据上下文 ( WpisyDataContext.cs
):
using System.Data.Linq;
namespace *************
{
public class WpisyDataContext : DataContext
{
public WpisyDataContext(string connectionString)
: base(connectionString)
{
}
public Table<Wpis> Wpisy
{
get
{
return this.GetTable<Wpis>();
}
}
}
}
这是连接字符串:
public static string strConnectionString = @"Data Source=isostore:/DevDB.sdf";
拜托各位,我已经没有耐心了。谢谢。