ConnectionString配置错误

时间:2009-04-15 20:46:32

标签: c# .net asp.net configuration

我正在尝试使用asp.net,sql和xml制作RSS Feed。 我收到了错误

  

编译器错误消息:CS0103:   名称'MyConnString'不存在于   目前的背景“在第22行   “SqlConnection objConnection = new   的SqlConnection( “MyConnString”);

我的网页配置包含

<connectionStrings>
    <add name="MyConnString" connectionString="
     providerName="System.Data.SqlClient" />
  </connectionStrings>

这是代码

<%@ Page Language="C#" MasterPageFile="ContentMasterPage.master" Debug="true" %>

<%@ Import Namespace="System.Xml"%>
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.SqlClient" %>

<script runat="server">
    void Page_load(object sender, System.EventArgs e)
    {
        Response.Clear();
        Response.ContentType = "text/xml";
        XmlTextWriter objX = new XmlTextWriter(Response.OutputStream, Encoding.UTF8);
        objX.WriteStartDocument();
        objX.WriteStartElement("rss");
        objX.WriteAttributeString("version", "2.0");
        objX.WriteElementString("title", "News");
        objX.WriteElementString("link", "http://news.ca/news.aspx");
        objX.WriteElementString("description", "The latest headlines");
        objX.WriteElementString("copyright", "(c)2009, News Club, All rights reserved.");
        objX.WriteElementString("ttl", "5");
        SqlConnection objConnection = new SqlConnection("MyConnString");
        objConnection.Open();
        string sql = "SELECT TOP 5 Title, Description, ArticleID, DatePulished FROM articles ORDER BY DatePublished DESC";
        SqlCommand objCommand = new SqlCommand(sql, objConnection);
        SqlDataReader objReader = objCommand.ExecuteReader();
        while (objReader.Read())
        {
            objX.WriteStartElement("item");
            objX.WriteElementString("title", objReader.GetString(0));
            objX.WriteElementString("description", objReader.GetString(1));
            objX.WriteElementString("link", ("http://news.ca/GetArticle.aspx?id=" + objReader.GetInt32(2).ToString()));
            objX.WriteElementString("pubDate", objReader.GetDateTime(3).ToString("R"));
            objX.WriteEndElement();
        }
        objReader.Close();
        objConnection.Close();
        objX.WriteEndElement();
        objX.WriteEndElement();
        objX.WriteEndDocument();
        objX.Flush();
        objX.Close();
        Response.End();
    }
</script>

3 个答案:

答案 0 :(得分:3)

SqlConnection objConnection = new SqlConnection("MyConnString");

这是编译器遇到困难的一行。您是否用“MyConnString”替换了您的实际连接字符串,或者MyConnString是否应该是页面上的字符串值?

我的应用程序中有类似的代码行:

SqlConnection oConn= new SqlConnection(_connString);

您也可以从Web.Config

获取此值
SqlConnection oConn = new SqlConnection(ConfigurationManager.ConnectionStrings["MyConnString"].ConnectionString);

答案 1 :(得分:0)

SqlConnectionconnectionString视为web.config中的引用。或者仔细检查web.config

答案 2 :(得分:0)

正如例外所说,你在这一行上有一个错误:

SqlConnection objConnection = new SqlConnection("MyConnString");

假设您在connectionStings部分的web.config中有连接字符串,则应设置连接字符串:

SqlConnection objConnection = new 
SqlConnection(ConfigurationManager.ConnectionStrings["MyConnString"]);

顺便说一句,您应该添加 System.Configuration 作为项目的参考,使用ConfigurationManager类。