[c#]SQL 服务器 ConnectionString 错误

标签: Asp.net SQLServer C#
发布时间: 2017/2/25 10:42:00
注意事项: 本文中文内容可能为机器翻译,如要查看英文原文请点击上面连接.

只是改变了到一个 SQL Server 数据库,只想访问我 connectionstring 我 web.config 中设置。我得到以下错误︰

ConnectionString 属性尚未初始化。

Web.Config 的代码︰

<configuration>
  <configSections>
    <sectionGroup name="applicationSettings" type="System.Configuration.ApplicationSettingsGroup, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
      <section name="EditorGenerator.Properties.Settings" type="System.Configuration.ClientSettingsSection, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
    </sectionGroup>    
  </configSections>
  <connectionStrings>
    <add name="loganWebConn" connectionString="Data Source=PMCUSSRV05;Integrated Security=true;Initial Catalog= loganWeb;User Id=logan_sys;Password=appPwd;" providerName="System.Data.SqlClient" />
  </connectionStrings>

C# 代码︰

        SqlConnection con = new System.Data.SqlClient.SqlConnection();
        con.ConnectionString = ConfigurationManager.ConnectionStrings["loganWebConn"].ConnectionString;
        con.Open();
        try
        {
            string sql = "select j.*, u.fullname as plannerName from logan_jobs j left join on logan_user u on u.userID = j.plannerID ";
            sql += "where 0=0 ";
            sql += "and plannerID =" + planner + " ";
            sql += "and " + dept + " in (select deptID from logan_prodData p where p.jobID = j.jobID and curEstEndDate >= " + fromDate + " and curEstEndDate <= " + toDate + " ) ";
            sql += "order by jobNumber";

            SqlCommand cmd = new SqlCommand(sql, con);
            SqlDataAdapter da = new SqlDataAdapter(cmd);

我到底做错了?这是很基本的东西在这里......

解决方法 1:

我相信你的问题是指定了 Integrated Security=true ,但你也有提供一个用户名和密码。

如果你真的想要使用集成安全性,删除用户名/密码

它也会值得删除之前的目录名称空间 loganWeb

这将使您的连接字符串

<add name="loganWebConn" connectionString="Data Source=PMCUSSRV05;Initial Catalog=loganWeb;Integrated Security=true;" providerName="System.Data.SqlClient" />
赞助商