本文介绍了实际主机中的连接字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 你好...... 在本地我有一个连接字符串(web.config)如下 的ConnectionStrings> < add name = bongahConnectionString connectionString = 数据源= MEHDI-PC;初始目录= bongah;集成安全性=真 providerName = System.Data.SqlClient / > < / connectionStrings > 在主持人中有一个像这样的连接字符串 数据源= .\SQLEXPRESS;集成安全性= SSPI; AttachDBFilename = | DataDirectory | aspnetdb.mdf;用户实例= true 当我尝试访问db时出现此错误 找不到网络路径 描述:期间发生了未处理的异常执行当前的Web请求。请查看堆栈跟踪以获取有关错误及其源自代码的位置的更多信息。 异常详细信息:System.ComponentModel.Win32Exception:找不到网络路径 源错误: 行 78 :{行 79 :SqlConnection con = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings [ bongahConnectionString]的ToString()); 行 80 :con.Open(); 行 81 : string query1 = SELECT ID FROM aparteman WHERE ID =(SELECT MAX(ID)FROM aparteman); 行 82 :SqlCommand cmd = new SqlCommand(query1,con); 我该怎么办?解决方案 嗨Mehdi, 数据源= MEHDI-PC;初始目录= bongah;集成安全性=真 1)在上面的连接字符串中,尝试将MEHDI-PC替换为IP地址,从连接字符串的角度来看,它将更容易理解。(可选) 2)我认为你错过了数据库实例名称。 示例SQL Server连接字符串应该如下所示 connectionString =SERVER = 192.168.xx.xx\SQL2008; DataBase = DatabaseName; Initial Catalog = bongah; UID = Username; pwd = ****; 希望这对你有所帮助。 问候, RK 在Web.config中使用此代码 < connectionstrings > < add name = NewConnectionString connectionstring = Data Source = .\SQLEXPRESS; AttachDbFilename = | DataDirectory | \ shoping.mdf; Integrated Security = True; User Instance = True providername = System.Data.SqlClient / > < / connectionstrings > 现在在FileName.aspx.CS文件中使用此代码 SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings [ NewConnectionString]的ConnectionString); Hi there...In local hast I have a connection string(web.config) as belowconnectionStrings> <add name="bongahConnectionString" connectionString="Data Source=MEHDI-PC;Initial Catalog=bongah;Integrated Security=True" providerName="System.Data.SqlClient" /> </connectionStrings>and in host there is a connectionstring like thisdata source=.\SQLEXPRESS;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|aspnetdb.mdf;User Instance=truewhen i try to access to db this error appearThe network path was not foundDescription: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. Exception Details: System.ComponentModel.Win32Exception: The network path was not foundSource Error: Line 78: {Line 79: SqlConnection con = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["bongahConnectionString"].ToString());Line 80: con.Open();Line 81: string query1 = "SELECT ID FROM aparteman WHERE ID=(SELECT MAX(ID)FROM aparteman)";Line 82: SqlCommand cmd = new SqlCommand(query1, con);What can i do? 解决方案 Hi Mehdi,Data Source=MEHDI-PC;Initial Catalog=bongah;Integrated Security=True1) In the above connection string, try to replace MEHDI-PC to IP Address it will be more easy to understand in Connection string point of view.(optional)2) I think you have missed the DB instance name in it.Sample SQL Server Connection string should look like this connectionString="SERVER=192.168.xx.xx\SQL2008;DataBase=DatabaseName;Initial Catalog=bongah;UID=Username;pwd=****;"Hope this helps you a bit.Regards,RKUse This Code in Web.config<connectionstrings> <add name="NewConnectionString" connectionstring="Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\shoping.mdf;Integrated Security=True;User Instance=True" providername="System.Data.SqlClient" /></connectionstrings>Now Use this code in FileName.aspx.CS file SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["NewConnectionString"].ConnectionString); 这篇关于实际主机中的连接字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 10-29 19:32