操作数据库之查、增、删、改、统计、排序、混合查询、指定条数、不重复记录

时间:2021-11-27 08:20:34
//---------------------------------------------------数据库删除
/// <summary>
///ShuanChu_JiLu 的摘要说明:根据ID号、数据表名称、字段名称,删除数据库对应ID记录操作。
/// </summary>
public void ShuanChu_JiLu(string BiaoMing,string ZiDuanMing, int _ID)//删除记录
{
DataSet MyData = Socut.Data.ExecuteDataSet("DELETE FROM " + BiaoMing.Trim() + " WHERE " + ZiDuanMing.Trim() + "=" + _ID + "");
}
//---------------------------------------------------数据库读取
/// <summary>
///XianShi_TiaoJian 的摘要说明:根据需数据表名称、字段名称=字段内容,显示数据库所有记录操作。
/// </summary>
public DataSet XianShi_TiaoJian(string BiaoMing, string ZiDuanMing, string NeiRong)//显示记录
{
DataSet Xian_Shi = Socut.Data.ExecuteDataSet("SELECT * FROM " + BiaoMing.Trim() + " WHERE " + ZiDuanMing.Trim() + "='" + NeiRong + "' ");
return Xian_Shi;
}
/// <summary>
///XianShi_LianHe_Pager 的摘要说明:联表混合查询-2个数据表AB名称、字段标头名称AB,字段内容模糊显示数据库记录条数操作。
/// </summary>
public int XianShi_LianHe_Pager(string BiaoMingA, string BiaoMingB, string ZiDuanMingA, string ZiDuanMingB, string NeiRongIN, string NeiRong1, string NeiRong2)//联合查询显示记录
{
int Pager = (int)Socut.Data.ExecuteScalar("SELECT COUNT(*) FROM (SELECT DISTINCT A2.* FROM " + BiaoMingA.Trim() + " A2, " + BiaoMingB.Trim() + " A1 where " + ZiDuanMingA.Trim() + "_QUYU IN (" + NeiRongIN.Trim() + ")and(" + ZiDuanMingA.Trim() + "_JIFAN like '%" + NeiRong1.Trim() + "%') AND (" + ZiDuanMingA.Trim() + "_MINGCHENG like '%" + NeiRong2.Trim() + "%'or " + ZiDuanMingA.Trim() + "_BIANHAO like '%" + NeiRong2.Trim() + "%') AND (A1." + ZiDuanMingA.Trim() + "_QUYU=A2." + ZiDuanMingB.Trim() + "_QUYU) AND (A1." + ZiDuanMingA.Trim() + "_JIFAN=A2." + ZiDuanMingB.Trim() + "_JIFAN) AND (A1." + ZiDuanMingA.Trim() + "_MINGCHENG=A2." + ZiDuanMingB.Trim() + "_MINGCHENG) AND( A1." + ZiDuanMingA.Trim() + "_BIANHAO=A2." + ZiDuanMingB.Trim() + "_BIANHAO)AND( A1." + ZiDuanMingB.Trim() + "_IDbiaoji=A2." + ZiDuanMingB.Trim() + "_ID))");
return Pager;
}

