WebService連接SQL Server建立多用戶登錄驗證服務

之前寫過一篇文章是對於單用戶移動客戶端登錄驗證程序,主要是闡述一下移動客戶端如何調用webservice,但對於一個正式應企業級應用程序,用戶不可能只用一個用戶(一個用戶用得着登錄驗證碼!!!自找麻煩把),

客戶端調用請參考:http://blog.csdn.net/u012239760/article/details/17038589

所以添加多組用戶信息是很有必要的,這裏採用的數據庫選擇了SQL Server 2008 R2,數據庫的建立工程很簡單,

打開數據庫軟件新建數據庫(這裏的數據庫名稱是UserInfo),並右擊你所建立的數據庫,一次選擇任務—導入數據,可以將用戶信息導入到新建的數據庫中,同時通過數據庫查詢語言  select * from UserInfo  可以查看導入數據是否成功,由於這是測試,隨意選擇了四組用戶信息(只有用戶名和相應的密碼),



數據庫建設好後,就可以進行webservice的建立了,在VS中新建一個webservice網站,首先連接數據庫,打開VS中數據庫資源管理器窗口,右擊 數據連接—添加連接  打開添加連接對話框,並按照步驟一次填寫內容,選擇服務器名稱(這個要與你所建立的數據庫所在服務器對應,不然找不到自己的數據庫),根據需要是否需要身份驗證,並連接到數據庫,之後就要進行代碼的編寫了。

新建一個分析數據庫的類,DBConnection.cs


namespace WebService1
{
    public class DBConnection:IDisposable
    {
        public static SqlConnection sqlCon;
        String serverStr = "Data Source=localhost;Initial Catalog=Test;Persist Security Info=True;User ID=sa;Password=123";
        //構造默認函數
        public DBConnection()
        {
            if (sqlCon == null)
            {
                sqlCon = new SqlConnection();
                sqlCon.ConnectionString = serverStr;
                sqlCon.Open();           
            }           
        }
        //關閉、銷燬函數,相當於close();
        public void Dispose()
        {
            if(sqlCon!=null)
            {
                sqlCon.Close();
                sqlCon = null;
            }
        }
        //查看數據庫連接狀態
        public Boolean SqlState()
        {
            switch (sqlCon.State)
            {
                case System.Data.ConnectionState.Connecting:
                    return true;
                case System.Data.ConnectionState.Broken:
                    return false;
                case System.Data.ConnectionState.Closed:
                    return false;
                default:
                    break;               
            }
            return true;
        }

        /// <summary>
        /// 檢測用戶和密碼是否正確
        /// </summary>
        /// <param name="name"></param>
        /// <param name="password"></param>
        /// <returns>string</returns>
        public string userCheck(string user_name,string user_password)
        {

            String sqlStr = "select * from UserInfo where user_name='" + user_name.Trim() + "' and user_pawd='" + user_password.Trim() + "'";
            SqlCommand cmd = new SqlCommand(sqlStr, sqlCon);
           
            SqlDataReader sdr = cmd.ExecuteReader();
            sdr.Read();
            if (sdr.HasRows)
            {
                sdr.Close();
                return "user_check_yes";
            }
            else
            {
                sdr.Close();
                return "user_check_no";
            }        

        }
        /// <summary>
        /// 增加一條用戶信息,用戶註冊
        /// </summary>
        /// <param name="user_name"></param>
        /// <param name="user_password"></param>
        /// <returns>string</returns>
        public string userInsert(string user_name, string user_password)
        {
            String sqlStr = "select * from UserInfo where user_name='" + user_name.Trim() + "' and user_pawd='" + user_password.Trim() + "'";
            SqlCommand cmd = new SqlCommand(sqlStr, sqlCon);

            SqlDataReader sdr = cmd.ExecuteReader();
            sdr.Read();
            if (sdr.HasRows)
            {
                return "user_insert_existed";
            }
            else
            {
                sdr.Close();
                string insertStr = "insert into UserInfo(user_name,user_pawd) values ('" + user_name + "','" + user_password + "')";
                SqlCommand sqlCmd = new SqlCommand(insertStr, sqlCon);//定義OleDbCommnad對象並連接數據庫
                sqlCmd.ExecuteNonQuery();//執行插入語句
                sqlCon.Close();//關閉對象並釋放所佔內存空間  
                sqlCon.Dispose();
                return "user_insert_ok";
            }

        }

        /// <summary>
        /// 刪除用戶信息
        /// </summary>
        /// <param name="user_name"></param>
        /// <param name="user_password"></param>
        /// <returns>string</returns>
        public string userDelete(string user_name, string user_password)
        {
            String sqlStr = "select * from UserInfo where user_name='" + user_name.Trim() + "' and user_pawd='" + user_password.Trim() + "'";
            SqlCommand cmd = new SqlCommand(sqlStr, sqlCon);

            SqlDataReader sdr = cmd.ExecuteReader();
            sdr.Read();
            if (sdr.HasRows)
            {
                sdr.Close();
                string deleteStr = "delete from UserInfo where user_name='" + user_name+"'";
                SqlCommand sqlCmd = new SqlCommand(deleteStr, sqlCon); 
                cmd.ExecuteNonQuery(); 
                cmd.Dispose(); 

                return "user_insert_ok";
            }
            else
            {
                return "user_delete_unexist";
            }        
        }

    }
}

在該類中,對數據庫的處理有三種,查詢,插入(用於註冊新用戶),刪除,看需要進行調用


然後是在WebService1.asmx.cs中調用(這裏只寫了用戶查詢一種方法,用於測試)

 public class Service1 : System.Web.Services.WebService
    {
        DBConnection dbOperation = new DBConnection();
        String result="";
        [WebMethod]
        public string HelloWorld()
        {
            return "Hello World";
        }

        [WebMethod(Description = "查詢")]
        public int login(string user_name, string user_password)
        {
            result = dbOperation.userCheck(user_name, user_password);
            if (result == "user_check_yes")
            {
                return 1;
            }
            else if (result == "user_check_no")
            {
                return 2;
            }
            else
            {
                return 0;
            }
        }

        //用戶註冊  insert(string,string)

        //用戶刪除  delete(string,string)      
    }



然後就可以運行測試了,測試結果:


 


正常運行就可以部署到IIS進行客戶端調用了


客戶端與服務器端進行登錄驗證的小程序到此告一段落,總會用到的···




發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章