检查此代码中的错误 [英] Check the error from this code
本文介绍了检查此代码中的错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
请参见以下代码:
我想插入三个值,例如Id,Name,address,但使用此代码,它仅在数据库中插入地址列值.
see the below code:
I want to insert three values, such as Id,Name,address but using this code it insert only address column value in the database.
public static int Dynamicarray(string connectionString, Dictionary<string, object> parameters)
{
using (SqlConnection con = Database.GetConnection())
{
string column_name="";
string value="";
foreach (KeyValuePair<string, object> kvp in parameters)
{
column_name =RemoveSpecialChars(kvp.Key + ",".TrimEnd(','));
value = kvp.Key+",".TrimEnd(',');
}
SqlCommand cmdExecute = new SqlCommand("insert into tbl2 ("+column_name+") values("+value+")",con);
foreach (KeyValuePair<string, object> kvp in parameters)
{
cmdExecute.Parameters.AddWithValue(kvp.Key, kvp.Value);
}
return cmdExecute.ExecuteNonQuery();
}
}
请在这方面帮助我.
在此先谢谢您.
Please help me in this regard.
Thanks in Advance.
推荐答案
如果我们忽略了您正在配置的字符串(请看一下使用StringBuilder,它对这种事情的效率要高得多) ),那么您的问题就在这里:
If we ignore that you are concatentating strings (please, look at using a StringBuilder - it''s a lot more efficient for that kind of thing), then your problem is here:
column_name =RemoveSpecialChars(kvp.Key + ",".TrimEnd(','));
"column_name"字符串始终是最后一个键,后跟一个逗号...
The "column_name" string will always be the last key, followed by a comma...
我认为问题出在这里
I think the problem is here
foreach (KeyValuePair<string,> kvp in parameters)
{
column_name += RemoveSpecialChars(kvp.Key + ",".TrimEnd('',''));
value += kvp.Key+",".TrimEnd('','');
}
这篇关于检查此代码中的错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文