本文介绍了初始化连接字符串失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我像这样在app.config中指定了连接字符串和我的文件路径


< configuration>
< connectionstrings>
< add name ="nConnectionString">
connectionString =数据源= CSZ-PCS43132 \ SQLEXPRESS;初始目录=测试;集成安全性= True"
providerName ="System.Data.SqlClient"/>


< appsettings>
<添加key ="empdetails" value =〜/C:/Documents and Settings/aj99823/Desktop/project/Employee Details.txt">



在前端我称他们为

public void employeedetails()
{
SqlConnection conn =新的SqlConnection(ConfigurationManager.ConnectionStrings ["nConnectionString"].ConnectionString);
//使用(SqlConnection连接=新的SqlConnection(str))
{
试试
{
conn.Open();
}
捕获(例如System.Data.SqlClient.SqlException)
{
返回;
}
字符串selectCommandText =选择不同的用户ID,全名,姓氏,名字,本地职位,就业状态,加入日期,退出日期,网站ID,电子邮件,顶级功能ID,活动,退出公司,lmsroleid,本地语言职位从tblUserProfile向上,tbl报告为tr,tblt国家为temo为空" br/> 使用(SqlDataAdapter adpSQL =新的SqlDataAdapter(selectCommandText,conn))
{
使用(DataTable表= new DataTable("tblUserProfile"))
{
adpSQL.Fill(table);
StringBuilder commaDelimitedText = new StringBuilder();
//commaDelimitedText.Append("EMP_Number |名称|姓氏|名字|中间名|名称后缀|位置标题| EMPcode | EMPstatus |管理员编号|批准人编号|开始日期|结束日期| Lastreviewdate |地址1 |地址2 |城市|州|邮政编码|国家/地区|电子邮件| Phone1 | Phone2 | Fax | URL | Note | DomainCode | Organizationcode | Jobcode | Active | Enabled | Username | NTlogin | Role | Timezone | Currency | Language | Text1 | Text2 | Text3 | Text4 | Memo1 | Date1 | Date2 | Indicator1 | Money1 | Integer1 | Float1 | JobJoiningdate | Organizationjoiningdate);
foreach(表中的DataRow行.行)
{
字符串值= string.Format("{0} | {1} | {2} |||| {3} || {4} | {5} |||||| {6} || {7} | { 8} | {9} ||||||||| {10} | {11} | {12} | {13} |",第[0]行,第[1]行,第[2]行,第[3]行],行[4],行[5],行[6],行[7],行[8],行[9],行[10],行[11],行[12],行[13 ],第[14]行);
commaDelimitedText.AppendLine(value);
}
File.AppendAllText(ConfigurationManager.AppSettings ["empdetails"],commaDelimitedText.ToString());
}
}
}
}


但这会引发错误,表示无法初始化连接.

如果我将连接字符串放在后面之后,连接字符串将被成功初始化,但是在我指定文件路径的地方会引发错误.....即以下行...

File.AppendAllText(ConfigurationManager.AppSettings ["empdetails"],commaDelimitedText.ToString());

谁能帮我吗??????????????

i specified the connection string and my file path in app.config like this


<configuration>
<connectionstrings>
<add name="nConnectionString">
connectionString ="Data Source=CSZ-PCS43132\SQLEXPRESS;Initial Catalog=test;Integrated Security=True"
providerName="System.Data.SqlClient"/>


<appsettings>
<add key="empdetails" value="~/C:/Documents and Settings/aj99823/Desktop/project/Employee Details.txt">



and in the front end i called them as

public void employeedetails()
{
SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["nConnectionString"].ConnectionString);
// using (SqlConnection connection = new SqlConnection(str))
{
try
{
conn.Open();
}
catch (System.Data.SqlClient.SqlException ex)
{
return;
}
string selectCommandText = "SELECT distinct userid,fullname,lastname,firstname,localjobtitle,employmentstatusid,dateofjoining,dateofexit,siteid,email,topfunctionid,active,exitingthecompany,lmsroleid,locallanguagejobtitle FROM tblUserProfile up,tblreportingto tr,tblCountry where temo is null";
using (SqlDataAdapter adpSQL = new SqlDataAdapter(selectCommandText, conn))
{
using (DataTable table = new DataTable("tblUserProfile"))
{
adpSQL.Fill(table);
StringBuilder commaDelimitedText = new StringBuilder();
//commaDelimitedText.Append("EMP_Number|Name|Lastname|Firstname|Middleinitial|NameSuffix|Positiontitle|EMPcode|EMPstatus|Manager number|Approvernumber|Startdate|Enddate|Lastreviewdate|Address1|Address2|City|State|Zipcode|Country|Email|Phone1|Phone2|Fax|URL|Note|DomainCode|Organizationcode|Jobcode|Active|Enabled|Username|NTlogin|Role|Timezone|Currency|Language|Text1|Text2|Text3|Text4|Memo1|Date1|Date2|Indicator1|Money1|Integer1|Float1|JobJoiningdate|Organizationjoiningdate");
foreach (DataRow row in table.Rows)
{
string value = string.Format("{0}|{1}|{2}|||{3}||{4}|{5}||||||{6}||{7}|{8}|{9}||||||||{10}|{11}|{12}|{13}|", row[0], row[1], row[2], row[3], row[4], row[5], row[6], row[7], row[8], row[9], row[10], row[11], row[12], row[13], row[14]);
commaDelimitedText.AppendLine(value);
}
File.AppendAllText(ConfigurationManager.AppSettings["empdetails"], commaDelimitedText.ToString());
}
}
}
}


But it is throwing error saying failed to initialize the connection.

if i put the just after the my connection string will get initialized successfuly but it will throw error where i specified the file path.....that is the following line...

File.AppendAllText(ConfigurationManager.AppSettings["empdetails"], commaDelimitedText.ToString());

can anyone please help me?????????????

推荐答案


这篇关于初始化连接字符串失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-23 08:13