使用 WPF 应用程序连接到数据库

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14111566/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 06:51:37  来源:igfitidea点击:

Connecting to a database using a WPF application

c#wpfdatabasemvvm

提问by Asaf

I've started getting into WPF not so long ago.
As I'm in the stage of learning MVVM, I'm using THIStutorial.

不久前我开始接触 WPF。
由于我正处于学习 MVVM 的阶段,因此我正在使用教程。

Following that tutorial I now have a basic project that involves products.
The next thing I want to do, is learn how to connect to a database and store/retrieve information from it.

按照该教程,我现在有一个涉及产品的基本项目。
我要做的下一件事是学习如何连接到数据库并从中存储/检索信息。

My question is, what are the available ways to connect to a database? what is the best, efficient way to do it?

我的问题是,连接到数据库的可用方法有哪些?最好、最有效的方法是什么?

Also, can WPF applications connect to an hosted mysql database (the ones used for websites)?

此外,WPF 应用程序是否可以连接到托管的 mysql 数据库(用于网站的数据库)?

I'm using VS2012 if that makes any difference.

如果这有什么不同,我正在使用 VS2012。

Excuse my newbie-ness! I'm still just a beginner! Thank you in advance!

原谅我的菜鸟!我还只是一个初学者!先感谢您!

采纳答案by Jobo

Check this: EF code first with oracle, mysql, etc.

检查这一点:首先使用 oracle、mysql 等进行 EF 代码。

At the end, there is download link for a sample.

最后,有一个示例的下载链接。

As for WPF, i would recommend to read some tutorials if you′re not familiar with it. Searching for WPF and EF together will lead to a lot of more or less helpful tutorials and blogs, etc. But i would recommend playing around with the Entityframework and the code-first approach first.

至于WPF,如果你不熟悉它,我建议你阅读一些教程。一起搜索 WPF 和 EF 会产生很多或多或少有用的教程和博客等。但我建议先尝试使用 Entityframework 和代码优先方法。

回答by NoName

Setting up Entity Framework takes too long + Compatibility issues, thus not efficient. Use this:

设置实体框架耗时太长 + 兼容性问题,效率不高。用这个:

 SqlConnection connection = new SqlConnection
 {
      ConnectionString = ConfigurationManager.ConnectionStrings["Connection_String_Name"].ConnectionString
 };

 connection.Open();

 SqlCommand cmd = new SqlCommand("Query_For_What_You_Wanna_Do");
 cmd.ExecuteNonQuery();

 connection.Close();