简体   繁体   English

SQL Server中的连接类

[英]Connection class in SQL Server

I'm implementing connection I implemented methods for Connect and Insert . 我正在实现连接,我实现了ConnectInsert方法。 Those methods are working. 这些方法正在起作用。 But I have problem how to implement getdata() method (get data from database). 但是我有一个问题,如何实现getdata()方法(从数据库获取数据)。 After I send query to the method and I need to know how to use SqlDataReader . 在向该方法发送查询之后,我需要知道如何使用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# 我想完成此get方法,我正在使用SQL Server和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. 在大多数情况下,类型化数据集和EF迄今为止是.NET中检索和处理数据的最佳方法。 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. MS已经用您可能无法实现的更好的方式编写了所有您要编写的内容。 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. 这对您来说更是如此,因为您是在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());                   
   }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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