开发笔记

数据访问

1.SQLHelper.cs 通用数据访问类

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

namespace DAL
{
    /// <summary>
    /// 通用数据访问类
    /// </summary>
    public class SQLHelper
    {
        private static string connString = ConfigurationManager.ConnectionStrings["connString"].ToString();
        /// <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();
                int result = cmd.ExecuteNonQuery();
                return result;
            }
            catch (Exception ex)
            {
                //写入日志

                throw ex;
            }
            finally
            {
                conn.Close();
            }

            
        }
        /// <summary>
        /// 获取单一结果查询
        /// </summary>
        /// <param name="sql"></param>
        /// <returns></returns>
        public static object GetSingleResult(string sql)
        {
            SqlConnection conn = new SqlConnection();
            SqlCommand cmd = new SqlCommand(sql, conn);
            try
            {
                conn.Open();
                object result = cmd.ExecuteScalar();
                return result;
            }
            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();
            SqlCommand cmd = new SqlCommand(sql,conn);
            try
            {
                conn.Open();
                return cmd.ExecuteReader(CommandBehavior.CloseConnection);
            }
            catch (Exception ex)
            {
                //写入日志
                conn.Close();//如果不发生异常CommandBehavior.CloseConnection 将自动关闭数据库连接
                throw ex;
            }
        }

    }
}

2. 应用程序配置文件(app.config)的访问

1). 引用 System.Configuration

2). using System.Configuration

3). 访问

app.config文件

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <connectionStrings>
    <add name="connString" connectionString="Data Source=.;Initial Catalog=sms;Integrated Security=True"/>
  </connectionStrings>
</configuration>

读取数据库连接字段

private static string connString = ConfigurationManager.ConnectionStrings["connString"].ToString();

 

 

用户登录的实现

1.实现思路:在Program.cs代码中 加入以下代码。首先实例化登陆窗体,如果登陆窗体用户名密码正确则返回一个DialogResult.OK的标记,通过Application.Run调用主窗体。否则退出窗体

FrmLogin objFrmLogin = new FrmLogin();
            DialogResult result = objFrmLogin.ShowDialog();

            if (result == DialogResult.OK)
                Application.Run(new FrmMain()); //调用主窗体
            else
                Application.Exit();

 

 

 

 

 

 

 

赞(0)
未经允许不得转载:TaKaSa » 开发笔记