Let’s say I already have a correctly formed connection string named ‘myConn‘ in my Web.config. The database is on a different server and is MySQL. How do I connect to this database in my ASP.NET web app?
What I tried:
I tried installing the ‘ADO.NET C# DbContext Generator With WCF Support‘ but this blew up in my face with the error message ‘The assembly reference “System.Data.Entity” could not be added to the project. This wizar will continue to run, but the resulting project may not build properly.‘
Notes:
- Using Visual Studio 2010 (Version 10.0.40219.1 SP1Rel)
- Using .NET Framework 2.0
Assuming ODBC (not the only option), it should just be a matter of:
edit: oh, and in case it was getting it from the web.config you were wondering about (probably not, but who knows), that part would be:
Per your request, here’s the basics behind obdc data operations:
The following is for a query-type statement:
That will take data from your database and shove it into a DataTable object, using the OdbcDataAdapter object. This is not the only way to do it, but it is certainly the most basic. Look at this great answer for a method of converting the result into a list of whatever object you want, using a little bit of reflection. You can also use something like LINQ to map the data to custom classes, but I’ll leave that bit to you.
Anyway, here’s the same, but with a non-query:
Make sure to sanity-check my quotes and stuff, as I wrote it in the StackOverflow editor, not any kind of IDE, so unfortunately I dont get syntax highlighting 😛