zoukankan      html  css  js  c++  java
  • 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添加即可,如图:

  • 相关阅读:
    漏斗算法 java
    servlet request参数只能取一次解决方法
    redis的使用
    关于Http协议与TCP协议的一些简单理解
    OSI七层与TCP/IP五层网络架构详解
    linux命令详解——eval
    使用apache benchmark(ab) 测试报错: apr_socket_recv: Connection timed out (110)
    hadoop相关随记
    mesos-master启动失败,报错Failed to load unknown flag 'quorum.rpmsave'
    根目录/缺少执行权限x产生的两种错误
  • 原文地址:https://www.cnblogs.com/hongmaju/p/3616333.html
Copyright © 2011-2022 走看看