2016-01-13 73 views
0

C#我想运行SQL查询并将结果解析为QueryString以传递到网页。在我的测试,我想这样做,但我得到从查询结果中创建查询字符串

关键的错误已经存在

什么是不正确或什么是做到这一点的正确方法是什么?

private void PrepQuery() 
{ 
    Dictionary<string, string> dictFormValues = new Dictionary<string, string>(); 
    string connectionString = null; 
    SqlConnection cnn; 
    SqlCommand cmd; 
    StringBuilder sql = new StringBuilder(); 
    SqlDataReader reader; 
    connectionString = "Data Source=server;Initial Catalog=database;User ID=;Password="; 
    sql.Append("select employeename, employeeaddress, employeezip, employeecity, employeestate, employeephone "); 
    sql.Append("from abcd "); 
    sql.Append("where validated is not null "); 
    cnn = new SqlConnection(connectionString); 
    try 
    { 
     cnn.Open(); 
     cmd = new SqlCommand(sql.ToString(), cnn); 
     reader = cmd.ExecuteReader(); 
     while (reader.Read()) 
     { 
      dictFormValues.Add("employeename", reader.GetValue(0).ToString()); 
      dictFormValues.Add("employeeaddress", reader.GetValue(1).ToString()); 
      dictFormValues.Add("employeezip", reader.GetValue(2).ToString()); 
      dictFormValues.Add("employeecity", reader.GetValue(3).ToString()); 
      dictFormValues.Add("employeestate", reader.GetValue(4).ToString()); 
      dictFormValues.Add("employeephone", reader.GetValue(5).ToString()); 
      name = reader.GetValue(0).ToString(); 
     } 
     reader.Close(); 
     cmd.Dispose(); 
     cnn.Close(); 
    } 
    bool success = false; 
    success = FormatForWebAPI(); 
    if (success = true; { GenerateEmail(); 
    if (sucess = false;) { ShowErrorOnScreen(); } 
} 

private void FormatForWebAPI(); 
{ 
    string strEndpointURL = string.Format("http://requestb.in/pm9rstuv/"); 
    System.Web.Script.Serialization.JavaScriptSerializer json = new System.Web.Script.Serialization.JavaScriptSerializer(); 
    foreach (var d in dictFormValues) { strPostData += d.Key + "=" + Server.UrlEncode(d.Value) + "&"; } 
    strPostData += "hs_context="; 
    System.Net.HttpWebRequest r = (System.Net.HttpWebRequest)System.Net.WebRequest.Create(strEndpointURL); 
    r.Method = "POST"; 
    r.Accept = "application/json"; 
    r.ContentType = "application/x-www-form-urlencoded"; 
    r.ContentLength = strPostData.Length; 
    r.KeepAlive = false; 
    using (System.IO.StreamWriter sw = new System.IO.StreamWriter(r.GetRequestStream())) 
    { 
      try { sw.Write(strPostData); } 
      catch { return false; } 
    } 
    return true; 
} 
+1

看起来你的数据中有重复。 Dictionarys不允许重复键。为什么你要使用字典而不是可能是元组列表的任何特定原因(请参阅:http://www.dotnetperls.com/tuple)? – sr28

+0

@FunFlyWhiteGuy你错过了一个'catch'块 – MethodMan

+0

你循环的结果,所以如果有n行你尝试添加n个关键字的名字不是'employeename'。请参阅[如何在C#中为URL创建查询字符串?](http://stackoverflow.com/questions/829080/how-to-build-a-query-string-for-a-url-in-c) –

回答

0

基本上字典不允许重复的键。从MSDN - Dictionary

在字典每个键必须根据 字典的相等比较器是唯一的。

在你的情况下,你要添加'employeename','employeeaddress'等作为你的字典的关键字。所以从外观上看,如果你期望有超过1名员工(或数据行),那么你会得到这个错误,因为对于每个1,它会尝试并再次添加相同的键(雇用等等)

根据我的评论尝试使用Tuples,如List<tuple<string, string>>。存储数据的方式有多种,其中包含重复项,如DataTable或可能位于xml文档中。这真的取决于你打算如何处理它。

或者创建一个类型为'employee'的类,其中包含要存储的所有信息(名称,地址,邮编等)的属性,然后创建一个列表,如List<employee>

+0

我想返回值并将它们格式化为一个查询字符串,这样我就可以发送到一个网页API –

+1

@FunFlyWhiteGuy - 我可能会去创建自己的雇员班,然后创建一个列表。 – sr28