我是新來的.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應用程序項目。任何人都可以幫助我。提前致謝。
將功能封裝在混合模式程序集(使用C++/CLI)中,並公開本機接口。 – IInspectable