2015-09-19 28 views
-1

我是新來的.NET和編寫的代碼使用SQL存儲過程和代碼來驗證用戶名和密碼是下面:嘗試使用SQL存儲過程在MFC應用程序

using System; 
using System.Data; 
using System.Data.SqlClient; 

namespace ********* 
{ 
    public static class DBHelper 
    { 

     public static bool ValidateUser(string userID, string password) 
     { 
      bool isCustomerExists = false; 

      string constr = "Data Source = ****-PC\\SQLEXPRESS; Initial Catalog = *******; Integrated Security = True"; 

      using (SqlConnection con = new SqlConnection(constr)) 
      { 
       using (SqlCommand cmd = new SqlCommand("Validate_User")) 
       { 

        cmd.CommandType = CommandType.StoredProcedure; 
        cmd.Parameters.AddWithValue("@UserId", userID); 
        cmd.Parameters.AddWithValue("@Password", password); 

        cmd.Connection = con; 
        con.Open(); 

        SqlDataReader reader = cmd.ExecuteReader(); 


        if (reader.Read()) 
        { 
         if (reader["UserID"] != DBNull.Value) 
         { 
          isCustomerExists = true; 
         } 

        } 
        con.Close(); 
       } 

      } 

      return isCustomerExists; 
     } 



     internal static bool AddNewCustomer(Customer customer) 
     { 
      bool isCustomerCreated = false; 

      try 
      { 
       string constr = "Data Source = *****-PC\\SQLEXPRESS; Initial Catalog = *****; Integrated Security = True"; 

       using (SqlConnection con = new SqlConnection(constr)) 
       { 
        using (SqlCommand cmd = new SqlCommand("InserNewCustomer")) 
        { 

         cmd.CommandType = CommandType.StoredProcedure; 
         cmd.Parameters.AddWithValue("@PFirstName", customer.FirstName); 
         cmd.Parameters.AddWithValue("@PLastName", customer.LastName); 
         cmd.Parameters.AddWithValue("@PLoginID", customer.LoginID); 
         cmd.Parameters.AddWithValue("@PCustomerPassword", customer.CustomerPassword); 
         cmd.Parameters.AddWithValue("@PConfirmCustomerPassword", customer.ConfirmCustomerPassword); 
         cmd.Parameters.AddWithValue("@PBirthday", customer.Birthday); 
         cmd.Parameters.AddWithValue("@PCustomerAddress", customer.CustomerAddress); 
         cmd.Connection = con; 
         con.Open(); 

         SqlDataReader reader = cmd.ExecuteReader(); 

          if (reader.RecordsAffected == 1) 
          { 
           isCustomerCreated = true; 
          } 
         con.Close(); 
        } 

       } 


      } 

      catch (Exception ex) 
      { 
       isCustomerCreated = false; 
      } 

      return isCustomerCreated; 
     } 
    } 
} 

我想用上面的代碼中MFC應用程序項目。任何人都可以幫助我。提前致謝。

+0

將功能封裝在混合模式程序集(使用C++/CLI)中,並公開本機接口。 – IInspectable

回答

0

將此代碼移植到MFC中是非常容易的。看看CDatabaseCRecordset類。您還可以找到以下有用的文章:http://www.codeguru.com/cpp/data/mfc_database/storedprocedures/article.php/c1167/Calling-Stored-Procedures.htm

+0

[我如何寫出一個好答案](http://stackoverflow.com/help/how-to-answer):*「**爲鏈接提供上下文:**鼓勵鏈接到外部資源,但請添加上下文圍繞鏈接,所以你的同行用戶將會知道它是什麼以及它爲什麼在那裏。總是引用重要鏈接中最相關的部分,以防目標站點無法訪問或永久脫機。「* – IInspectable

0

這是一個很晚的回覆,但它對於學習mfc數據庫連接的人可能會有用。

最近我有機會學習MFC和SQL Server存儲過程,我檢查了很多解決方案,發現這一個我與你分享,希望它會有所幫助。

我使用VS2012和SQL Server 2008

我用你的代碼數據,並試圖複製您的方案。 相應地在SQL中創建存儲過程。

bool ClassName::ValidateUser(string userID, string password) 
{ 
    bool isCustomerExists = false; 

    CDatabase database; 

    CString strConn; 

    strConn = L"Data Source = ****-PC\\SQLEXPRESS; Initial Catalog = *******; Integrated Security = True"; 
    // your connection string, i was using ODBC Data source. 


    TRY 
    { 

     database.Open(NULL,0,0,strConn); 
     CRecordset recordset(&database); 
     CString strQuery; 

     strQuery.Format(L"{CALL Validate_User('%s','%s')}",userID,password); 


     recordset.Open(CRecordset::forwardOnly,strQuery,CRecordset::readOnly); 

     if(!recordset.IsBOF()) 
     { 
      isCustomerExists = true; 
     } 

     recordset.Close(); 

     database.Close(); 
    } 
    CATCH(CDBException,e) 
    { 
     MessageBox(L"Exception: "+e->m_strError); 
    } 
    END_CATCH; 
    return isCustomerExists; 
} 

bool ClassName::AddNewCustomer(Customer customer) 
{ 
    bool isCustomerCreated; 
    CDatabase database; 

    CString strConn; 

    strConn = L"Data Source = ****-PC\\SQLEXPRESS; Initial Catalog = *******; Integrated Security = True"; 
    //your connection string, i was using ODBC Data source. 


    TRY 
    { 

     database.Open(NULL,0,0,strConn); 
     CRecordset recordset(&database); 
     CString strQuery; 

     strQuery.Format(L"{CALL InserNewCustomer('%s','%s','%s','%s','%s','%s','%s')}",customer.FirstName,customer.LastName,customer.LoginID,customer.CustomerPassword,customer.ConfirmCustomerPassword,customer.Birthday,customer.CustomerAddress); 

     database.ExecuteSQL(strQuery); 

     isCustomerCreated = true; 
     database.Close(); 
    } 
    CATCH(CDBException,e) 
    { 
     MessageBox(L"Exception: "+e->m_strError); 
     isCustomerCreated = false; 
    } 
    END_CATCH; 
    return isCustomerCreated; 
} 

如果您有任何疑問,您可以問我,我會盡力解決它。

相關問題