linq to sql - MVC2 and mySQL database -
i'm following example 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 set 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 run project following error:
a network-related or instance-specific error occurred while establishing connection sql server. server not found or not accessible. verify instance name correct , sql server configured allow remote connections. (provider: named pipes provider, error: 40 - not open connection sql server)
i have downloaded mysql .net connector , in server explorer able view tables on db. have made sure dbs on server allows remote connections.
i creating , running code locally via vs2010 , mysql db on hosted server.
what have mvc2 example function mysql?
update:
i playing around code while waiting answer , updated productscontroller 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 repository:
public sqlproductsrepository(mysql.data.mysqlclient.mysqlconnection connection) { connection.open(); productstable = (new datacontext(connection)).gettable<product>(); connection.close(); }
now totally different error:
{"you have error in sql syntax; check manual corresponds mysql server version right syntax use near '[t0].[productid], [t0].[name], [t0].[description], [t0].[price], [t0].[category]' @ line 1"}
i should mention... example using linq sql. i'm not sure if matters or not. please advise.
a couple of thoughts come mind.
- the connection string using connection string if connecting sql server db instead of mysql .net connector connection string. you're going have change connection string example book mysql needs. example, mysql doesn't have trusted connection option. (http://dev.mysql.com/doc/refman/5.1/en/connector-net-connection-options.html)
- in sqlproductsrepository you'll need use mysqlconnection object , forth instead of sqlconnection object if used. think book used linq sql or linq entity framework. either way you'll have modify code use mysql... objects or modify model hit mysql if it's entity framework. if did connection string in give idea of need in #1.
http://dev.mysql.com/doc/refman/5.1/en/connector-net-tutorials-intro.html
edit - update...
mysql doesn't have same syntax sql server sometimes. assuming error pointing to.
on mysql connector connection string, has an option use sql server syntax called sql server mode. set true , see if works....
something like:
server=serverip;database=dbname;uid=username;pwd=password;sql server mode=true;
Comments
Post a Comment