2

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

方法一

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;        
 }

方法二

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());

我怎样才能结合这两种方法来制作更通用的类型..有人对如何做到这一点有任何建议..非常感谢.....

4

3 回答 3

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 + "'")
于 2013-08-22T11:49:55.810 回答
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;
    }
}
于 2013-08-22T12:13:21.430 回答
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];

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

于 2013-08-22T11:55:58.393 回答