结合两个返回两个不同值的方法

时间:2013-08-22 11:44:43

标签: c# c#-4.0 ado.net

您好我有两个方法正在返回两个不同的返回类型的值,如int和string,我在方法内执行查询,传递不同的变量,如下所示

方法1

private string SelectTransactionHistory(int transactionId, ContextObject contextObject)
{
    SqlConnection con;
    SqlCommand cmd;
    con = new SqlConnection(contextObject.ConnectionString);
    con.Open();

    string returnvalue = string.Empty;            
    string selecteQuery = "SELECT Comments 
                           From dbo.TransactionHistory 
                           WHERE TransactionID = '" + transactionId + "'";
    cmd = new SqlCommand(selecteQuery, con);
    returnvalue = (string)cmd.ExecuteScalar();
    con.Close();            
    return returnvalue;        
 }

方法2

private int SelectTransactionHistoryID(string comment, ContextObject contextObject)
{
    SqlConnection con;
    SqlCommand cmd;
    con = new SqlConnection(contextObject.ConnectionString);
    con.Open();

    string query = "SELECT TransactionID  
                    From dbo.TransactionHistory 
                    WHERE Comments = '" + comment + "'";
    cmd = new SqlCommand(query, con);
    int returnvalue = (int)cmd.ExecuteScalar();
    con.Close();
    return returnvalue;
} 

我正在用这样的另一种方法调用这些方法

int transactionId = SelectTransactionHistoryID(comment, GetContext()); 
string commentsreturnValue = SelectTransactionHistory(transactionId, GetContext());

如何将这两种方法结合起来制作更通用的类型.. 任何人都有任何关于如何做到这一点的建议.. 非常感谢.....

3 个答案:

答案 0 :(得分:1)

您可以创建一个方法来使用ado.net执行任何查询,例如:

private static T ExecuteQuery<T>(ContextObject contextObject, string query)
{
    T result;
    using (SqlConnection con = con = new SqlConnection(contextObject.ConnectionString))
    {
        try
        {
            con.Open();
            using (SqlCommand cmd = cmd = new SqlCommand(query, con))
            {
                result = (T)cmd.ExecuteScalar();
            }
        }
        catch
        {
            result = null;
        }
        finally
        {
            con.Close();
        }

    }   
    returnr result;  
}

并传递一个返回单个值的查询(在sql中我们使用TOP 1),如下所示:

var resultComment = ExecuteQuery<string>("SELECT TOP 1 Comments From dbo.TransactionHistory WHERE TransactionID = '" + transactionId + "'");
var resultTransactionId = ExecuteQuery<int>("SELECT TOP 1 TransactionID  From dbo.TransactionHistory WHERE Comments = '" + comment + "'")

答案 1 :(得分:1)

我已将所有基础结构类设置为使用Dapper。但是,您可以使用常规方法替换dapper扩展方法。

基本服务:

public interface IService
{
     T Execute<T>(Func<IDbConnection, T> query);
     void Execute(Action<IDbConnection> query);
}

public sealed class Service : IService
{
    private readonly string _connectionString;

    public Service(string connectionString)
    {
        _connectionString = connectionString;
    }

    private IDbConnection CreateConnection()
    {
        var connection = new SqlConnection(_connectionString);
        connection.Open();

        return connection;
    }

    public T Execute<T>(Func<IDbConnection, T> query)
    {
        using (var connection = CreateConnection())
        {
            return query(connection);
        }
    }

    public void Execute(Action<IDbConnection> query)
    {
        using (var connection = CreateConnection())
        {
            query(connection);
        }
    }
}

DTO:

public class TransactionHistory
{
    public int TransactionID { get; set; }
    public string Comments { get; set; }
}

服务:

public interface ITransactionHistoryService
{
    IEnumerable<TransactionHistory> GetByTransactionId(int transactionId);
    IEnumerable<TransactionHistory> GetByComment(string comment);
}

public sealed class TransactionHistoryService : ITransactionHistoryService
{
    // Note SELECT * is frowned upon. Replace with actual column names.
    private const string GetByTransactionIdQuery =
        "SELECT * FROM dbo.TransactionHistory WHERE TransactionID = @TransactionId";
    private const string GetByCommentQuery =
        "SELECT * FROM dbo.TransactionHistory WHERE Comments = @Comment";
    private readonly IService _service;

    public TransactionHistoryService(IService service)
    {
        _service = service;
    }

    public IEnumerable<TransactionHistory> GetByTransactionId(int transactionId)
    {
        var result = _service.Execute(c =>
                                        c.Query<TransactionHistory>(GetByTransactionIdQuery,
                                                                    new { TransactionId = transactionId }));

        return result;
    }

    public IEnumerable<TransactionHistory> GetByComment(string comment)
    {
        var result = _service.Execute(c =>
                                        c.Query<TransactionHistory>(GetByCommentQuery,
                                                                    new { Comment = comment }));

        return result;
    }
}

答案 2 :(得分:0)

您可以按如下方式创建单个功能 - (未测试)

private string[] SelectTransactionHistory(int transactionId, ContextObject contextObject)
{
        string[] returnValues;
        SqlConnection con;
        SqlCommand cmd;
        SqlDataReader reader;
        con = new SqlConnection(contextObject.ConnectionString);
        con.Open();

        string returnvalue = string.Empty;
        string selecteQuery = "SELECT TransactionID, Comments From dbo.TransactionHistory WHERE TransactionID = '" + transactionId + "'";
        cmd = new SqlCommand(selecteQuery, con);
        reader = cmd.ExecuteReader();
        while(reader.Read())
        {
            returnValues[0] = reader["TransactionID"].ToString();
            returnValues[1] = reader["Comments"].ToString();
        }
        con.Close();
        return returnValues; 
}

然后按如下方式调用它 -

string[] TransactionHistory = SelectTransactionHistory(transactionId, GetContext());
int transactionId = Convert.ToInt32(TransactionHistory[0]);
string commentsreturnValue = TransactionHistory[1];

以上代码未经过测试。但你可以得到一个想法。