当我只查询一个记录/行时,sqldatareader给出了正确的结果,但是当我查询多行时,它在客户端给出错误。下面是我的代码。请告诉我这里有什么问题。
[WebMethod]
public static string SendMessage(string order)
{
string Server = "alyeyey";
string Username = "apjsjsjs";
string Password = "jjsjsjs";
string Database = "Amhshshs";
string ConnectionString = "Data Source=" + Server + ";";
ConnectionString += "User ID=" + Username + ";";
ConnectionString += "Password=" + Password + ";";
ConnectionString += "Initial Catalog=" + Database;
string query = "select * from optionsRelation where orderNumber = " + order;//+" OR orderNumber = 17";
DataTable dt = new DataTable();
Hashtable sendData = new Hashtable();
try
{
using (SqlConnection conn = new SqlConnection(ConnectionString))
{
using (SqlCommand cmd = new SqlCommand(query, conn))
{
conn.Open();
SqlDataReader dr = cmd.ExecuteReader(CommandBehavior.CloseConnection);
dt.Load(dr);
}
}
//Creating StringBuilder array for storing keys
StringBuilder[] empKeys = new StringBuilder[4];
for (int i = 0; i < empKeys.Length; i++)
{
empKeys[i] = new StringBuilder();
}
//Creating stringbuilder array for storing key values
StringBuilder[] empDetails = new StringBuilder[4];
for (int i = 0; i < empDetails.Length; i++)
{
empDetails[i] = new StringBuilder();
}
//putting datatable data to Keys array i-e empKeys and Values array i-e empDetails array
int inc = 0;
int j = 0;
foreach (DataRow dr in dt.Rows)
{
foreach (DataColumn dc in dt.Columns)
{
empKeys[inc].Append(dc.ColumnName);
inc++;
}
foreach (DataColumn dc in dt.Columns)
{
empDetails[j].Append(dr[dc]);
j++;
}
}
//mapping keys array and values array in hashtable
for (int k = 0; k < empKeys.Length; k++)
{
sendData.Add(empKeys[k].ToString(), empDetails[k].ToString());
}
//sendData.Add("orderNum", order);
JavaScriptSerializer jss = new JavaScriptSerializer();
string output = jss.Serialize(sendData);
return output;
}
catch (Exception ex)
{
return ex.Message + "-" + ex.StackTrace;
}
}
}
答案 0 :(得分:0)
这样的东西?但这并不能解决客户端错误。它确实使用参数而不是字符串连接。
string query = "select * from Customer_Order where orderNumber = @ordernumber";
DataTable dt = new DataTable();
using (SqlConnection conn = new SqlConnection(ConnectionString))
{
using (SqlCommand cmd = new SqlCommand(query, conn))
{
cmd.Parameters.Add(new SqlParameter("ordernumber", ordernumber));
SqlDataAdapter dr = new SqlDataAdapter(cmd);
dr.Fill(dt);
}
}
答案 1 :(得分:0)
错误,但这是因为数据来自服务器端。因为我已经检查过向这个客户端发送不同的值,并且它与这些值一起工作正常。即使我只使用一行查询字符串,如“select * from table where orderNumber = 17”,它工作正常。但在查询多行时给出错误。和“order”是从jquery ajax调用传递的字符串参数(我已经检查过“order”包含正确的字符串,所以没有问题)。我正在使用empKeys和empDetails数组来保存数据表值以将它们转换为哈希表,因为我必须以json格式将数据发送到javascript,并且只有哈希表我成功地执行了此操作。下面是我的整个服务器端代码:
using System.Web.UI.WebControls;
using System.Web.Services;
using System.Data;
using System.Text;
using System.Web.Script.Serialization;
using System.Web.Script.Services;
using System.Collections;
using System.Data.SqlClient;
public partial class orderSearch : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e){}
[WebMethod]
public static string SendMessage(string order)
{
string Server = "alyeyey";
string Username = "apjsjsjs";
string Password = "jjsjsjs";
string Database = "Amhshshs";
string ConnectionString = "Data Source=" + Server + ";";
ConnectionString += "User ID=" + Username + ";";
ConnectionString += "Password=" + Password + ";";
ConnectionString += "Initial Catalog=" + Database;
string query = "select * from optionsRelation where orderNumber = " + order;//+" OR orderNumber = 17";
DataTable dt = new DataTable();
Hashtable sendData = new Hashtable();
try
{
using (SqlConnection conn = new SqlConnection(ConnectionString))
{
using (SqlCommand cmd = new SqlCommand(query, conn))
{
conn.Open();
SqlDataReader dr = cmd.ExecuteReader(CommandBehavior.CloseConnection);
dt.Load(dr);
}
}
//Creating StringBuilder array for storing keys
StringBuilder[] empKeys = new StringBuilder[4];
for (int i = 0; i < empKeys.Length; i++)
{
empKeys[i] = new StringBuilder();
}
//Creating stringbuilder array for storing key values
StringBuilder[] empDetails = new StringBuilder[4];
for (int i = 0; i < empDetails.Length; i++)
{
empDetails[i] = new StringBuilder();
}
//putting datatable data to Keys array i-e empKeys and Values array i-e empDetails array
int inc = 0;
int j = 0;
foreach (DataRow dr in dt.Rows)
{
foreach (DataColumn dc in dt.Columns)
{
empKeys[inc].Append(dc.ColumnName);
inc++;
}
foreach (DataColumn dc in dt.Columns)
{
empDetails[j].Append(dr[dc]);
j++;
}
}
//mapping keys array and values array in hashtable
for (int k = 0; k < empKeys.Length; k++)
{
sendData.Add(empKeys[k].ToString(), empDetails[k].ToString());
}
//sendData.Add("orderNum", order);
JavaScriptSerializer jss = new JavaScriptSerializer();
string output = jss.Serialize(sendData);
return output;
}
catch (Exception ex)
{
return ex.Message + "-" + ex.StackTrace;
}
}
}
答案 2 :(得分:0)
试试这个...我认为这仍然有错误的JSON输出,但它会给你一个想法。请参阅Rick Strahl关于converting a datatable to json的文章。它比我的例子复杂一点。
List<Hashtable> sendData = new List<Hashtable>();
foreach (DataRow dr in dt.Rows)
{
foreach (DataColumn dc in dt.Columns)
{
Hashtable ht = new Hashtable();
ht.Add(dc.ColumnName, dr[dc].ToString());
sendData.Add(ht);
}
}