简单三层构架的学习日志(六)-------精简三层,各层独立文件夹,vs默认的app.config是哪个?

一:这次是进一步的三层构架,不过还是没有业务逻辑层,但有单独的层的文件夹,解决方案资源管理器图如下:

二:operatorDAl.cs代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using HRMSys.Model;
using System.Data.SqlClient;

namespace HRMSys.DAL
{
    public class OperatorDAL
    {
        public void insertUser(Operator op)
        {
            sqlhelper.ExecuteNon(@"insert into T_Operator (Id,UserName,Password) values (newid(),@UserName,@Password)",
                                new SqlParameter ("@UserName",op.UserName),
                                new SqlParameter ("@Password",op.Password));
        }
    }
}
View Code

三:sqlhelper.cs代码

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;

namespace HRMSys.DAL
{
     static class sqlhelper
    {
        
        /// <summary>
        /// 返回受影响的行数
        /// </summary>
       public static readonly  string  constr = 
           ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
        public static int ExecuteNon(string sql,params SqlParameter[] parameter )
        {
            using (SqlConnection conn = new SqlConnection(constr))
            {
                conn.Open();
                using (SqlCommand cmd = conn.CreateCommand())
                {
                    cmd.CommandText = sql;
                    cmd.Parameters.AddRange(parameter);
                    return cmd.ExecuteNonQuery();

                }
            }
 
        }

        /// <summary>
        ///返回结果的首行首列
        /// </summary>
        /// <param name="sql"></param>
        /// <param name="parameter"></param>
        /// <returns></returns>
        public static int ExecuteSca(string sql, params SqlParameter[] parameter)
        {
            using (SqlConnection conn = new SqlConnection(constr))
            {
                conn.Open();
                using (SqlCommand cmd = conn.CreateCommand())
                {
                    cmd.CommandText = sql;
                    cmd.Parameters.AddRange(parameter);
                    int result = (int) cmd.ExecuteScalar();
                    return result;
                }
            }
        }

        /// <summary>
        /// 返回一个数据流
        /// </summary>
        /// <param name="sql"></param>
        /// <param name="parameter"></param>
        /// <returns></returns>
        public static object SqldataRea(string sql, params SqlParameter[] parameter)
        {
            using (SqlConnection conn = new SqlConnection(constr))
            {
                conn.Open();
                using (SqlCommand cmd = conn.CreateCommand())
                {
                    cmd.CommandText = sql;
                    cmd.Parameters.AddRange(parameter);
                    SqlDataReader sdr= sdr=cmd.ExecuteReader();
                    
                    return sdr;
                }
            }
        }

        /// <summary>
        /// 返回一个表
        /// </summary>
        /// <param name="sql"></param>
        /// <param name="parameter"></param>
        /// <returns></returns>
        public static DataTable datatable(string sql, params SqlParameter[] parameter)
        {
            using (SqlConnection conn = new SqlConnection(constr))
            {
                conn.Open();
                using (SqlCommand cmd = conn.CreateCommand())
                {
                    cmd.CommandText = sql;
                    cmd.Parameters.AddRange(parameter);
                    SqlDataAdapter sda = new SqlDataAdapter(cmd);
                    DataSet ds = new DataSet();
                    sda.Fill(ds);
                    return ds.Tables[0];
                }
            }
        }
    }
}
View Code

四:operator.cs代码

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace HRMSys.Model
{
    public class Operator
    {
        public Guid Id { get; set; }
        public string UserName { get; set; }
        public string Password { get; set; }
    }
}
View Code

五:form1.cs代码

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using HRMSys.Model;
using HRMSys.DAL;

namespace HYMSys.UI
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        /// <summary>
        /// 添加一个用户
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void 操作管理员ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Operator op = new Operator();
            op.UserName = "aaa";
            op.Password = "123";

            OperatorDAL opDal = new OperatorDAL();
            opDal.insertUser(op);

        }
    }
}
View Code

六:数据库表的建立

七:做这个实例时出现的一个错误,如图:

八:错误的解决,之前由于vs非人为的报错关闭,导致app.config的丢失,我后来又新建了,app1.config,但vs默认读取app.config导致,将之前的app.config添加即可,如图:

简单三层构架的学习日志(六)-------精简三层,各层独立文件夹,vs默认的app.config是哪个?,,5-wow.com

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。