0

I would like to retrieve top ten data which has highst score value and assign them string array, here is my code but i actualy dont know very well how to do it.

string[] K = new string[10];
using (con3)
{
    con3.Open();               
    SqlCommand cms = con3.CreateCommand();
    cms.CommandText = "SELECT word FROM wordtable where word.score>= 1 LIMIT 10";
    cms.ExecuteNonQuery();

}
con3.Close();
1
  • Is SELECT word return single cell value?? Commented May 11, 2016 at 12:08

2 Answers 2

1

Don't use ExecuteNonQuery if you want to execute a query, use ExecuteReader:

List<string> words = new List<string>();
using(var rd = cms.ExecuteReader())
{
    while(rd.Read())
    {
        string word = rd.GetString(0);
        words.Add(word);
    }
}
string[] K = words.ToArray();  // or fill your array without the list which is more error-prone.

Side-note: since you are using the using-statement the connection will be disposed at the end which will close it implicitly. So con3.Close() after the using is redundant.

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

1 Comment

@dpointttt: i haven't used a sql query because you have already used it. The sql query seems not to be a problem. cms is your SqlCommand with the CommandText="SELECT word FROM wordtable where word.score>= 1 LIMIT 10". So i don't know why i should use word.score in my code. You are only selecting the word-column and not the score-column. Do you want both?
0

Use ExecuteReader to read sql query return values.

For detailed information look following links: Fill an array (or arraylist) from SqlDataReader and Use SqlDataReader and string array

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.