简体   繁体   中英

getting unrecognised escape sequence error

I wanted to store value in datatable by a database comparing it with value from textbox, but I am getting unrecognised escape sequence error near the connection string. I have tried the techniques given link ---> [ CS1009: Unrecognized escape sequence . But this has not worked because I will be getting exception called unrecognized path . Please tell a fix.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Configuration;
using System.Drawing;
using System.Data;

public partial class searchsale : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        try
        {

            SqlConnection conn = new SqlConnection("Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\rdb.mdf;Integrated Security=True;User Instance=True");

            conn.Open();
            string scriptname = TextBox1.Text;
            string accnum = TextBox2.Text;
            string sql = @"select scriptname,accnum,Quantity,price from transac where scriptname = @sn, accnum = @an and transactio = 'Sell'";
            SqlCommand cmd = new SqlCommand();
            cmd.CommandType = CommandType.Text;
            cmd.CommandText = sql;
            cmd.Parameters.AddWithValue("@an", accnum);
            cmd.Parameters.AddWithValue("@sn", scriptname);
            SqlDataReader dr = cmd.ExecuteReader();
            DataTable dt = GetDataTable(sql);
        }
        catch (Exception ex)
        {
            Response.Write("error" + ex.ToString());
        }

    }
    private DataTable GetDataTable (string sql)
    {
        DataTable dt = new DataTable();
        using (SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ToString()))
        {
            SqlDataAdapter da = new SqlDataAdapter(sql, conn);
            da.Fill(dt);
        }
        return dt;
    }
}

Change:

"Data Source=.\\SQLEXPRESS;AttachDbFilename=|DataDirectory|\\rdb.mdf;Integrated Security=True;User Instance=True"

To @"Data Source=.\\SQLEXPRESS;AttachDbFilename=|DataDirectory|\\rdb.mdf;Integrated Security=True;User Instance=True"

Or "Data Source=.\\\\SQLEXPRESS;AttachDbFilename=|DataDirectory|\\\\rdb.mdf;Integrated Security=True;User Instance=True"

Check this link about the escape sequence in C#. The @ in front of a string makes it not do any escape characters.

在连接字符串之前使用逐字@ ,例如

SqlConnection conn = new SqlConnection(@"Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\rdb.mdf;Integrated Security=True;User Instance=True");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM