c#连接数据库

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

namespace DAL
{
    public sealed  class SQLHelper
    {
        public readonly static string strCon = "server=.;uid=sa;pwd=123456;database=Library";

        public int ExecuteNonQuery(string sql)
        {
            using (SqlConnection Conn = new SqlConnection(strCon))
            {
                try
                {
                    Conn.Open();
                    SqlCommand sqlcom = new SqlCommand(sql, Conn);
                    int n= sqlcom.ExecuteNonQuery();
                    return n;
                }
                catch (SqlException ex)
                {
                    throw new Exception(ex.Message);
                }
                finally
                {
                    Conn.Dispose();
                }
            }
        
        }

        /// <summary>
        /// 登陆存储过程
        /// </summary>
        /// <param name="sql">SQL语句或者存储过程名</param>
        /// <param name="paras">参数数组</param>
        /// <param name="CmdType">命令类型</param>
        /// <returns>
        /// 1、输出参数值
        /// 2、返回值
        /// </returns>
        public int[] User_login(string sql, SqlParameter[] paras, CommandType CmdType)
        {
            using (SqlConnection Conn = new SqlConnection(strCon))
            {
                try
                {
                    Conn.Open();
                    SqlCommand Comm = new SqlCommand();
                    Comm.Connection = Conn;
                    Comm.CommandText = sql;
                    Comm.CommandType = CmdType;

                    //循环遍历添加参数
                    foreach (SqlParameter sp in paras)
                    {
                        Comm.Parameters.Add(sp);
                    }
                    //执行
                    Comm.ExecuteNonQuery();

                    int[] arr = new int[2];

                    //获取输出参数和返回值
                    foreach (SqlParameter sp in paras)
                    {
                        if (sp.Direction == ParameterDirection.Output)
                        {
                            if (sp.Value != DBNull.Value)
                            {
                                arr[0] =Convert.ToInt32(sp.Value);
                            }
                        }
                        else if (sp.Direction == ParameterDirection.ReturnValue)
                        {
                            arr[1] = Convert.ToInt32(sp.Value);
                        }
                    }


                    //返回数组
                    return arr;
                }
                catch (SqlException ex)
                {
                    throw new Exception(ex.Message);
                }
                finally
                {
                    Conn.Dispose();
                }

            }
        
        }


        /// <summary>
        /// 返回一个Table数据集
        /// </summary>
        /// <param name="sql">sql查询语句</param>
        /// <returns>返回Table数据集</returns>
        public DataTable GetTable(string sql)
        {
            using (SqlConnection Conn = new SqlConnection(strCon))
            {
                try
                {
                    SqlDataAdapter sda = new SqlDataAdapter(sql, Conn);
                    DataTable dt = new DataTable();
                    sda.Fill(dt);
                    return dt;
                }
                catch (SqlException ex)
                {
                    throw new Exception(ex.Message);
                }
                finally
                {
                    Conn.Dispose();
                }
                
            }
        }

    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值