13

我创建了一个具有以下连接字符串的站点。

我收到以下错误消息,任何帮助将不胜感激。

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

我的代码:

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:\Hosting\9372580\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:\Hosting\9372580\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();
    }
}
4

2 回答 2

23

\像下面这样逃避那些

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

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

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

或者您可以将其设为文字字符串

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

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

例如,\n是换行符,\0是 null,并且\\\

于 2013-07-03T19:38:47.007 回答
2

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

于 2013-07-03T19:40:00.443 回答