学习三层架构,遇到个问题,求助大牛们
学习三层架构,遇到这个问题,代码检查了,应该没有问题,层之间的映射关系也对着,求助:程序代码:
App.config: <?xml version="1.0" encoding="utf-8" ?> <configuration> <connectionStrings> <add name="ConnectionString" connectionString="Data Source=.\sqlexpress;Initial Catalog=AddressList;Integrated Security=True" providerName="System.Data.SqlClient"/> </connectionStrings> </configuration> SqlhelperDB.cs: using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Data; using System.Data.SqlClient; using System.Configuration; namespace SQLDAL { ///针对SQL Server数据库操作的通用类: public class SqlDbHelper { private static string connString = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString; public static string ConnectionString { get { return connString; } set { connString = value; } } //编写ExecuteDataTable方法,对数据库进行非连接式查询操作: public static DataTable ExecuteDataTable(string commandText, CommandType commandType, SqlParameter[] parameters) { DataTable data = new DataTable(); using (SqlConnection conn = new SqlConnection(connString)) { using (SqlCommand cmd = new SqlCommand(commandText, conn)) { = commandType; if (parameters != null) { foreach (SqlParameter param in parameters) { cmd.Parameters.Add(param); } } SqlDataAdapter da = new SqlDataAdapter(cmd); da.Fill(data); } } return data; } //继续编写俩个重载的ExecuteDataTable方法: public static DataTable ExecuteDataTable(string commandText) { return ExecuteDataTable(commandText, CommandType.Text, null); } public static DataTable ExecuteDataTable(string commandText, CommandType commandType) { return ExecuteDataTable(commandText, commandType, null); } //编写ExecuteReader方法,对数据库进行连接式查询: public static SqlDataReader ExecuteReader(string commandText, CommandType commandType, SqlParameter[] parameters) { SqlConnection conn = new SqlConnection(connString); SqlCommand cmd = new SqlCommand(commandText, conn); = commandType; if (parameters != null) { foreach (SqlParameter param in parameters) { cmd.Parameters.Add(param); } } conn.Open(); return cmd.ExecuteReader(CommandBehavior.CloseConnection); } //继续编写俩个重载ExecuteReader方法 public static SqlDataReader ExecuteReader(string commandText) { return ExecuteReader(commandText, CommandType.Text, null); } public static SqlDataReader ExecuteReader(string commandText, CommandType commandType) { return ExecuteReader(commandText, commandType, null); } //编写ExecuteScalar方法,对数据库进行单个值的检索 public static Object ExecuteScalar(string commandText, CommandType commandType, SqlParameter[] parameters) { object result = null; using (SqlConnection conn = new SqlConnection(connString)) { using (SqlCommand cmd = new SqlCommand(commandText, conn)) { = commandType; if (parameters != null) { foreach (SqlParameter param in parameters) { cmd.Parameters.Add(param); } } conn.Open(); result = cmd.ExecuteScalar(); } } return result; } //继续编写俩个重载ExecuteScalar方法: public static Object ExecuteScalar(string commandText) { return ExecuteScalar(commandText, CommandType.Text, null); } public static Object ExecuteScalar(string commandText, CommandType commandType) { return ExecuteScalar(commandText, commandType, null); } //编写ExecuteNonQuery方法,对数据库进行增、删、改操作: public static int ExecuteNonQuery(string commandText, CommandType commandType, SqlParameter[] parameters) { int count = 0; using (SqlConnection conn = new SqlConnection(connString)) { using (SqlCommand cmd = new SqlCommand(commandText, conn)) { = commandType; if (parameters != null) { foreach (SqlParameter param in parameters) { cmd.Parameters.Add(param); } } conn.Open(); count = cmd.ExecuteNonQuery(); } } return count; } //继续编写俩个重载ExecuteNonQuery方法: public static int ExecuteNonQuery(string commandTex) { return ExecuteNonQuery(commandTex, CommandType.Text, null); } public static int ExecuteNonQuery(string commandText, CommandType commandType) { return ExecuteNonQuery(commandText, commandType, null); } } } BLL层: using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace BLL { public class User { SQLDAL.User user = new SQLDAL.User(); public bool Login(string userName, string userPwd) { return user.Login(userName,userPwd); } public bool Update(Model.User model) { return user.Update(model); } } }
SQLDA层:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
namespace SQLDAL
{
//数据访问类:User
public partial class User
{
public User()
{ }
#region Method
//判断用户名、密码是否正确:
public bool Login(string userName, string userPassword)
{
StringBuilder strSql = new StringBuilder();
strSql.Append("select count(1) from [User] ");
strSql.Append(" where UserName=@UserName and Password=@UserPassword");
SqlParameter[] parameters = {
new SqlParameter("@UserName",SqlDbType.NVarChar,50),
new SqlParameter("@UserPassword",SqlDbType.NVarChar,50)
};
parameters[0].Value = userName;
parameters[1].Value = userPassword;
int n = Convert.ToInt32(SqlDbHelper.ExecuteScalar(strSql.ToString(), CommandType.Text, parameters));
if (n == 1)
{
return true;
}
else
{
return false;
}
}
}