简体   繁体   中英

Connection class in SQL Server

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#

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.

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 .

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();

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());                   
   }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM