轉自 http://blog.csdn.net/limlimlim/article/details/8626898sql
注意:當SQL語句中出現兩條Select語句,例如:string sql = "select * from Employees;select * from TblClass";
能夠用SqlDataReader的NextResult()來接收多個結果集
使用
do
{
}
while(reader.NextResult())spa
static void Main(string[] args)
{.net
string constr = "Data Source=zxtiger;Initial Catalog=itcastcn;Integrated Security=True";
using (SqlConnection con = new SqlConnection(constr))
{
string sql = "select * from Employees;select * from TblClass";
using (SqlCommand cmd = new SqlCommand(sql, con))
{
con.Open();
using (SqlDataReader reader = cmd.ExecuteReader())
{
do
{
if (reader.HasRows)
{
while (reader.Read())
{
//循環列
for (int i = 0; i < reader.FieldCount; i++)
{blog
//根據索引,獲取列名
//reader.GetName(i);
//獲取當前列的數據類型
string dbType = reader.GetDataTypeName(i);
switch (dbType)
{
case "varchar":
case "nvarchar":
case "char":
case "nchar":
Console.Write(reader.GetString(i) + "\t");
break;
case "int":
Console.Write(reader.GetInt32(i) + "\t");
break;
}
}
Console.WriteLine();
}
}
} while (reader.NextResult()); //當多個結果集的時候能夠調用NextResult索引
}
}
}
Console.WriteLine("ok");
Console.ReadKey();
}cmd