C# 在MFC应用程序中尝试使用Sql存储过程

C# 在MFC应用程序中尝试使用Sql存储过程,c#,.net,mfc,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) {

我是.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应用程序项目中使用上述代码。谁能帮帮我吗。提前感谢。

将此代码移植到MFC非常容易。看看
CDatabase
CRecordset
类。您还可以找到以下有用的文章:

这是一个迟来的回复,但它可能对学习mfc DB连接的人有用

最近我有机会学习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;
}

如果您有任何疑问,可以问我,我将尝试解决。

将功能包装在混合模式程序集中(使用C++/CLI),并公开本机接口:“为链接提供上下文:鼓励链接到外部资源,但请在链接周围添加上下文,以便您的其他用户了解它是什么以及为什么存在。始终引用重要链接中最相关的部分,以防无法访问目标站点或永久脱机。我在2020中添加了这个评论,当这真的帮助我时,使用Visual C++ 2017和Windows Server 2016,运行SQLServer。这是google search前几页中唯一的一个工作示例和博客,它在使用CDATA基类从MFC应用程序中使用CALL执行参数化SQL Server过程时产生了精确的结果。令人惊叹的!