C# CS1009:无法识别的转义序列

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17456686/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 09:31:07  来源:igfitidea点击:

CS1009: Unrecognized escape sequence

c#asp.net

提问by Efe

I have created a site with the following connection string.

我使用以下连接字符串创建了一个站点。

I am getting the following error message any help would be really appreciated.

我收到以下错误消息,非常感谢任何帮助。

Compiler Error Message: CS1009: Unrecognized escape sequence
Line 21: ad.DataFile = "D:\Hosting\9372580\html\pearl\Pearl.mdb";

编译器错误消息:CS1009:无法识别的转义序列
Line 21: ad.DataFile = "D:\Hosting\9372580\html\pearl\Pearl.mdb";

my codes:

我的代码:

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.OleDb;

public partial class html_Show_Projinfo : System.Web.UI.Page
{
    OleDbCommand cmd;
    OleDbConnection con = new OleDbConnection();
    OleDbDataReader rd;
    protected void Page_Load(object sender, EventArgs e)
    {
        int id = Convert.ToInt32(Request.QueryString["id"]);
        con.ConnectionString = ConfigurationManager.ConnectionStrings["pearl"].ToString();
        cmd = new OleDbCommand("Select * from Pearl_Projects where ProjectId=" + id, con);
        con.Open();
        rd = cmd.ExecuteReader();
        string ns;
        while (rd.Read())
        {
            Label2.Text = rd["ProjectName"].ToString();
            ns = rd["Shortdes"].ToString();
            if (ns.Length > 541)
            {
                Label1.Text = ns.Substring(0, 541);
            }
            else
            {
                Label1.Text = ns.Substring(0, ns.Length);
            }            

            Label3.Text = rd["Description"].ToString();
            Label4.Text = rd["location"].ToString();
        }
        rd.Close();
        con.Close();

        //con.Open();
        //cmd = new OleDbCommand("Select ProjectId from Pearl_ProjectDetails where DetailId=" + id, con);
        //int j = Convert.ToInt32(cmd.ExecuteScalar());
        //con.Close();

        //con.Open();
        //cmd = new OleDbCommand("Select ProjectName from Pearl_Projects where ProjectId=" + j, con);
        //Label1.Text = cmd.ExecuteScalar().ToString();
        //con.Close();
        if (Label4.Text == "")
        {
            Label4.Visible = false;
            Label5.Visible = false;
        }
        else
        {
            Label4.Visible = true;
            Label5.Visible = true;
        }
        AccessDataSource ad = new AccessDataSource();
        ad.DataFile = "D:\Hosting72580\html\pearl\Pearl.mdb";
        ad.SelectCommand = "SELECT top 3 ProjectId,ProjectName,Status FROM [Pearl_Projects] where Status=no Order by ProjectId Desc";
        DataList1.DataSource = ad;
        DataList1.DataBind();

        AccessDataSource ad1 = new AccessDataSource();
        ad1.DataFile = "D:\Hosting72580\html\pearl\Pearl.mdb";
        ad1.SelectCommand = "SELECT top 3 ProjectId,ProjectName,Status FROM [Pearl_Projects] where Status=yes Order by ProjectId Desc";
        DataList2.DataSource = ad1;
        DataList2.DataBind();
    }
}

采纳答案by Sam I am says Reinstate Monica

escape those \in lines like the following

逃脱那些\像下面这样的行

ad.DataFile = "D:\Hosting72580\html\pearl\Pearl.mdb";


you can either manually escape them all like so

您可以像这样手动将它们全部转义

ad.DataFile = "D:\Hosting\9372580\html\pearl\Pearl.mdb";

or you can make it a literal string

或者你可以把它变成一个文字字符串

ad.DataFile = @"D:\Hosting72580\html\pearl\Pearl.mdb";


the character '\'begins what is called an "Escape Sequence", and it essentially that you're using 2 characters to represent 1(special) character.

该字符'\'以所谓的“转义序列”开头,本质上您使用 2 个字符来表示 1 个(特殊)字符。

for instance, \nis a newline character, \0is null, and \\is \

例如,\n是一个换行符,\0为空,并且\\\

回答by AM_Hawk

You need to use "\\" as "\" is an escape sequence.
http://msdn.microsoft.com/en-us/library/44ezxxy3(v=vs.80).aspx

您需要使用“\\”,因为“\”是转义序列。
http://msdn.microsoft.com/en-us/library/44ezxxy3(v=vs.80).aspx