1

I'm implementing connection I implemented methods for Connect and Insert. Those methods are working. But I have problem how to implement getdata() method (get data from database). After I send query to the method and I need to know how to use SqlDataReader.

public String GetData(string _query)
{
     try
     {
         SqlCommand cmd = new SqlCommand(_query, this.dbCon);
         results = cmd.ExecuteReader().ToString();
         return results;
     }
     catch (Exception)
     {
         return "Error";
     }
}

I want to complete this get method, I'm using SQL Server and C#

4 Answers 4

1

Do not reinvent the wheel. Typed DataSets and EF are by far and away the best methods of retrieving and manipulating data in .NET for most scenarios. MS has already written all of what you are trying to write in a MUCH better way than you'll probably ever be able to achieve. The time you spend in learning these two technologies will give you long-lasting benefit. This is more true for you since you're using it against SQL Server.

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

Comments

0

This is a fairly straight forward example you can look at:

public SqlDataReader ExecuteReader(SqlCommand cmd)
        {
            return cmd.ExecuteReader();
        }

Using this method, I could execute the following:

var reader = ExecuteReader(new SqlCommand("SELECT ColumnA, ColumnB FROM Table"));

string ColA = string.empty;
string ColB = string.empty;

while (reader.Read())
       ColA = reader["ColumnA"].ToString();
       ColB = reader["ColumnB"].ToString();

reader.Close();
reader.Dispose();

2 Comments

I want to use this method method for more than one query how to change this to use for several queries
@user2490167 Each time you call ExecuteReader, you canpass in a different SqlCommand. Therefore it can be for whatever query you want and it will return the SqlDataReader for it.
0

Try something like this:

private void TestMethod(string sqlCmd, List<string> myColumns)
{
    try
    {
        SqlDataReader myReader = null;
        SqlCommand    myCommand = new SqlCommand(sqlCmd, myConnection);
        myReader = myCommand.ExecuteReader();
        while(myReader.Read())
        {
            foreach (var col in myColumns)
            {   
               Console.WriteLine(myReader[col].ToString());                   
            }
        }
    }
    catch (Exception e)
    {
        Console.WriteLine(e.ToString());
    }
}

In addition you can take a look at this Article.

3 Comments

I want to use this method method for more than one query how to change this to use for several queries
Extract the SQL command as a parameter so you can call the methode each time with different query.
how to change while(myReader.Read()){}
0

Write a method to get the data reader as below,

private SqlDataReader TestMethod(string sqlCmd, List<string> myColumns)
{
 try{
     SqlDataReader myReader = null;
     SqlCommand    myCommand = new SqlCommand(sqlCmd, myConnection);
     myReader = myCommand.ExecuteReader();
     return myReader;
   }
   catch (Exception e)
   {
     Console.WriteLine(e.ToString());
   }
}

And then just use it as,

SqlDataReader myReader  = TestMethod("your query",youtcolumnlist)
while(myReader.Read())
{

   foreach (var col in myColumns)
   {   
       Console.WriteLine(myReader[col].ToString());                   
   }
}

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.