如何使用SqlDataReader返回通用元组

时间:2016-02-09 07:49:16

标签: generics tuples sqldatareader

我有以下代码:

public static Tuple<string, long, long> ExecuteSqlText(string sql, string dbName)
{
    var sqlConnection = new SqlConnection("Server=localhost;Integrated security=SSPI;database=" + dbName + "");

    var sqlCommand = new SqlCommand(sql, sqlConnection);

    sqlConnection.Open();

    var reader = sqlCommand.ExecuteReader();

    Tuple<string, long, long> tuple = null;

    while (reader.Read())
    {
        var item1 = reader.GetString(0);
        var item2 = reader.GetInt64(1);
        var item3 = reader.GetInt64(2);
        tuple = Tuple.Create(item1, item2, item3);
    }

    sqlConnection.Close();

    return tuple;
}

我想让这段代码变得通用,这样我就可以将Tuple类型作为泛型参数传递并返回该类型。并根据通用Tuple参数中的类型填充它。

0 个答案:

没有答案