Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/csharp-4.0/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Tsql Where条件的组合-仍然参数化查询-简洁_Tsql_C# 4.0_Dapper - Fatal编程技术网

Tsql Where条件的组合-仍然参数化查询-简洁

Tsql Where条件的组合-仍然参数化查询-简洁,tsql,c#-4.0,dapper,Tsql,C# 4.0,Dapper,我有一个简洁的问题如下 Public void GetAllCusomers(string CustmoerId, StringFirstName, String LastName, String Gender) { TblCustomer tblCustomer = new TblCustomer(); using (var sqlConnection = new SqlConnection(“DatabaseConncetionString"))

我有一个简洁的问题如下

Public void GetAllCusomers(string CustmoerId, StringFirstName, String LastName, String Gender)
    {
        TblCustomer tblCustomer  = new TblCustomer();

        using (var sqlConnection = new SqlConnection(“DatabaseConncetionString"))
        {
            sqlConnection.Open();
            //tblCustomer = sqlConnection.Query<TblCustomer >("SELECT *  FROM tblCustomer WHERE CustomerId = @CustomerID" AND FirstName = @FirstName……………, new { CustomerID = CustomerId,……………. }).ToList(); 
            tblCustomer = sqlConnection.Query<TblCustomer >("SELECT *  FROM tblCustomer WHERE CustomerId = @CustomerID", new { CustomerID = CustomerId }).ToList();
            sqlConnection.Close();
        }
}
Public void GetAllCusomers(字符串customerid、字符串firstname、字符串LastName、字符串性别)
{
TblCustomer TblCustomer=新TblCustomer();
使用(var sqlConnection=newsqlconnection(“databaseconcetionstring”))
{
sqlConnection.Open();
//tblCustomer=sqlConnection.Query(“从tblCustomer中选择*,其中CustomerId=@CustomerId”和FirstName=@FirstName………..,新的{CustomerId=CustomerId………})。ToList();
tblCustomer=sqlConnection.Query(“从tblCustomer中选择*,其中CustomerId=@CustomerId”,new{CustomerId=CustomerId}”).ToList();
sqlConnection.Close();
}
}
问题是如何构建查询?在上述方法中,用户可以为他希望查询的任何参数提供值。如果参数值为空,则不会在WHERE条件中使用。我将使用WHERE条件中提供的所有参数,并执行AND操作

如果没有Dapper,通过根据提供的参数连接SQL语句来构建动态查询是很容易的。如何在不影响参数化功能的情况下以Dapper构建这些查询

谢谢,,
Ganesh

您的操作与构建动态查询的方式类似。动态构建字符串(基于用户输入),仅根据需要在
Where
子句中包含过滤器。 例如:

就传入参数而言,您可以构造一个对象,该对象包含所有可能的参数以及要传入的值:

new {FirstName = "John", LastName = "Doe"}
new Dictionary<string,object> { {"FirstName", "John" } }
或者,如果您只想传入实际使用的参数,可以构建一个
字典
,其中只包含需要传入的参数:

new {FirstName = "John", LastName = "Doe"}
new Dictionary<string,object> { {"FirstName", "John" } }
新词典{{“FirstName”,“John”}