从查询结果创建查询字符串
Create Query String From Query Results
在C#
中我想运行一个SQL查询并将结果解析成一个QueryString
传递给网页。在我的测试中,我正在尝试这样做,但我收到
错误
Key already exists
哪里不正确或正确的做法是什么?
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;
}
基本上字典不允许重复键。来自 MSDN - Dictionary:
Every key in a Dictionary must be unique according to
the dictionary's equality comparer.
在您的情况下,您要添加 'employeename'、'employeeaddress' 等作为字典的键。因此,从外观上看,如果您希望拥有超过 1 名员工(或数据行),那么您将收到此错误,对于每 1 名员工,它将尝试再次添加相同的键(员工姓名等)
根据我的评论尝试使用 Tuples such as a List<tuple<string, string>>
. There are various other ways to store data, which contains duplicates such as in a DataTable 或可能在 xml 文档中。这真的取决于你真正掌握它后打算用它做什么。
或者创建您自己的 class 类型 'employee',其中包含您要存储的所有信息(姓名、地址、邮政编码等)的属性,然后创建这些信息的列表,例如 List<employee>
.
在C#
中我想运行一个SQL查询并将结果解析成一个QueryString
传递给网页。在我的测试中,我正在尝试这样做,但我收到
Key already exists
哪里不正确或正确的做法是什么?
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;
}
基本上字典不允许重复键。来自 MSDN - Dictionary:
Every key in a Dictionary must be unique according to the dictionary's equality comparer.
在您的情况下,您要添加 'employeename'、'employeeaddress' 等作为字典的键。因此,从外观上看,如果您希望拥有超过 1 名员工(或数据行),那么您将收到此错误,对于每 1 名员工,它将尝试再次添加相同的键(员工姓名等)
根据我的评论尝试使用 Tuples such as a List<tuple<string, string>>
. There are various other ways to store data, which contains duplicates such as in a DataTable 或可能在 xml 文档中。这真的取决于你真正掌握它后打算用它做什么。
或者创建您自己的 class 类型 'employee',其中包含您要存储的所有信息(姓名、地址、邮政编码等)的属性,然后创建这些信息的列表,例如 List<employee>
.