根据搜索的值更新Linq数据库字段 - 密码更改

时间:2013-03-06 13:11:12

标签: sql database vb.net linq

我有一个VB.net LINQ程序,允许用户输入电子邮件地址。根据名为“User_signon”的数据库表检查电子邮件地址,该表包含“电子邮件(PK)”和“密码”字段。单击“btnPassword”时,将根据数据库检查电子邮件地址,并通过电子邮件将随机生成的密码发送到电子邮件地址。问题是我无法弄清楚如何在数据库中更新新的随机密码以连接电子邮件地址。我有以下代码,但我错过了更新密码的方法。

'与数据库建立连接             Dim db As New DatabaseDataClassesDataContext()

        'initialise global variabel to take email string entered in text box
        GlobalVariables.SearchUserEmail = txtEmailAddress.Text

        'initialise global variable to hold email address retrieved from database
        GlobalVariables.CurrentEmailAddress = (From u In db.User_Signons
                                               Where u.Email = SearchUserEmail
                                               Select u.Email).First

       'calls generatepassword method
        Dim stNewPassword As String = GeneratePassword()

请帮忙吗?

2 个答案:

答案 0 :(得分:1)

你可以这样做

Dim user= (From u In db.User_Signons Where     u.Email = SearchUserEmail Select u.Email).First;

user.password =stNewPassword
db.SaveChanges();

就是这样。

答案 1 :(得分:1)

取代仅提取用户名,获取整个用户对象,然后设置密码并保存结果。看看以下是否有帮助:

    'initialise global variabel to take email string entered in text box
    GlobalVariables.SearchUserEmail = txtEmailAddress.Text

    'initialise global variable to hold email address retrieved from database
    Dim user = db.User_Signons.First(Function(u) u.Email = SearchUserEmail)
    GlobalVariables.CurrentEmailAddress = user.UserEmail

   'calls generatepassword method
    Dim stNewPassword As String = GeneratePassword()
    user.Password = stNewPassword
    db.SubmitChanges()