Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/281.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
c#,使用动态查询_C#_Sql_Dynamicquery - Fatal编程技术网

c#,使用动态查询

c#,使用动态查询,c#,sql,dynamicquery,C#,Sql,Dynamicquery,如何在C#中使用动态查询?从我搜索的内容来看,它与我们使用带有参数的SqlCommand来防止sql注入(下面的示例)时的情况类似 我是c#的新手,我想知道我如何才能做到这一点,提前谢谢。基本上只是基于一组条件构建字符串sqlQuery,并确保设置了适当的参数。例如,下面是一些psuedo-C(未测试bug): 实体框架涉及到一些设置,但根据我的经验,语法更容易理解,您的生产率也会提高。希望这能让你走上正轨 如果您的团队中有人出于性能原因反对使用LINQ,则LINQ to Entite还可以映射

如何在C#中使用动态查询?从我搜索的内容来看,它与我们使用带有参数的SqlCommand来防止sql注入(下面的示例)时的情况类似


我是c#的新手,我想知道我如何才能做到这一点,提前谢谢。

基本上只是基于一组条件构建字符串sqlQuery,并确保设置了适当的参数。例如,下面是一些psuedo-C(未测试bug):

实体框架涉及到一些设置,但根据我的经验,语法更容易理解,您的生产率也会提高。希望这能让你走上正轨

如果您的团队中有人出于性能原因反对使用LINQ,则LINQ to Entite还可以映射SQL存储过程:

或者,如果您必须在C代码中编写自定义查询,实体框架中也允许这样做:


基本上,只需根据一组条件构建字符串sqlQuery,并确保设置了适当的参数。例如,下面是一些psuedo-C(未测试bug):

实体框架涉及到一些设置,但根据我的经验,语法更容易理解,您的生产率也会提高。希望这能让你走上正轨

如果您的团队中有人出于性能原因反对使用LINQ,则LINQ to Entite还可以映射SQL存储过程:

或者,如果您必须在C代码中编写自定义查询,实体框架中也允许这样做:


最好问一下如何解决您认为动态查询可以解决的问题。我有一个项目,我必须管理ERP中的数据,我被告知使用动态查询编辑数据。我在下面发布了一个答案,建议如何扩展上面的示例,以根据特定变量是空字符串或空白。我还编写了一个使用实体框架的更现代的示例。最好是询问如何解决您认为动态查询将解决的问题。我有一个项目,我必须管理来自ERP的数据,我被告知使用动态查询来编辑数据。我在下面发布了一个答案,以建议如何扩展上面的示例来构建基于特定变量是空字符串还是空白的动态查询。我还使用实体框架编写了一个更现代的示例。
using (SQLiteConnection DB_CONNECTION = new SQLiteConnection(connectionString))
        {
            DB_CONNECTION.Open();
            string sqlquery = "UPDATE table SET Name =@Name, IsComplete=@IsComplete WHERE Key =@Key;";
            int rows = 0;
            using (SQLiteCommand command = new SQLiteCommand(sqlquery, DB_CONNECTION))
            {
                SQLiteParameter[] tableA = { new SQLiteParameter("@Key", todo.Key), new SQLiteParameter("@Name", table.Name), new SQLiteParameter("@IsComplete", table.IsComplete) };
                command.Parameters.AddRange(tableA);
                rows = command.ExecuteNonQuery();
            }
            DB_CONNECTION.Close();
            return (rows);
        }
//Set to true, so our queries will always include the check for SomeOtherField.
//In reality, use some check in the C# code that you would want to compose your query.
//Here we set some value we want to compare to.
string someValueToCheck = "Some value to compare";

using (SQLiteConnection DB_CONNECTION = new SQLiteConnection(connectionString))
{
    DB_CONNECTION.Open();
    string sqlquery = "UPDATE MyTable SET Name =@Name, IsComplete=@IsComplete WHERE Key =@Key";

    //Replace this with some real condition that you want to use.
    if (!string.IsNullOrWhiteSpace(someValueToCheck))
    {
        sqlquery += " AND SomeOtherField = @OtherFieldValue"
    }

    int rows = 0;
    using (SQLiteCommand command = new SQLiteCommand(sqlquery, DB_CONNECTION))
    {
        //Use a list here since we can't add to an array - arrays are immutable.
        List<SQLiteParameter> tableAList = {
            new SQLiteParameter("@Key", todo.Key),
            new SQLiteParameter("@Name", table.Name),
            new SQLiteParameter("@IsComplete", table.IsComplete) };

        if (!string.IsNullOrWhiteSpace(someValueToCheck)) {
            //Replace 'someValueToCheck' with a value for the C# that you want to use as a parameter.
            tableAList.Add(new SQLiteParameter("@OtherFieldValue", someValueToCheck));
        }

        //We convert the list back to an array as it is the expected parameter type.
        command.Parameters.AddRange(tableAList.ToArray());
        rows = command.ExecuteNonQuery();
    }
    DB_CONNECTION.Close();
    return (rows);
}
//The context you have setup for the ERP database.
using (var db = new ERPContext()) 
{ 

    //db is an Entity Framework database context - see 
    //https://msdn.microsoft.com/en-au/data/jj206878.aspx
    var query = db.MyTable
        .Where(c => c.Key == todo.Key);

    if (!string.IsNullOrWhiteSpace(someValueToCheck))
    {
        //This where is used in conjunction to the previous WHERE,
        //so it's more or less a WHERE condition1 AND condition2 clause.
        query = query.Where(c => c.SomeOtherField == someValueToCheck);
    }

    //Get the single thing we want to update.
    var thingToUpdate = query.First();

    //Update the values.
    thingToUpdate.Name = table.Name;
    thingToUpdate.IsComplete = table.IsComplete;

    //We can save the context to apply these results.
    db.SaveChanges();

}