Disclaimer: This article is an original blog post published by the javaeye website. It is strictly prohibited to repost this article on any website without the author's written consent. Otherwise, it will be held legally responsible!
I have read more than N. net layer-3 data, but I have not really tried to use layer-3 for a small project. Today, I finally made up my mind to use layer-3 as a news publishing system, which is very simple, add, delete, query, and modify a database...
I didn't expect that I had trouble at the beginning. How can I read the database connection string in Web. config In the Dal class after adding the new project Dal in the solution ....
After my multi-party verification and search, I finally solved the problem. Now I want to write down the solution:
Create a project,
File → add → new project,
Select C #'s underpants and store them in all directories of the web project,
In this case, the solution manager can change the default Class Name:
After the preparation of the foreplay, we began to write the database connection string in Web. config. below is the code snippet of my web. config:
<connectionStrings><add name="connStr" connectionString="server=niunan\sqlexpress; database=newssystem; uid=sa; pwd=123456"/></connectionStrings>
We want to read the web from the Dal layer. you must use system. the *** class in the Web, but in the class switch, we enter the using system. web. the code below is automatically prompted, so you can refer to the reference of the Dal layer in solution Manager:
And no system is found. web, so the previous Code will automatically prompt to crash, next we just need to introduce the system. the Web is enough. After testing, you have to introduce system. configuration can be read to the Web. add reference in config:
Solution manager after successful addition
Then we can use the code to intelligently prompt reference of system. Web. configuration, and then we can get the content in Web. config. below is the code of sqlhelper. cs I wrote:
Using system; using system. collections. generic; using system. LINQ; using system. text; using system. web. configuration; // reference the namespace Dal {public class sqlhelper {// <summary> // test method, obtain the database connection string /// </Summary> /// <returns> </returns> Public static string getconnstr () {return webconfigurationmanager. connectionstrings ["connstr"]. tostring ();}}}
Then we will test whether the data is obtained successfully on the page. Similarly, we need to use the data in the Dal layer in the web project because it belongs to two different projects, so we also need to reference Dal,
After the reference, I found that there was no change in the solution manager, but there was no change, but it was indeed a reference to the Dal layer. Now I can write code. below is my default. aspx. the Cs source code is here to test whether it can be obtained, and there is nothing on the page:
Using system; using system. collections. generic; using system. LINQ; using system. web; using system. web. ui; using system. web. UI. webcontrols; using Dal; // reference the Dal layer. Pay attention to the case. The name must be public partial class _ default: system. web. UI. page {protected void page_load (Object sender, eventargs e) {// I used the static method over there, so I can directly click the class name to find its method response. write (Dal. sqlhelper. getconnstr ());}}
Finally, preview the page to see the read ....