0

I'm trying to fill a class with data from a SQL Server table.

I've done the exact thing before using Java for another application (see below)

public LanguageList getSystemLanguages() throws SQLException 
{
    String sql;
    LanguageList language;
    sql = "Select DISTINCT code_language From CODE";
    prest = conn.prepareStatement(sql);
    rset = prest.executeQuery();
    language = new LanguageList(rset);
    return language;
}

In C# I have so far:

public Incident GetIncidentViaLogID(String LogID)
{
     Incident incident;
     SqlCommand cmd = new SqlCommand();
     cmd.CommandText = "SELECT * FROM dbo.Incident WHERE LogID = @LogID";
     cmd.Connection = conn;
     SqlParameter p1 = cmd.Parameters.Add("@LogID", SqlDbType.VarChar);
     p1.Value = LogID;
     p1.Size = -1;
     cmd.Prepare();

     //Load data into class
}

I've tried numerous things to finish off this method but none have worked. What would be the approach to complete this method?

Thanks!

1
  • In which way do I execute it and return it? Thanks! Commented Jan 18, 2014 at 19:30

2 Answers 2

1

I think that you mean: how do I load this into a class. There are several options for that, but the most used solution is Entity Framework

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

Comments

1

Here is an example that may help you.

let your class is

          class Person
                {
                   public int age;
                   public string name;
                }

Create an object

                Person p1 = null; 

                List<Person> personList = new List<Person>(); //To fill with data retrieve from database 
//Method to fill Class with data


               public void getAllPersonData()
                {

                    try
                    {
                        if (con.State == ConnectionState.Closed)
                        {
                            con.Open();
                        }

                        string query = "SELECT * FROM Persons";

                        SqlCommand cmd = new SqlCommand(query, con);

                        SqlDataReader rdr = cmd.ExecuteReader();

                        while (rdr.Read())
                        {
                            p1 = new Person(); //Create Instances
                            p1.age = rdr.GetInt32(0);
                            p1.name = rdr.GetString(1);
                            personList.Add(p1);                                
                        }
                    }
                    catch (SqlException x)
                    {
                        MessageBox.Show(x.Message);
                    }
                }

Comments

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.