将新条目添加到Access表

时间:2011-11-23 22:19:29

标签: database vb.net ms-access

我是编程的新手,我正在尝试将数据库链接到网站。我希望网站允许用户创建用户名(OrgID)和密码(OrgPassword),并在我的数据库表(Organizer)中显示它们。这是我到目前为止的代码,但我无法更新数据库中的信息。有没有人有任何建议?

protected void RegisterUser_CreatedUser(object sender, EventArgs e)
{   

  if (txtUserName.Text != "" && OrgPassword.Text !="")
  {
    string cnnString = "Provider= Microsoft.ACE.OLEDB.12.0; Data Source =C:/Users/codym/Desktop/Fall 2011/Information Systems/Project/CampuSpaceDatabase2.accdb";
    OleDbConnection cnx = new OleDbConnection(cnnString);

    OleDbDataAdapter adapter = new OleDbDataAdapter();
    string cmdText= "SELECT * FROM Organizer";
    OleDbCommand cmd = new OleDbCommand(cmdText, cnx);
    adapter.SelectCommand= cmd;
    adapter.Fill(Organizer);
    Session["Organizer"]= Organizer;
    Organizer= ((DataTable)Session["Organizer"]);
    string orgname = OrgID.Text;
    string orgpass = OrgPassword.Text;
    foreach (DataRow in Organizer.Rows)
    {
      if(row["OrgID"].ToString() == orgname & row["OrgPassword"].ToString() == orgpass)
      {
        errLabel.Text = "Welcome "+ row["OrgID"].ToString();
        return;
      }
      else
      {
        errLabel.Text = "OrgID/Password Invalid";
        return;
      }
    }
  }
}

1 个答案:

答案 0 :(得分:1)

看起来你错过了打开连接:

OleDbConnection cnx = new OleDbConnection(cnnString);
cnx.Open();