我想返回一个应该包含字符串的数据,在返回数据时手动添加字符串,下面是我正在使用的代码:
static public DataTable GetInfoByCode(string code, string user)
{
string sql = "SELECT aspnet_Users.UserName, " +
"Code.dateUsed" +
"FROM aspnet_Membership INNER JOIN" +
"aspnet_Users ON aspnet_Membership.UserId = aspnet_Users.UserId INNER JOIN"+
"rAccessCode ON aspnet_Users.UserId = rAccessCode.userID INNER JOIN"+
"Code ON rAccessCode.CodeID = Code.accessCodeID"+
"WHERE (Code.accessCode = code)";
MembershipUser userPass = Membership.GetUser(user);
string password = userPass.GetPassword();
SqlDataAdapter da = new SqlDataAdapter(sql, ConnectionString);
DataTable dt = new DataTable();
da.Fill(dt);
return dt; //want to return password field in the dt so that i can link datasource with datagridview and show password and username in gridview
}
我想在dt中添加密码,以便在datagrid视图中检索密码字段,如:
<asp:GridView ID="gvList" runat="server" >
<Columns>
<asp:BoundField DataField="UserName" ReadOnly="True" HeaderText="Username" />
<asp:BoundField DataField="password" ReadOnly="True" HeaderText="Password" />
</Columns>
</asp:GridView>
答案 0 :(得分:2)
如果您希望数据表中只有一行,并且只有一行,则可以进行以下修改:
DataTable dt = new DataTable();
da.Fill(dt);
//Add the password column:
dt.Columns.Add("password", typeof (String));
//set the value:
dt.Rows[0]["password"] = password;
//Now return your datatable
这将为名为password
的DataTable添加一个额外的列,然后为其设置值。
但是,在您的原始示例中,我无法看到您在用户名上过滤的位置。像下面这样的查询应该有帮助:
string sql = "SELECT aspnet_Users.UserName, " +
"Code.dateUsed " +
"FROM aspnet_Membership INNER JOIN" +
"aspnet_Users ON aspnet_Membership.UserId = aspnet_Users.UserId INNER JOIN"+
"rAccessCode ON aspnet_Users.UserId = rAccessCode.userID INNER JOIN"+
"Code ON rAccessCode.CodeID = Code.accessCodeID"+
"WHERE (Code.accessCode = @code)";
using(SqlConnection connection = new SqlConnection("connstring"))
{
using(SqlCommand command = new SqlCommand())
{
command.Connection = connection;
command.CommandType = CommandType.Text;
command.Text = sql;
command.Parameters.AddWithValue("@code", code);
connection.Open();
using(SqlDataReader reader = command.ExecuteReader())
{
dt.Load(reader);
}
}
}
请注意,查询已经过参数化,因此我们只会尝试返回与用户代码匹配的行。如果不存在这样的行,您可能希望满足此要求(例如,如果dt.Rows.Count == 0
)
但是,我同意其他海报,没有理由不将其表示为自定义用户对象 - 您仍然可以将数据绑定到显示器。例如:
public class User{
public string UserName { get; set; }
public string Code { get; set; }
public string PasswordHash { get; set; }
public User()
{
}
}
然后,在加载期间:
User user = new User();
using(...)
{
using(SqlDataReader reader = command.ExecuteReader())
{
if(reader.Read())
{
user.UserName = reader.GetString(reader.GetOrdinal("UserName"));
}
}
}
user.PasswordHash = userPass.GetPassword();
user.Code = code;
其中using(...)是外部使用块的简写。
也没有理由不这样做List<User>
。
答案 1 :(得分:1)
最好返回包含所有所需值的对象,然后将datagrid绑定到此类对象的列表中。但是如果您想坚持使用DataTable解决方案,您可以添加列(或将其添加到下面的示例中进行选择)并使用密码数据填充它。顺便说一句。看来这个查询只返回一行DataTable:
static public DataTable GetInfoByCode(字符串代码,字符串用户) {
string sql = "SELECT aspnet_Users.UserName, " +
"Code.dateUsed, Password as '' " +
"FROM aspnet_Membership INNER JOIN" +
"aspnet_Users ON aspnet_Membership.UserId = aspnet_Users.UserId INNER JOIN"+
"rAccessCode ON aspnet_Users.UserId = rAccessCode.userID INNER JOIN"+
"Code ON rAccessCode.CodeID = Code.accessCodeID"+
"WHERE (Code.accessCode = code)";
MembershipUser userPass = Membership.GetUser(user);
string password = userPass.GetPassword();
SqlDataAdapter da = new SqlDataAdapter(sql, ConnectionString);
DataTable dt = new DataTable();
da.Fill(dt);
if (dt.Rows.Count > 0)
dt.Rows[0].SetField<string>("Password", password);
return dt; //want to return password field in the dt so that i can link datasource with datagridview and show password and username in gridview
}
答案 2 :(得分:1)
如果总是返回一条记录,那么你可以
dt.Columns.Add("Password");
dt.Rows[0]["Password"] = password;
否则你将需要遍历你的记录并分配密码