| The line above is typical for instantiating a SqlCommand object. It takes a string parameter that holds the command you want to execute and a reference to a SqlConnection object. SqlCommand has a few overloads, which you will see in the examples of this tutorial.
上面一行是典型的实例化SqlCommand对象的代码。它使用一个string参数来保存你想要执行的命令以及一个关于SqlConnection对象的引用。SqlCommand具有重载形式,这些形式你将在以后的示例中看到。
Querying Data
查询数据
When using a SQL select command, you retrieve a data set for viewing. To accomplish this with a SqlCommand object, you would use the ExecuteReader method, which returns a SqlDataReader object. We''''ll discuss the SqlDataReader in a future lesson. The example below shows how to use the SqlCommand object to obtain a SqlDataReader object:
当使用SQL的select命令,会得到一组数据集。为了和SqlCommand对象配合使用,你应该使用ExecuteReader方法,它返回一个SqlDataReader对象。我们将在后面的内容讨论SqlDataReader。下面的例子显示了如何使用SqlCommand对象来得到SqlDataReader对象:
// 1. Instantiate a new command with a query and connection SqlCommand cmd = new SqlCommand("select CategoryName from Categories", conn);
// 2. Call Execute reader to get query results SqlDataReader rdr = cmd.ExecuteReader();
上一页 [1] [2] [3] [4] [5] [6] [7] [8] [9] [10] ... 下一页 >> |