I'm following the example from the ASP.NET MVC 2 Framework book (Steven Sanderson), except instead of using SQLServer, I'm using mySQL.
public class ProductsController : Controller
{
private IProductsRepository productsRepository;
public ProductsController()
{
// Temporary hard-coded connection string until we set up dependency injection
string connString = "server=xx.xx.xxx.xxx;Database=db_SportsStore;User Id=dbUser;Pwd=xxxxxxxxx;Persist Security Info=True;";
productsRepository = new SportsStore.Domain.Concrete.SqlProductsRepository(connString);
}
public ViewResult List()
{
return View(productsRepository.Products.ToList());
}
}
When I run the project I get the following error:
A network-related or instance-specific error occurred while establishing a connection to SQL Server. The server was not found or was not accessible. Verify that the instance name is correct and that SQL Server is configured to allow remote connections. (provider: Named Pipes Provider, error: 40 - Could not open a connection to SQL Server)
I have downloaded the MySQL .Net connector and in the Server Explorer I was able to view the tables on that DB. I also have made sure that the DBs on the server allows for remote connections.
I am creating and running the code locally via VS2010 and the MySQL DB is on hosted server.
What do I have to do to get this MVC2 example to function for MySQL?
Update:
I was playing around with the code while waiting for an answer and I updated the ProductsController to the following:
using MySql.Data.MySqlClient;
string connString = "server=xx.xx.xxx.xxx;Database=db_SportsStore;User Id=dbUser;Pwd=xxxxxx;";
var connection = new MySqlConnection(connString);
productsRepository = new SportsStore.Domain.Concrete.SqlProductsRepository(connection);
Then in the repository:
public SqlProductsRepository(MySql.Data.MySqlClient.MySqlConnection connection)
{
connection.Open();
productsTable = (new DataContext(connection)).GetTable<Product>();
connection.Close();
}
Now I get a totally different error:
{"You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '[t0].[ProductID], [t0].[Name], [t0].[Description], [t0].[Price], [t0].[Category]' at line 1"}
I should also mention... This example is using LINQ to SQL. I'm not sure if that matters or not. Please advise.