本文整理匯總了C#中System.Data.SqlClient.SqlDataReader.GetOrdinal方法的典型用法代碼示例。如果您正苦於以下問題:C# SqlDataReader.GetOrdinal方法的具體用法?C# SqlDataReader.GetOrdinal怎麽用?C# SqlDataReader.GetOrdinal使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類System.Data.SqlClient.SqlDataReader
的用法示例。
在下文中一共展示了SqlDataReader.GetOrdinal方法的2個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的C#代碼示例。
示例1: ReadGetOrdinal
private static void ReadGetOrdinal(string connectionString)
{
string queryString = "SELECT DISTINCT CustomerID FROM dbo.Orders;";
using (SqlConnection connection =
new SqlConnection(connectionString))
{
SqlCommand command =
new SqlCommand(queryString, connection);
connection.Open();
SqlDataReader reader = command.ExecuteReader();
// Call GetOrdinal and assign value to variable.
int customerID = reader.GetOrdinal("CustomerID");
// Use variable with GetString inside of loop.
while (reader.Read())
{
Console.WriteLine("CustomerID={0}", reader.GetString(customerID));
}
// Call Close when done reading.
reader.Close();
}
}
示例2: Main
//引入命名空間
using System;
using System.Data;
using System.Data.SqlClient;
class UsingColumnOrdinals
{
public static void Main()
{
SqlConnection mySqlConnection =new SqlConnection("server=(local)\\SQLEXPRESS;database=MyDatabase;Integrated Security=SSPI;");
SqlCommand mySqlCommand = mySqlConnection.CreateCommand();
mySqlCommand.CommandText = "SELECT TOP 5 ID, FirstName, LastName FROM employee " +
"ORDER BY ID";
mySqlConnection.Open();
SqlDataReader productsSqlDataReader = mySqlCommand.ExecuteReader();
int idPos = productsSqlDataReader.GetOrdinal("ID");
int firstNamePos = productsSqlDataReader.GetOrdinal("LastName");
int lastNamePos = productsSqlDataReader.GetOrdinal("FirstName");
while (productsSqlDataReader.Read())
{
Console.WriteLine("ID = " + productsSqlDataReader[idPos]);
Console.WriteLine("FirstName = " + productsSqlDataReader[firstNamePos]);
Console.WriteLine("LastName = " + productsSqlDataReader[lastNamePos]);
}
productsSqlDataReader.Close();
mySqlConnection.Close();
}
}