使用MVVM设计模式连接到WPF中的数据库 [英] Connection to the database in WPF with MVVM design pattern

查看:817
本文介绍了使用MVVM设计模式连接到WPF中的数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是MVVM设计模式和WPF的初学者我正在尝试使用WPF和MVVM Light Toolkit模式设计创建一个连接到MS Access数据库的小例子。但是当我运行程序时它没有做任何事情,当我调试时,我发现指令在指令级停止。

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文件,然后转到MainWindow.xaml.cs。

我不知道为什么他不继续执行所有指令?



我尝试过的事情:



Model / DataConnection.cs



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



ViewModel / MainViewModel.cs

public class MainViewModel : ViewModelBase  
    {  
 private DataConnection _dataconnection;  
 public MainViewModel(DataConnection dataconnexion)  
        {  
            _dataconnection = dataconnexion;  
          _dataconnection.OpenConnection();  
   
 }  
}  



ViewModel / ViewModelLocator.cs


ViewModel / ViewModelLocator.cs

public class ViewModelLocator  
   {  
       static ViewModelLocator()  
       {  
           ServiceLocator.SetLocatorProvider(() => SimpleIoc.Default);  
SimpleIoc.Default.Register<DataConnection>();  
SimpleIoc.Default.Register<MainViewModel>();  
       }  
 public MainViewModel Main  
       {  
           get  
           {  
               return ServiceLocator.Current.GetInstance<MainViewModel>();  
           }  
       }  
  
       /// <summary>  
       /// Cleans up all the resources.  
       /// </summary>  
       public static void Cleanup()  
       {  
       }  
   }

推荐答案

它说因为你没有安装适当的Office体系结构或访问运行时 [ ^ ]安装在机器上。



如果您的应用程序是编译AnyCPU并在64位计算机上运行,​​则必须使用64位Office或运行时。



如果您的应用程序已编译一个nyCPU并在32位机器上运行,你必须使用32位Office或Runtime。



如果你的应用程序是x86编译的,你必须安装了32位Office或Runtime。



如果您的应用程序是x64编译的,则必须安装64位Office或Runtime。
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.


这篇关于使用MVVM设计模式连接到WPF中的数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