熱點推薦:
您现在的位置: 電腦知識網 >> 編程 >> .NET編程 >> 正文

C#中調用SQL存儲過程實現登錄認證代碼

2022-06-13   來源: .NET編程 
存儲過程如下

  set ANSI_NULLS ONset QUOTED_IDENTIFIER ONGOALTER

  procedure [dbo][security_check](@user_sort int@userID nchar()@userPWD nchar() )

  as

  declare @uid nchar();

  declare @pwd nchar();

  declare @state bit;

  set @state=;

  if( @user_sort=)

  begin

  declare  cursor_temp cursor local for   定義游標

  select 學號密碼   from 學生表  where 學號 = @userID and 密碼 = @userPWD;

  open cursor_temp;      打開游標

  fetch cursor_temp into @uid@pwd;    推進游標

  close cursor_temp;                   關閉游標

  end

  if( @uid =@userID and @pwd=@userPWD )

  begin

  set @state=;

  return @state;

  end;

  else

  begin

  set @state=;

  return @state;

  end;

  C#代碼如下

  using System;
using SystemCollectionsGeneric;
using SystemText;
using SystemData;
using SystemDataSqlClient;

  namespace 密碼驗證
{
    class security
    {
        public static DataSet  check(string uid)
        {
            SqlConnection mySqlConnection = new SqlConnection(server=\\SqlExpress;database=XSXK;integrated security=SSPI);

  mySqlConnectionOpen();

  SqlCommand mySqlCommand = mySqlConnectionCreateCommand();

  mySqlCommandCommandText = select * from 學生表 where 學號 = + uid;

  SqlDataAdapter mySqlDataAdapter = new SqlDataAdapter();

  DataSet myDataSet = new DataSet() ;

       mySqlDataAdapterSelectCommand = mySqlCommand  ;

  mySqlDataAdapterFill (myDataSet 學生表);

  mySqlConnectionClose();

  return myDataSet;

  }

  public static bool secuirty_check(int user_select string uid string pwd)
        {
            int result = ;

  SqlConnection mySqlConnection = new SqlConnection(server=\\SqlExpress;database=XSXK;integrated security=SSPI);

  SqlCommand mySqlCommand = new SqlCommand( security_checkmySqlConnection );

  mySqlCommandCommandType = CommandTypeStoredProcedure;

  ////關鍵部分!!

  SqlParameter param = new SqlParameter();

  paramDirection = SystemDataParameterDirectionReturnValue;

  mySqlCommandParametersAdd(param);

  //
            mySqlCommandParametersAdd( @user_sortSqlDbType Int )Value = user_select ;

  mySqlCommandParametersAdd(@userIDSqlDbTypeNChar )Value = uid ;

  mySqlCommandParametersAdd(@userPWDSqlDbTypeNChar )Value = pwd ;

  //mySqlCommandParameters[@state]Direction = ParameterDirectionReturnValue ;

  mySqlConnectionOpen();

  mySqlCommandExecuteScalar();

  mySqlConnectionClose();

 result = (int)paramValue;
           
            if ( result == )
                return false;
            else
                return true;
           
        }
   
    }
    class Program
    {
        static void Main(string[] args)
        {

  /*
            string uid;
            ConsoleWriteLine(請輸入用戶名);
            uid=ConsoleReadLine();
           
          
            DataSet myDataSet = securitycheck(uid);

  foreach (DataRow thisDataRow in myDataSetTables[學生表]Rows)
            {

  ConsoleWriteLine(學號:\t + thisDataRow[學號]);
                ConsoleWriteLine(姓名:\t + thisDataRow[姓名]);
                ConsoleWriteLine(密碼:\t + thisDataRow[密碼]);
           
            }
            */

  string uid;
            string pwd;
            ConsoleWriteLine(請輸入用戶名);
            uid = ConsoleReadLine();
            ConsoleWriteLine(請輸入密碼);
            pwd = ConsoleReadLine();
            if (securitysecuirty_check( uid pwd))
                ConsoleWriteLine(登錄成功);
            else
                ConsoleWriteLine(用戶名或密碼錯誤!);
            ConsoleRead();
        }
    }

  }


From:http://tw.wingwit.com/Article/program/net/201311/12756.html
    推薦文章
    Copyright © 2005-2022 電腦知識網 Computer Knowledge   All rights reserved.