/// <summary>
///XianShi_LianHe_XinXi 的摘要说明:联表混合查询-2个数据表AB名称、字段标头名称AB,字段内容模糊显示数据库记录操作。
/// </summary>
public DataSet XianShi_LianHe_XinXi(string BiaoMingA, string BiaoMingB, string ZiDuanMingA, string ZiDuanMingB, string NeiRongIN, string NeiRong1, string NeiRong2, int PageSize, int PageIndex)//联合查询显示记录
{
DataSet Xian_Shi = Socut.Data.ExecuteDataSet("SELECT DISTINCT A2.* FROM " + BiaoMingA.Trim() + " A2, " + BiaoMingB.Trim() + " A1 where " + ZiDuanMingA.Trim() + "_QUYU IN (" + NeiRongIN.Trim() + ") AND (" + ZiDuanMingA.Trim() + "_JIFAN like '" + NeiRong1.Trim() + "')and(" + ZiDuanMingA.Trim() + "_MINGCHENG like '%" + NeiRong2.Trim() + "%'or " + ZiDuanMingA.Trim() + "_BIANHAO like '%" + NeiRong2.Trim() + "%') AND (A1." + ZiDuanMingA.Trim() + "_MINGCHENG=A2." + ZiDuanMingB.Trim() + "_MINGCHENG) AND( A1." + ZiDuanMingA.Trim() + "_BIANHAO=A2." + ZiDuanMingB.Trim() + "_BIANHAO)AND( A1." + ZiDuanMingB.Trim() + "_IDbiaoji=A2." + ZiDuanMingB.Trim() + "_ID) order by " + ZiDuanMingB.Trim() + "_BIANHAO desc", PageSize * (PageIndex - 1), PageSize);//asc
return Xian_Shi;
}

