我的List<String>
名为record
,其最大Count
为102.我需要提取50个元素并将其添加为查询参数。但是,所有50可能都不存在,如果没有,那么我需要将参数添加为DBNull.Value
。以下工作,但有没有比检查每次索引是否存在更好的方法?
List<String> record = new List<String>(myRecord.ToString().Split(new string[] { "^" }, StringSplitOptions.None));
cmd.CommandText = "INSERT INTO myTable ([Field1], [Field3], [Field7], [Field9]"
+ ") VALUES ("
+ "@p1, @p2, @p3, @p4)"
if (record.ElementAtOrDefault(1) != null)
{
cmd.Parameters.AddWithValue("@p1", record[1]);
}
else
{
cmd.Parameters.AddWithValue("@p1", DBNull.Value);
}
if (record.ElementAtOrDefault(3) != null)
{
cmd.Parameters.AddWithValue("@p2", record[3]);
}
else
{
cmd.Parameters.AddWithValue("@p2", DBNull.Value);
}
if (record.ElementAtOrDefault(7) != null)
{
cmd.Parameters.AddWithValue("@p3", record[7);
}
else
{
cmd.Parameters.AddWithValue("@p3", DBNull.Value);
}
if (record.ElementAtOrDefault(9) != null)
{
cmd.Parameters.AddWithValue("@p4", record[9]);
}
else
{
cmd.Parameters.AddWithValue("@p4", DBNull.Value);
}
....
....repeat this for @p5 thru @p50
....
cmd.ExecuteNonQuery();
cmd.Parameters.Clear();
工作解决方案如下:
var indexes = new List<int> { 1, 2, 4, 5, 10, 19, 20, 21, 22, 23, 24, 25, 26, 27, 32, 33, 35, 36, 38, 39, 44, 45, 46, 49, 51, 52, 53, 54, 55, 56, 57, 58, 62, 64, 65, 66, 67, 68, 69, 70, 71, 72, 79, 80, 81, 82, 83 };
cmd.CommandText = "INSERT INTO myTable ([Field1], [Field3], [Field7], [Field9]"
+ ") VALUES ("
+ "@p1, @p2, @p3, @p4)"
List<String> record = new List<String>(myRecord.ToString().Split(new string[] { "^" }, StringSplitOptions.None));
var records = record.Where((item, index) => indexes.Contains(index)).ToList();
for (int i = 0; i < 5; i++)
{
string paramName = "@p" + (i);
if (i <= records.Count-1) //index starts with a 0
{
cmd.Parameters.AddWithValue(paramName, (object)records[(i)] ?? DBNull.Value);
}
else
{
cmd.Parameters.AddWithValue(paramName, DBNull.Value);
}
}
cmd.ExecuteNonQuery();
cmd.Parameters.Clear();
答案 0 :(得分:2)
如果您按索引访问元素,那么您可以将索引存储到List
:
var indexes = new List<int> { 1, 3, 7, 9 ... };
然后得到你的元素:
var records = record.Where((item,index) => indexes.Contains(index)).ToList();
然后使用for
循环(而不是if语句)
for(int i=1; i<records.Count; i++)
{
string paramName = "@p" + i;
cmd.Parameters.AddWithValue(paramName, records[i] ?? DBNull.Value);
}
答案 1 :(得分:1)
完全没有经过测试,可能包含错误/错别字,但一般来说,我会这样做:
void ParseMyList(string[] myRecord)
{
List<String> record = new List<String>(myRecord.ToString().Split(new string[] { "^" }, StringSplitOptions.None));
Dictionary<int, string> paramLookup = new Dictionary<int, string>();
paramLookup.Add(1, "@p1");
paramLookup.Add(3, "@p2");
paramLookup.Add(7, "@p3");
paramLookup.Add(9, "@p4");
/*
....
....repeat this for @p5 thru @p50
....
*/
cmd.CommandText = "INSERT INTO myTable ([Field1], [Field3], [Field7], [Field9]"
+ ") VALUES ("
+ "@p1, @p2, @p3, @p4)"
foreach (KeyValuePair<int, string> entry in paramLookup)
{
if (record.ElementAtOrDefault(entry.Value))
{
cmd.Parameters.AddWithValue(entry.Value, record[entry.Key]);
}
else
{
cmd.Parameters.AddWithValue(entry.Value, DBNull.Value);
}
}
cmd.ExecuteNonQuery();
cmd.Parameters.Clear();
}