我正在使用以下网络服务并以json格式获得以下结果,但我想在下面提到另一种格式。我希望结合格式的结果意味着通过我的Web服务产生单个数组或数组列表。 我正在使用3种方法 1.登录() 2. GetCetorgy() 3. GetSubCategory()
code Start
===========
public class LoginWebService : ILoginWebService
{
public bool Login(string uname, string pwd)
{
bool bresult;
SqlConnection con = new SqlConnection();
SqlCommand cmd = new SqlCommand();
con.ConnectionString = ConfigurationManager.ConnectionStrings["cn"].ConnectionString;
if (con.State == ConnectionState.Closed)
{
con.Open();
}
cmd.CommandText = "select * from UserList where UserName=@username and Password=@pwd";
cmd.Connection = con;
cmd.Parameters.Add("@username", SqlDbType.VarChar, 50).Value = uname;
cmd.Parameters.Add("@pwd", SqlDbType.VarChar, 50).Value = pwd;
SqlDataReader dr = cmd.ExecuteReader();
if (dr.Read() == true)
{
bresult = true;
}
else
{
bresult = false;
}
con.Close();
return bresult;
}
public String[] GetCategory()
{
var names = new List<string>();
SqlConnection myConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["cn"].ConnectionString);
//SqlConnection myConnection = new SqlConnection(@"Data Source=.\SQLEXPRESS;Initial Catalog=student;User ID=sa;Password=123");
try
{
myConnection.Open();
SqlCommand myCommand = new SqlCommand();
myCommand.Connection = myConnection;
myCommand.CommandText = "Select CategoryName from CategoryTG order by CategoryName ";
//myCommand.Parameters.AddWithValue("@name", nameFilter);
SqlDataReader myReader = myCommand.ExecuteReader();
while (myReader.Read())
{
names.Add(myReader["CategoryName"].ToString());
}
}
catch (Exception ex)
{
//Console.WriteLine(ex.Message);
}
finally
{
myConnection.Close();
}
return names.ToArray();
}
public String[] GetSubCategory()
{
var names = new List<string>();
SqlConnection myConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["cn"].ConnectionString);
//SqlConnection myConnection = new SqlConnection(@"Data Source=.\SQLEXPRESS;Initial Catalog=student;User ID=sa;Password=123");
try
{
myConnection.Open();
SqlCommand myCommand = new SqlCommand();
myCommand.Connection = myConnection;
myCommand.CommandText = "Select CategoryName,SubCategoryName from SubCategoryTG order by CategoryName ";
//myCommand.Parameters.AddWithValue("@name", nameFilter);
SqlDataReader myReader = myCommand.ExecuteReader();
while (myReader.Read())
{
names.Add("Category: " + myReader["CategoryName"].ToString());
names.Add("SubCategory: " + myReader["SubCategoryName"].ToString());
}
}
catch (Exception ex)
{
//Console.WriteLine(ex.Message);
}
finally
{
myConnection.Close();
}
return names.ToArray();
}
}
Code End
=========
I am getting followung result from my service method
======================================================
1. Login = true
2. GetCategory = [
"Eat",
"Entertainment",
"Hobbies",
"Sports"
]
3. GetSubCategory =[
"Category: Eat",
"SubCategory: Pubs",
"Category: Eat",
"SubCategory: Eat out ",
"Category: Entertainment",
"SubCategory: Dance",
"Category: Hobbies",
"SubCategory: Test",
"Category: Sports",
"SubCategory: Tennis",
"Category: Sports",
"SubCategory: Golf",
"Category: Sports",
"SubCategory: Soccer"
]
But i want following result
==============================
{
"Login" "True"
"Categories" |
{
"Category" "Eat",
"Entertainment",
"Hobbies",
"Sports"
}
{"Category" "Eat"
"SubCategory"|
"Eat Out"
"Pubs"
}
{"Category" "Sports"
"SubCategory"|
"Tennis"
"Golf"
}
}
答案 0 :(得分:0)
恕我直言,此代码违反了单一责任原则。即你的登录类应该有一个目的。(SOLID原则http://butunclebob.com/ArticleS.UncleBob.PrinciplesOfOod).If你需要根据SOLID原则重构上述代码所需的可维护和可靠的代码。
如果您需要按原样完成此操作,那么最好使用数据传输对象(DTO)并使用json格式化程序将它们发送到客户端。它比操纵字符串列表更清晰,更恰当。
本文介绍了如何在WCF中轻松支持不同的数据和序列化格式
https://code.msdn.microsoft.com/Supporting-different-data-b0351c9a