I am a beginner at MVVM design patter and WPF I am trying to make a small example of connection to the MS Access database using the WPF and MVVM Light Toolkit patter design. but when i run the program it does not do anything and when i debug i find that the instruction stops at the instruction level.con.Open();
model / DataConnection.cs file and then it goes to MainWindow.xaml.cs.
I do not find why he does not continue the execution of all the instructions?
What I have tried:
Model / DataConnection.cs
public class DataConnection
{
public void OpenConnection()
{
OleDbConnection con = new OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=db.accdb;" +
"Persist Security Info=False;");
OleDbCommand cmd = con.CreateCommand();
cmd.CommandText = "Insert into table (num,nom)Values(123,'nom1')";
cmd.Connection = con;
con.Open();
cmd.ExecuteNonQuery();
MessageBox.Show("Record Submitted", "Congrats");
}
}
ViewModel / MainViewModel.cs
public class MainViewModel : ViewModelBase
{
private DataConnection _dataconnection;
public MainViewModel(DataConnection dataconnexion)
{
_dataconnection = dataconnexion;
_dataconnection.OpenConnection();
}
}
ViewModel / ViewModelLocator.cs
public class ViewModelLocator
{
static ViewModelLocator()
{
ServiceLocator.SetLocatorProvider(() => SimpleIoc.Default);
SimpleIoc.Default.Register();
SimpleIoc.Default.Register();
}
public MainViewModel Main
{
get
{
return ServiceLocator.Current.GetInstance();
}
}
/// ///Cleans up all the resources. /// public static void Cleanup()
{
}
}
解决方案It says that because you don't have either an appropriate architecture of Office installed or Access Runtime[^] installed on the machine.
If your app is compiled AnyCPU and running on a 64-bit machine, you'd have to use the 64-bit Office or Runtime.
If your app is compiled AnyCPU and running on a 32-bit machine, you'd have to use the 32-bit Office or Runtime.
If your app is compiled x86, you have to have the 32-bit Office or Runtime installed.
If your app is compiled x64, you have to have the 64-bit Office or Runtime installed.