这位怪蜀黍 中午的时光真难熬!还好有你在!

C# 增删改查应用(二)

 

 DBHelper类:

复制代码
 public class DBHelper
    {
        //读取配置文件,连接数据库语句
        public static string strCon = System.Configuration.ConfigurationManager.ConnectionStrings["Family"].ConnectionString;

        /// <summary>
        /// 查询数据
        /// </summary>
        /// <param name="sqlStr">查询语句</param>
        /// <param name="parameter">参数</param>
        /// <returns></returns>
        public static DataTable QueryData(string sqlStr, params SqlParameter[] parameter)
        {
            try
            {
                //实例化连接对象 conn 
                using (SqlConnection conn = new SqlConnection(strCon))
                {
                    conn.Open();
                    SqlCommand cmd = new SqlCommand(sqlStr, conn);
                    DataSet dt = new DataSet();
                    SqlDataAdapter adapter = new SqlDataAdapter();
                    cmd.Parameters.AddRange(parameter);
                    adapter.SelectCommand = cmd;
                    adapter.Fill(dt);
                    conn.Close();
                    return dt.Tables[0];
                }
            }
            catch (Exception ex)
            {
                throw new ApplicationException("查询数据异常" + ex.Message);
            }
        }


        /// <summary>
        /// 添加数据
        /// </summary>
        /// <param name="sqlStr">添加语句</param>
        /// <param name="parameter">参数</param>
        /// <returns></returns>
        public static bool AddData(string sqlStr, params SqlParameter[] parameter)
        {
            try
            {
                using (SqlConnection conn = new SqlConnection())
                {
                    conn.Open();
                    SqlCommand cmd = new SqlCommand(sqlStr, conn);
                    cmd.Parameters.AddRange(parameter);
                    var row = cmd.ExecuteNonQuery();
                    conn.Close();
                    if (row > 0)
                    {
                        return true;
                    }
                    return false;
                }
            }
            catch (Exception ex)
            {
                throw new ApplicationException("添加数据异常" + ex.Message);
            }
        }


        /// <summary>
        /// 更新数据
        /// </summary>
        /// <param name="sqlStr">更新语句</param>
        /// <param name="parameter">参数</param>
        /// <returns></returns>
        public static bool UpdateData(string sqlStr, params SqlParameter[] parameter)
        {
            try
            {
                using (SqlConnection conn = new SqlConnection())
                {
                    conn.Open();
                    SqlCommand cmd = new SqlCommand(sqlStr, conn);
                    cmd.Parameters.AddRange(parameter);
                    var row = cmd.ExecuteNonQuery();
                    conn.Close();
                    if (row > 0)
                    {
                        return true;
                    }
                    return false;
                }
            }
            catch (Exception ex)
            {
                throw new ApplicationException("更新数据异常" + ex.Message);
            }
        }


        /// <summary>
        /// 删除数据
        /// </summary>
        /// <param name="sqlStr">删除语句</param>
        /// <param name="parameter">参数</param>
        /// <returns></returns>
        public static bool DeleteData(string sqlStr, params SqlParameter[] parameter)
        {
            try
            {
                using (SqlConnection conn = new SqlConnection())
                {
                    conn.Open();
                    SqlCommand cmd = new SqlCommand(sqlStr, conn);
                    cmd.Parameters.AddRange(parameter);
                    var row = cmd.ExecuteNonQuery();
                    conn.Close();
                    if (row > 0)
                    {
                        return true;
                    }
                    return false;
                }
            }
            catch (Exception ex)
            {
                throw new ApplicationException("删除数据异常" + ex.Message);
            }
        }




    }
复制代码

调用:

复制代码
 //-------------DBHelperl类调用-----------------------------------------------------------
        /// <summary>
        /// 根据条件查询
        /// </summary>
        /// <param name="UserID">参数</param>
        /// <returns></returns>
        public static DataTable GetInfo(string UserID)
        {
            var sqlStr = "select * from [User] where UserID=@UserID";
            //数据表对象获取方法数据          System.Data.SqlClient.SqlParameter参数获取
            DataTable table = DBHelper.QueryData(sqlStr, new System.Data.SqlClient.SqlParameter[] { new System.Data.SqlClient.SqlParameter("@UserID", UserID) });
            return table;
        }

        /// <summary>
        /// 增加数据
        /// </summary>
        /// <param name="UserlevelID">参数</param>
        /// <param name="UserleverlType">参数</param>
        /// <returns></returns>
        public static bool AddData(string UserlevelID, string UserleverlType)
        {
            var sqlStr = "insert into [level] (UserlevelID, UserleverlType)values(@UserlevelID,@UserleverlType)";
            bool bl = DBHelper.AddData(sqlStr, new System.Data.SqlClient.SqlParameter[] { new System.Data.SqlClient.SqlParameter("@UserlevelID", UserlevelID), new System.Data.SqlClient.SqlParameter("@UserleverlType", UserleverlType) });
            return bl;
        }

        /// <summary>
        /// 修改数据
        /// </summary>
        /// <param name="UserName">参数</param>
        /// <param name="UserSet">参数</param>
        /// <param name="UserID">参数</param>
        /// <returns></returns>
        public static bool UpdateData(string UserName, string UserSet, string UserID)
        {
            var sqlStr = "update [User] set UserName=@UserName, UserSet=@UserSet where UserID=@UserID";
            bool bl = DBHelper.UpdateData(sqlStr, new System.Data.SqlClient.SqlParameter[] {
                new System.Data.SqlClient.SqlParameter("@UserID", UserID),
                new System.Data.SqlClient.SqlParameter("@UserName", UserName),
                new System.Data.SqlClient.SqlParameter("@UserSet", UserSet)});
            return bl;
        }

        /// <summary>
        /// 删除数据
        /// </summary>
        /// <param name="UserID">参数</param>
        /// <returns></returns>
        public static bool DeleteData(string UserID)
        {
            string sqlStr = "Delete From [User] Where UserID=@UserID";
            bool bl = DBHelper.DeleteData(sqlStr, new System.Data.SqlClient.SqlParameter[] { new System.Data.SqlClient.SqlParameter("@UserID", UserID) });
            return bl;
        }
复制代码

 

posted @   蟾宝  阅读(153)  评论(0编辑  收藏  举报
编辑推荐:
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
点击右上角即可分享
微信分享提示