usersdb.cs

来自「Professional ASP.NET source code」· CS 代码 · 共 63 行

CS
63
字号
using System;
using System.Data;
using System.Data.SqlClient;
using System.Collections;

namespace IBuyAdventure
{
   public class UsersDB
   {

   private string m_ConnectionString;

   public UsersDB( string dsn ) {
      m_ConnectionString = dsn; 
   }

   public void AddNewUser(string customerName, string password) {

          String insertStatement = "INSERT INTO Accounts (CustomerName, Password) values ('" + customerName + "', '" + password + "')";

          SqlConnection sqlConnection = new SqlConnection(m_ConnectionString);
          SqlCommand myCommand = new SqlCommand(insertStatement, sqlConnection);

          myCommand.Connection.Open();
          myCommand.ExecuteNonQuery();
          myCommand.Connection.Close();
      }      
  
  public bool UserExists(string customerName){
          SqlConnection sqlConnection = new SqlConnection(m_ConnectionString);
          SqlDataAdapter sqlAdapter1 = new SqlDataAdapter("SELECT CustomerName FROM Accounts WHERE CustomerName='"+customerName+"'", sqlConnection);

          DataSet accountDetails = new DataSet();
          sqlAdapter1.Fill(accountDetails, "accountDetails");

          if (accountDetails.Tables[0].Rows.Count < 1) {
            return false;
          }
		  else
		  	return true;
          
}
      
      public bool ValidateLogin(String customerName, String password) {
      
          SqlConnection sqlConnection = new SqlConnection(m_ConnectionString);
          SqlDataAdapter sqlAdapter1 = new SqlDataAdapter("SELECT * FROM Accounts WHERE CustomerName='"+customerName+"'", sqlConnection);

          DataSet accountDetails = new DataSet();
          sqlAdapter1.Fill(accountDetails, "accountDetails");

          if (accountDetails.Tables[0].Rows.Count < 1) {
            return false;
          }
          
          return (String.Compare(password.Trim(), ((String) accountDetails.Tables[0].Rows[0]["Password"]).Trim()) == 0);
      }
   }

}


⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?