Last post Dec 09, 2015 09:32 AM by Mikesdotnetting
Dec 08, 2015 03:36 PM|Dan Zara|LINK
Hello please how can i create a _AppStart.cshtml to connect to a MySQL database with connection string on webmatrix.
Dec 08, 2015 08:26 PM|Mikesdotnetting|LINK
You shouldn't connect to the database in AppStart. You should only connect to it when you need data.
Dec 09, 2015 08:24 AM|Dan Zara|LINK
Thanks mike, so you mean i dont need the _AppStart file at all when working with MySql database? Please any code example for connecting to the MySQL database or can i still use the
var db = Database.Open("database name");
to connect to the MySql database?
Dec 09, 2015 09:32 AM|Mikesdotnetting|LINK
No, you don't need to use the AppStart file to open connections to a database.
When working with any database other that SQL Compact, you pass in the name of the name of a connection string to the Database.Open method. You define the connection string in the <connectionStrings> section of your web.config:
The connection string value will depend on what technology you are using to connect to the database, but here are a range of examples: https://www.connectionstrings.com/mysql/. If you are using the .NET
connector, your web.config might look like this:
Then in the pages where you want to work with the database, you call:
var db = Database.Open("Sample");