2012-07-19 42 views
1

我想回到一個確定年代,其中應包括一個字符串,返回一個確定年代的時候手動添加一個字符串,下面是我使用的代碼:我要添加密碼返回的數據表和一個字符串

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:

<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

是密碼散列或明文? – 2012-07-19 11:46:21

+0

密碼被散列 – 2012-07-19 12:08:32

+0

你不能以明文形式顯示散列密碼,所以你想顯示什麼? – 2012-07-19 12:10:34

回答

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 

這會在DataTable中增加一個名爲password的列,然後爲其設置值。

但是,在您的原始示例中,我無法看到您在用戶名上進行過濾的位置。類似下面的查詢應有助於在這裏:​​

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; 

其中使用(...)是用於外部使用的塊的簡寫。

也沒有理由爲什麼這也不是List<User>

1

返回將保存所有想要的值的對象會更好,然後將數據網格綁定到這些對象的列表。但是,如果你想堅持使用DataTable解決方案,你可以添加列(或添加它來選擇像我在下面的例子),並填寫密碼數據。順便說一句。似乎這個查詢將返回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 
    } 
1

如果一個記錄總是然後返回你可以做

dt.Columns.Add("Password"); 
dt.Rows[0]["Password"] = password; 

,否則你將不得不遍歷您的記錄,並指定密碼

相關問題