数据库连接的保障

版权声明:我的博客都是抄的,抄了太多就懒得注明出处了,请原谅! https://blog.csdn.net/SlowIsFastLemon/article/details/88837944

1 SQLHelper类的编写

在DAL中添加Helper文件夹,并添加SQLHelper类。
在这里插入图片描述

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

using System.Data;
using System.Data.SqlClient;

using System.Configuration;//引入读取配置文件的命名空间

namespace DAL
{
    /// <summary>
    /// 通用数据访问类
    /// </summary>
    public class SQLHelper
    {
        private static string connString = "Server=aaaa\\sqlexpress;DataBase=StudentManageDB;Uid=sa;Pwd=password01!";
        
        /// <summary>
        /// 执行增、删、改方法
        /// </summary>
        /// <param name="sql"></param>
        /// <returns></returns>
        public static int Update(string sql)
        {
            SqlConnection conn = new SqlConnection(connString);
            SqlCommand cmd = new SqlCommand(sql, conn);
            try
            {
                conn.Open();
                return cmd.ExecuteNonQuery();
            }
            catch (Exception ex)
            {
                //将错误信息写入日志...

                throw ex;
            }
            finally
            {
                conn.Close();
            }
        }
        /// <summary>
        /// 执行单一结果(select)
        /// </summary>
        /// <param name="sql"></param>
        /// <returns></returns>
        public static object GetSingleResult(string sql)
        {
            SqlConnection conn = new SqlConnection(connString);
            SqlCommand cmd = new SqlCommand(sql, conn);
            try
            {
                conn.Open();
                return cmd.ExecuteScalar();
            }
            catch (Exception ex)
            {
                //将错误信息写入日志...

                throw ex;
            }
            finally
            {
                conn.Close();
            }
        }
        /// <summary>
        /// 执行结果集查询
        /// </summary>
        /// <param name="sql"></param>
        /// <returns></returns>
        public static SqlDataReader GetReader(string sql)
        {
            SqlConnection conn = new SqlConnection(connString);
            SqlCommand cmd = new SqlCommand(sql, conn);
            try
            {
                conn.Open();
                return cmd.ExecuteReader(CommandBehavior.CloseConnection);
            }
            catch (Exception ex)
            {
                conn.Close();
                //将错误信息写入日志...

                throw ex;
            }
        }
    }
}

2 配置文件的使用

2.1 连接字符串存在的问题

字符串在编译后无法修改。
在这里插入图片描述

2.2 配置文件的使用

在项目的根目录添加App.config文件。
在这里插入图片描述

2.3 使用配置文件常见错误

类型初始化设定项引发异常:
在这里插入图片描述
解决办法:

  • 检查是否已经条件App.config配置文件。
  • 检查用户定义的配置文件“节点名称”和SQLHelper类中的“使用名称是否一致”。

2.4 配置文件总结

关于App.config文件:

  • 程序的配置文件放在应用程序根目录。
  • 属于XML文件。
  • 用途:
    • 保存数据库连接字符串。
    • 保存应用程序常量。

App.config文件使用步骤:

  • 添加App.config。
  • 引用System.Configuration程序集。
  • 引用命名空间System.Configuration。
  • 在App.config定义连接字符串。
  • 在代码中使用连接字符串。

出现错误时:

  • 检查配置文件是否正确添加和定义。
  • 查看定义结点和使用名称是否一致。
  • 配置文件中数据库命名实例需要使用单个"\",而直接在C#代码中需要两个"\\"。

猜你喜欢

转载自blog.csdn.net/SlowIsFastLemon/article/details/88837944