/// <summary>
///XianShi_TiaoJian_BuCongFuShengXu 的摘要说明:根据数据表名称、字段名称模糊条件1,不重复显示数据库记录操作,升序。
/// </summary>
public DataSet XianShi_TiaoJian_BuCongFuShengXu(string BiaoMing, string ZiDuanMing, string TiaoJian, string NeiRong)//显示记录
{
DataSet Xian_Shi = Socut.Data.ExecuteDataSet("SELECT DISTINCT " + ZiDuanMing.Trim() + " FROM " + BiaoMing.Trim() + " WHERE " + TiaoJian.Trim() + " like'%" + NeiRong.Trim() + "%' order by " + ZiDuanMing.Trim() + " asc ");
return Xian_Shi;
}
/// <summary>
///XianShi_JiLu1_JianXu 的摘要说明:根据需要显示的数据条数、数据表名称、字段名称=条件1、ID号降序显示数据库记录操作。
/// </summary>
public DataSet XianShi_JiLu1_JianXu(int TiaoShu, string BiaoMing, string ZiDuanMing, string _TiaoJian, string _ID)//显示记录
{
DataSet Xian_Shi = Socut.Data.ExecuteDataSet("SELECT TOP " + TiaoShu + " * FROM " + BiaoMing.Trim() + " WHERE " + ZiDuanMing.Trim() + "='" + _TiaoJian.Trim() + "' order by " + _ID.Trim() + " desc");
return Xian_Shi;
}
//---------------------------------------------------数据库更新
/// <summary>
///GenXin_JiLu_BuEr 的摘要说明:根据数据表名称、字段名称、ID号,更新数据库对应条件的布尔记录操作。
/// </summary>
public void GenXin_JiLu_BuEr(string BiaoMing, string ZiDuanMing, bool _NeiRong, string _IDMing, int _ID)//更新记录
{
DataSet MyData = Socut.Data.ExecuteDataSet("UPDATE " + BiaoMing.Trim() + " SET " + ZiDuanMing.Trim() + "='" + _NeiRong + "'WHERE " + _IDMing.Trim() + "=" + _ID + "");
}
//----------------------------------------------------数据库建立
/// <summary>
///XieRu_JiLu 的摘要说明:根据数据表名称、字段名称,数据库对应字段循环写入,创建一条记录操作。
/// </summary>
public void XieRu_JiLu(string BiaoMing, string[] ZiDuanMing, string[] _NeiRong)//创建记录
{
Socut.Data.ExecuteScalar("INSERT INTO " + BiaoMing.Trim() + " (" + ZiDuanMing[0].Trim() + ") VALUES ('" + "@!#$" + "')");
for (int i = 1; i < ZiDuanMing.Length; i++)
{
GenXin_JiLu_ZiDuanTiaoJian(BiaoMing, ZiDuanMing[i].Trim(), _NeiRong[i].Trim(), ZiDuanMing[0].Trim(), "@!#$");
}
GenXin_JiLu_ZiDuanTiaoJian(BiaoMing, ZiDuanMing[0].Trim(), _NeiRong[0].Trim(), ZiDuanMing[0].Trim(), "@!#$");
}
/// <summary>
///XiTong_DengLu 的摘要说明:根据数据表名称、字段名称、条件(登陆名称、密码的[与]逻辑),系统登陆。数据库读取需关闭[.Close()]
/// </summary>
public Socut.Reader XiTong_DengLu(string BiaoMing, string MingChen, string MingChen_NeiRong, string MiMa, string MiMa_NeiRong)//登陆系统
{
Socut.Reader DenLu = new Socut.Reader("SELECT * FROM " + BiaoMing.Trim() + " WHERE " + MingChen.Trim() + " = '" + MingChen_NeiRong.Trim() + "' and " + MiMa.Trim() + " = '" + MiMa_NeiRong.Trim() + "'");
return DenLu;
}
//--------------------------------------------------------数据库读取数字
/// <summary>
///DuQu_TiaoShu 的摘要说明:根据数据表名称,读取数据库数据总条数操作。
/// </summary>
public int DuQu_TiaoShu(string BiaoMing)//读取记录条数
{
int DuQuTiaoShu = (int)Socut.Data.ExecuteScalar("SELECT COUNT(*) FROM " + BiaoMing + "");
return DuQuTiaoShu;
}
}
sing System.IO;/// <summary>///Bei_Fen_DaTa 的摘要说明:数据库备份、还原操作。/// </summary>public class Bei_Fen_DaTa{    TiShi_XinXi Ts_XinXi = new TiShi_XinXi();public Bei_Fen_DaTa(){////TODO: 在此处添加构造函数逻辑//}    /// <summary>    ///Bei_Fen_data 的摘要说明:数据库备份操作。    /// </summary>    public string Bei_Fen_data()//数据库备份    {        string DateTimes = System.DateTime.Now.Year.ToString() + System.DateTime.Now.Month.ToString() + System.DateTime.Now.Day.ToString() + System.DateTime.Now.Hour.ToString() + System.DateTime.Now.Minute.ToString() + System.DateTime.Now.Second.ToString();        string ChenGong = "";        if (!File.Exists(System.Web.HttpContext.Current.Server.MapPath(@"~\bakDataBasc\" + "数据库备份" + DateTimes + ".aspx")))        {            File.Copy(System.Web.HttpContext.Current.Server.MapPath(@"~\App_Data\Socut.aspx"), System.Web.HttpContext.Current.Server.MapPath(@"~\bakDataBasc\" + "数据库备份" + DateTimes + ".aspx"));            ChenGong = "完成";            /*if (url != "")            { System.Web.HttpContext.Current.Response.Write("<script language=javascript>alert('数据库备份成功。');location='" + url + "';</script>"); }            else            { System.Web.HttpContext.Current.Response.Write("<script language=javascript>alert('数据库备份成功。')</script>"); }*/        }        return ChenGong;        //File.Delete(Server.MapPath(@"~\bei-fen\Socut.aspx"));        //File.Copy(Server.MapPath(@"~\App_Data\Socut.aspx"), Server.MapPath(@"~\bei-fen\Socut.aspx"));        //System.Web.UI.ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "友情提示!", "alert('数据库备份成功。')", true);    }    /// <summary>    ///HuanYuan_data 的摘要说明:数据库还原操作。    /// </summary>    public void HuanYuan_data(string ShuJu_MingCheng)//还原数据库    {        File.Copy(System.Web.HttpContext.Current.Server.MapPath(@"~\bakDataBasc\" + ShuJu_MingCheng), System.Web.HttpContext.Current.Server.MapPath(@"~\App_Data\Socut.aspx"), true);    }}

asp.net配合烁空组件,以上SQL语句通用。