1. 程式人生 > >C#第十四章示例三

C#第十四章示例三

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data.SqlClient;
namespace ConsoleApplication1
{
    class Program
    {

        private const string str = @"Data Source=.;Initial Catalog=MySchool;Integrated Security=True";

        static void Main(string[] args)
        {
            Program p=new Program();
            string userName="admin";
            string pwd="0";
            string strMsg="用來儲存資訊";
           
            bool y=p.sss(userName,pwd,ref strMsg);

            if (y)
            {
                Console.WriteLine(strMsg);
            }
            else {
                Console.WriteLine(strMsg);
            }
          
            Console.ReadLine();
        
        }
        public bool sss(string userName, string pwd, ref string strMsg)
        {

            SqlConnection c = new SqlConnection(str);
       
         try
         {    
             c.Open();
             string r = "SELECT COUNT(*) FROM [MySchool].[dbo].[Admin] where LoginId='"+userName+"'and LoginPwd='"+pwd+"'";
             Console.WriteLine(r);
             SqlCommand e = new SqlCommand(r,c);
               int i = (int)e.ExecuteScalar();
              if(i>0){
              strMsg="登陸成功,確實有這個資訊";
            return true;
              }
         }
         catch (Exception ex)
         {
             strMsg="登入失敗";
             Console.WriteLine(ex.Message);
             return false;
         }
         finally {
             Console.WriteLine("蘇琳琳大美女"); 
             c.Close();
         }
        //關閉資料庫連線成功
         return true;
      
          
        }
        }
}