C# 增删改查应用(二)

[亡魂溺海] 提交于 2020-03-05 06:37:10

 

 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;
        }

 

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!