开发者

Where to set MVC 3 Connection Strings in code

开发者 https://www.devze.com 2023-03-08 15:07 出处:网络
I would like to connect to an existing database, and I put the connection string in the ConnectionStrings section of the Web.Config. But where and how do you refer to this connection in the code. For

I would like to connect to an existing database, and I put the connection string in the ConnectionStrings section of the Web.Config. But where and how do you refer to this connection in the code. For example, I try things like

string connString = System.Configuration.ConfigurationManager.ConnectionStrings["MyDatabase"].ToStr开发者_JAVA技巧ing();

in my C# code right before I read the database.

The only way I can make it work is to hard-code the connection string in the C# code, because I don't know how to get it from Web.Config.


That code you have is correct (i fixed one small typo with it). Make sure you add a reference to System.Configuration to your project. And obviously make sure that "MyDatabase" exists as a connection string in the project's web.config.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号