简体   繁体   English

Dapper.net SQL语法错误,带有DynamicParameters

[英]Dapper.net SQL Syntax Error with DynamicParameters

I'm trying to build a dynamic sql query to update a table in SQL Server, like this: 我正在尝试建立动态sql查询以更新SQL Server中的表,如下所示:

string alan = "SaticiKodu";

DynamicParameters paramStSabit = new DynamicParameters();
string sqlSabit = "UPTADE TBLSTSABIT SET ";

if (alan == "SaticiKodu")
{
    sqlSabit += " SATICI_KODU = @SaticiKodu ,";
    paramStSabit.Add("SaticiKodu", _msk.SaticiKodu);//_msk.SaticiKodu comes from List<T> in foreach loop
}

if (sqlSabit.Contains("="))   // here I check if I have any column update with '='
{
    sqlSabit += "WHERE STOK_KODU = @StokKodu";
    paramStSabit.Add("StokKodu", _msk.StokKodu);
    sqlSabit= sqlSabit.Replace(",WHERE", " WHERE");
    db.Execute(sqlSabit, paramStSabit, transaction: _transaction, commandType: CommandType.Text);
}

I have a list of objects, it has lots of properties but to make example short, here I write only StokKodu and StokAdi. 我有一个对象列表,它具有很多属性,但为了简化示例,在这里我只写StokKodu和StokAdi。 This code throws an error 此代码引发错误

Incorrect syntax at '=' “ =”处的语法不正确

I think this code should work with Dapper. 我认为此代码应与Dapper一起使用。

How can I make this code work, where is my mistake? 如何使此代码起作用,我的错误在哪里? Thanks for the help from now. 感谢您的协助。

All i do is changing 我所做的就是改变

string sqlSabit

to

StringBuilder sqlSabit

and instead of join strings with +=, i used sqlSabit.Append(), now code is working. 我使用sqlSabit.Append()而不是使用+ =连接字符串,现在代码可以正常工作了。 And i tested with string, integer and float typed columns/parameters, no problem. 我用字符串,整数和浮点型列/参数进行了测试,没有问题。

But couldn't understand why StringBuilder solves the problem. 但是不明白为什么StringBuilder解决了这个问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM