2

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

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)?

I'm using VS2012 if that makes any difference.

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

6
  • 1
    Yes they can, just like any other .NET application: Connecting to MySQL Database using C# and .NET - there's plenty of tutorials online, did you check any? Commented Jan 1, 2013 at 15:25
  • Consider using the EntityFramework, it should work with MySql as well, although i never tried that for myself. Search for EntityFramework and MySql. Commented Jan 1, 2013 at 15:33
  • I agree with the previous comments, use Entity Framework for simplicity's sake. I want to add that I recommend you take a "code first" approach, because it is easier to start with, and because that's the direction the future releases of EF are headed. I'm using it and it's great. Commented Jan 1, 2013 at 17:51
  • -1 What does DB have to do with WPF? Your question needs to be more specific. Commented Jan 1, 2013 at 18:16
  • Hmm basically, I need to know how to retrieve/store information in a database from my application. I don't know of any other usage so I thought it was clear enough... editing Commented Jan 1, 2013 at 18:46

2 Answers 2

2

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();
Sign up to request clarification or add additional context in comments.

Comments

1

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

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.

1 Comment

Yep, that's what I WAS doing until now... I think it's about time to get my hands dirty and have some fun with it... EF it is then!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.