连接字符串c#中的错误数据库

时间:2014-03-27 09:58:59

标签: c# sql connection-string

我从网上下载了一个带有数据库的项目。我运行该项目并将其数据库添加到VS 2008 Sp1上的选项卡服务器并拥有SQl服务器2008.

但是当我将新的连接字符串复制到程序时,我收到以下错误消息:

  

无法识别的转义序列

using System;

using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using System.Data;
namespace AzmongirSource
{
class cl 

   {

    static public  string idstudent;
    static public string userteacher;
    static public string nameazmon;
    static public string codeazmon;

    static public string azmon_typ;
    static public string azmon_for_test_type;
    static public string time_limit;
    static public string tedade_soalat;

    static public  SqlConnection connection = new SqlConnection();
    static public  SqlCommand command = new SqlCommand();
    static public  SqlDataReader reader;
    static public  int truequestion = 0, falsequestion = 0, noquestion = 0;


    static public  void creatconnection()
    {

        connection.Close();
        connection.ConnectionString = "Data Source=WINXPX86-BE;AttachDbFilename=D:\payegah\azmoongir_data.MDF;Integrated Security=True";
        connection.Open();
        command.Connection = connection;
    }
   static public  DataTable StudentList()
    {
        creatconnection();
        string query = "Select idstu as[شماره دانشجویی],namestu as [نام],familystu as [نام خانوادگی],standardcode as [کداستاندارد],reshte as [رشته] from tbl_student";
        SqlDataAdapter da = new SqlDataAdapter(query, cl.connection);
        DataTable dtt = new DataTable();
        da.Fill(dtt);
        connection.Close();
        return dtt;

    }

}
}

3 个答案:

答案 0 :(得分:3)

您需要在连接字符串中转义反斜杠,或使用@

connection.ConnectionString = @"Data Source=WINXPX86-BE;AttachDbFilename=D:\payegah\azmoongir_data.MDF;Integrated Security=True";

文档:Escape Sequences

答案 1 :(得分:1)

默认情况下,\被视为转义字符。为了避免在连接string.like

之前添加@
connection.ConnectionString = @"Data Source=WINXPX86-BE;AttachDbFilename=D:\payegah\azmoongir_data.MDF;Integrated Security=True";

答案 2 :(得分:0)

在连接字符串

之前添加@符号
@--->Escape characters

connection.ConnectionString = @"Data Source=WINXPX86-BE;AttachDbFilename=D:\payegah\azmoongir_data.MDF;Integrated Security=True";