Reputation: 3511
I am developing a C# VS2008 / SQL Server website app and am new to the Dictionary class. Can you please advise on best method of accomplishing this? Here is a code snippet:
SqlConnection conn2 = new SqlConnection(connString);
SqlCommand cmd = conn2.CreateCommand();
cmd.CommandText = "dbo.AppendDataCT";
cmd.CommandType = CommandType.StoredProcedure;
cmd.Connection = conn2;
SqlParameter p1, p2, p3;
foreach (string s in dt.Rows[1].ItemArray)
{
DataRow dr = dt.Rows[1]; // second row
p1 = cmd.Parameters.AddWithValue((string)dic[0], (string)dr[0]);
p1.SqlDbType = SqlDbType.VarChar;
p2 = cmd.Parameters.AddWithValue((string)dic[1], (string)dr[1]);
p2.SqlDbType = SqlDbType.VarChar;
p3 = cmd.Parameters.AddWithValue((string)dic[2], (string)dr[2]);
p3.SqlDbType = SqlDbType.VarChar;
}
but this is giving me compiler error:
The best overloaded method match for 'System.Collections.Generic.Dictionary<string,string>.this[string]' has some invalid arguments
I just want to access each value from "dic" and load into these SQL parameters. How do I do this? Do I have to enter the key? The keys are named "col1", "col2", etc., so not the most user-friendly. Any other tips? Thanks!
Upvotes: 21
Views: 73466
Reputation: 46
Here's a piece of code that will do what you're after, I think.. You will have to add in an else statement, but it should get you started.
public void AttachParam(ref DbCommand command, Dictionary<string, string> parameters)
{
try
{
if (parameters.Count > 0)
{
foreach (KeyValuePair<string, string> kvp in parameters)
{
command.Parameters.AddWithValue(kvp.Key, kvp.Value);
}
}
}
catch (Exception ex)
{
throw;
}
}
Upvotes: 3
Reputation: 3522
I can't understand what really you are looking for, so here is my small advice. There is a way to browse dictionary by not requesting items by key. It's a foreach option.
foreach (KeyValuePair<string,string> pair in dic)
{
p. = cmd.Parameters.AddWithValue(dic.Value /*or dic.Key*/, (string)dr[0]);
p.SqlDbType = SqlDbType.VarChar;
// further processing of this parameter
}
Upvotes: 1
Reputation: 117057
If your keys are "col1", "col2", etc then using 0, 1, etc will cause the error you're getting. Try this:
p1 = cmd.Parameters.AddWithValue(dic["col1"], dr[0]);
p1.SqlDbType = SqlDbType.VarChar;
p2 = cmd.Parameters.AddWithValue(dic["col2"], dr[1]);
p2.SqlDbType = SqlDbType.VarChar;
p3 = cmd.Parameters.AddWithValue(dic["col3"], dr[2]);
p3.SqlDbType = SqlDbType.VarChar;
Also, there is no need to cast to string
as the dictionary object will return strings already.
Upvotes: 1
Reputation: 12966
A Dictionary
maps objects of one type into objects of another type. In your case you've got a Dictionary<string, string>
. So if you had it initialised like this:
Dictionary<string, string> dic = new Dictionary<string, string>()
{
{ "key1", "value1" },
{ "key2", "value2" },
{ "key3", "value3" }
};
You'd get the value for key2
by doing
dic["key2"]; // gives "value2"
The Dictionary
is strongly-typed, so there's no need for any casting. As Jon Skeet says, you may be better off with a List<string>
here. You can access these by the integer index.
Upvotes: 30
Reputation: 1500375
You seem to be trying to access a Dictionary<string, string>
by integer index. You can't do that - you have to look up the value by the key, which is a string.
If you don't want to use string keys, why are you using a Dictionary<string, string>
to start with? Could you either use a List<string>
or a Dictionary<int, string>
?
Note that once you have managed to use the indexer appropriately, you won't need to cast to string.
Upvotes: 7