试图在MFC应用程序中使用Sql存储过程

时间:2015-09-19 04:40:19

标签: c# .net mfc

我是.net的新手,并编写了使用sql存储过程验证用户ID和密码的代码,代码如下:

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应用程序项目中使用上面的代码。任何人都可以帮助我。提前谢谢。

1 个答案:

答案 0 :(得分: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;
}

如果您有任何疑问,可以问我,我会尽力解决。