[英]How to Convert DataSet to User Object in C#?
// GetUserByUsername
public DataSet GetUserByUsername(string Username)
{
User _user = null;
using (SqlCommand cmd = new SqlCommand(DBHelper.UserMethod("GetUsername"),con))
{
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@username", Username);
SqlDataAdapter da = new SqlDataAdapter();
DataSet ds = new DataSet();
da.Fill(ds);
}
return ds;
}
In my above method return type is DataSet
, here my dout is if I change the DataType
to User then how to Convert that DataSet ds to User let me show you. 在我上面的方法中,返回类型为
DataSet
,这是我的追求,如果我将DataType
更改为User,那么如何将DataSet ds转换为User让我展示。
For example: 例如:
// GetUserByUsername
public User GetUserByUsername(string Username)
{
User _user = null;
using (SqlCommand cmd = new SqlCommand(DBHelper.UserMethod("GetUsername"),con))
{
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@username", Username);
SqlDataAdapter da = new SqlDataAdapter();
DataSet ds = new DataSet();
da.Fill(ds);
}
return (User)ds;
}
Can I use in this way or is their any other way can I use instead of DataSet, DataTable
? 我可以以这种方式使用还是可以以其他任何方式代替
DataSet, DataTable
?
If you don't want to build the User obj by parsing the results yourself go for the automapper lib OR there is a such a thing as strongly type datasets (IE define a set AS User). 如果您不想自己解析结果来构建User obj,则可以使用automapper lib,或者使用诸如强类型数据集(即定义一个AS用户集)之类的东西。 Its been a long time since I've done that but this will get you started https://msdn.microsoft.com/en-us/library/esbykkzb(v=vs.110).aspx
自从我这样做已经很长时间了,但是这会让您入门https://msdn.microsoft.com/en-us/library/esbykkzb(v=vs.110).aspx
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.