3

How to set data from SQL Server in C#.

My Class Student:

public class student
{
    public int StudentID { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }
}

And I want to Set Data From SQL

SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["conStr"].ConnectionString);
SqlCommand cmd = new SqlCommand();
cmd.CommandText = "Select * From Student";
cmd.Connection = con;
DataTable datatable = new DataTable();
con.Open();
SqlDataAdapter sqlDataAdapter = new SqlDataAdapter(cmd);
sqlDataAdapter.Fill(datatable);
con.Close();
sqlDataAdapter.Dispose();
foreach (DataRow dr in datatable.Rows)
{
    //Set Data: ex: student[] students=new student[];
    //students=new students{StudentID=dr["ID"],FirstName=dr["FirstName"]}
    // ...
}

And My Code Set Data

namespace WebApi2.myapi
{
public class StudentController : ApiController
{
    student[] students = new student[]
     {
         new student { StudentID = 1, FirstName = "Jishan", LastName = "Siddique" },
         new student { StudentID = 2, FirstName = "Bharat", LastName = "Darji" },
         new student { StudentID = 3, FirstName = "Ravi", LastName = "Mori" },
         new student { StudentID = 4, FirstName = "Jay", LastName = "Singh" }
     };

    public IEnumerable<student> GetStudents()
    {
        return students;
    }
}
}

Finally: I would like 1 result like: student.StudentID = dr ["ID"]; student.FirstName = dr ["FirstName"]

Thank you all watch. Sorry for my bad english.

I'm searched result to google but not found

5
  • 1
    Please don't post code as images. Include your code as formatted text. Also, what is your question here. "Help me", isn't a question. Have a look at How do I ask a good question? and edit your question to help us help you. Commented Dec 11, 2018 at 8:54
  • Yest! I'm fixed the question. Can you help me? Commented Dec 11, 2018 at 8:58
  • Yes! I'm fixed. Thank you for the reminder Commented Dec 11, 2018 at 9:02
  • Is there a reason why you are not using an ORM to do the plumbing code for you? EF is very popular but personally I prefer working with Dapper. Of course, you could create a list of Student and populate it in a loop (but in that case, you might want to use the more lightweight DataReader than a DataSet). Commented Dec 11, 2018 at 9:06
  • Is there a way for my code to work? Commented Dec 11, 2018 at 9:09

1 Answer 1

3

You can do it by getting value from the DataRow:

List<student> students = new List<student>();
foreach (DataRow dataRow in datatable.Rows)
{  

     students.Add(new student(){
         StudentID =dataRow["StudentID"];
         FirstName = dataRow["FirstName"] ;
         LastName = dataRow["LastName"] ;
     })
}

So, students collection will have all your students.

Sign up to request clarification or add additional context in comments.

3 Comments

Severity Code Description Project File Line Suppression State Error IDE1007 The name 'students.Add' does not exist in the current context. WebApi2 C:\Users\obu\Downloads\WebApi2\WebApi2\WebApi2\myapi\StudentController.cs 14 Active
thank you. thank you so much thank you again :) It working!
@LongHoàngNguyễn I am glad to help you!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.