答案 0 :(得分:0)
请参阅下面的示例。它还显示了如何从C#代码中的StoredProcedure执行中读取记录。 此外,在使用块中启动SQLConnection和SQLCommand对象也是一种很好的做法。
using (var conn = new SqlConnection(cnnString))
using (var cmd = conn.CreateCommand())
{
conn.Open();
cmd.CommandText = "SearchCustomer";
cmd.CommandType = System.Data.CommandType.StoredProcedure;
// Use below line if you want to pass any parameter values to SP.
// cmd.Parameters.AddWithValue("@id", CustomerId);
using (var reader = cmd.ExecuteReader())
{
while(reader.Read())
{
// Read Column based on Column Name. Below sample reads String column
Console.WriteLine(reader.GetString(reader.GetOrdinal("columnName"));
// Read Column based on Column Index. Below sample reads int column
Console.WriteLine(reader.GetInt32(1));
}
}
}