无法在提琴手上显示记录

时间:2015-12-01 06:28:04

标签: c# asp.net rest asp.net-web-api2 fiddler

我使用ASP.net和C#连接到外部SQL Server 2012创建了一个web api。我的连接字符串是myConnection.ConnectionString = "Data Source=./PALLAVI-PC/SQLEXPRESS;Initial Catalog=StudentDB;Integrated Security=True;MultipleActiveResultSets=True;Application Name=EntityFramework";

代码中似乎没有错误。但是当我部署服务并尝试在fiddler上提取记录时,我收到404错误。 GET网址为localhost:xxxxx/api/student/1

发布以下代码:

StudentController.cs

   //api/student/id
    [HttpGet]
     [ActionName("GetStudentByID")]
    public Student Get(int id)
    {
        //SQL Reader
        SqlDataReader reader = null;

        //SQL Connection class
        SqlConnection myConnection = new SqlConnection();

        //creating the connection string            
        myConnection.ConnectionString = "Data Source=./PALLAVI-PC/SQLEXPRESS;Initial Catalog=StudentDB;Integrated Security=True;MultipleActiveResultSets=True;Application Name=EntityFramework";

        //SQL Commands class
        SqlCommand sqlCmd = new SqlCommand();
        sqlCmd.CommandType = System.Data.CommandType.Text;

        //sql query
        sqlCmd.CommandText = "Select * from Students where Roll_Number=" + id + ";";

        sqlCmd.Connection = myConnection;

        //opening the connection
        myConnection.Open();

        //extracting the record
        reader = sqlCmd.ExecuteReader();

        //object of class student
        Student myStudent = null;

        while (reader.Read())
        {
            myStudent = new Student();
            myStudent.Roll_Number = Convert.ToInt32(reader.GetValue(0));
            myStudent.FirstName = reader.GetValue(1).ToString();
            myStudent.LastName = reader.GetValue(2).ToString();
            myStudent.Class = Convert.ToInt32(reader.GetValue(3));
            myStudent.Gender = reader.GetValue(4).ToString();
        }

        return myStudent;

        //close connection
        myConnection.Close();


    }

如果我在连接字符串中将斜杠更改为转发,则会收到错误unrecognized character

1 个答案:

答案 0 :(得分:1)

将您的连接字符串更改为 myConnection.ConnectionString = @"Data Source=PALLAVI-PC\SQLEXPRESS;Initial Catalog=StudentDB;Integrated Security=True;MultipleActiveResultSets=True;Application Name=EntityFramework";