27

i have just started learning C# and i can write data to the database without a problem. But i'm having problems with reading, the SQL executes fine but i'm having issues with storing it. How would i store the four columns that should be returned and then show them as a message box? Thanks.

SqlCommand myCommand = new SqlCommand("select * from Requests where Complete = 0", myConnection);
SqlDataReader myReader = myCommand.ExecuteReader();
while (myReader.Read())

Console.WriteLine(myReader["Username"].ToString());
Console.WriteLine(myReader["Item"].ToString());
Console.WriteLine(myReader["Amount"].ToString());
Console.WriteLine(myReader["Complete"].ToString());
4
  • 6
    When using databases in c# you should really have a look at Linq2SQL or Entity Framework. It simplifies a lot. Commented May 14, 2011 at 17:18
  • 1
    @abatishchev Linq2Sql has helped me more often than not. The readability, compile time type checking and the intellisense really helps productivity in my experience. Commented May 14, 2011 at 17:40
  • 4
    @Albin: I agree that ORM is great technology but it isn't necessary to use it everywhere. On newbie level it will confuse more then teach Commented May 14, 2011 at 18:10
  • On top the question has NOTHING to do with teh database but more with how to deal with the data after reading it. Poster is bviously challenged with beginner questions regarding programming. Commented Sep 2, 2011 at 7:31

5 Answers 5

31

One problem is missing braces after the while

while (myReader.Read())
{  // <<- here
    Console.WriteLine(myReader["Username"].ToString());
    Console.WriteLine(myReader["Item"].ToString());
    Console.WriteLine(myReader["Amount"].ToString());
    Console.WriteLine(myReader["Complete"].ToString());
}  // <<- here

if you skip the braces only the first line will be processed in each loop, the rest will be processed after the loop, then myReader is past the last row.

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

1 Comment

@Paul if this answered your question, which appears to be the case, please be sure to mark the response as the answer. This is the check mark symbol by answer posts.
27

Don't forget to use the using(){} block :

using (SqlConnection connection = new SqlConnection(connectionString))
using (SqlCommand command = new SqlCommand("select * from Requests where Complete = 0", connection))
{
    connection.Open();  
    using (SqlDataReader reader = command.ExecuteReader())
    {
        while (reader.Read())
        {
            Console.WriteLine(reader["Username"].ToString());
            Console.WriteLine(reader["Item"].ToString());
            Console.WriteLine(reader["Amount"].ToString());
            Console.WriteLine(reader["Complete"].ToString());
        }
    }
}

Comments

7

Personally I'd write a class with 4 properties (with matching names and types), then use "dapper" (http://code.google.com/p/dapper-dot-net/):

var data = connection.Query<Request>(
    "select * from Requests where Complete = 0").ToList();

With something like:

public class Request {
    public string Username{get;set;}
    ...
    public bool Complete {get;set;}
}

Dapper is free, simple, has parameterisation to avoid SQL-injection, and is very very fast.

Comments

0

I would create an object with properties that holds those values and then pass that object around as needed.

public class YourObjectName
{
   public string Username { get; set; }
   public string Item { get; set; }
   public string Amount { get; set; }
   public string Complete { get; set; }
}

YourObjectName a = new YourObjectName();
a.Username = Reader['Username'].ToString();

1 Comment

Never, ever, ever call a custom type Object :)
0

i know its a bit late but you can use local string variables,or string array or list insert the data in the database there and then call it in your console write line

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.