操作数据库 增删改查

1.查询数据库

    /// <summary>
        /// 查询数据
        /// </summary>
        /// <param name="sqlStr">查询语句</param>
        /// <param name="parameter">参数</param>
        /// <returns></returns>
        public static DataTable QueryData(string sqlStr,params SqlParameter [] parameter) {
            try
            {
                using (SqlConnection conn = new SqlConnection())
                {
                    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);
            }
        }

2.更新数据

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

3.删除数据方法

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

4.添加数据

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

5.写个方法调用【查询数据方法】,其他三个方法调用与此类似

public DataTable GetInfo(string id) {
            var sqlStr = "select * from Student where id=@id";
            DataTable table = SqlHelper.QueryData(sqlStr, new System.Data.SqlClient.SqlParameter[] { new System.Data.SqlClient.SqlParameter("@id",id) });
            return table;
        }

 

posted @ 2022-06-09 09:08  宇宙无敌狗蛋  阅读(67)  评论(0编辑  收藏  举报