asp.net公共方法怎么写

1.企业客户管理系统ASP.net C#公共类实现 应该怎么写这个类包装了对数据库的普通操作,写这个类的主要目的我猜是为了转接不同类型的数据库容易一点,比如在Access,MS SQL和Oracle之间切换 。而且代码非常潦草,像是草稿或是写了一半没有完成 。
类中几个方法实现的功能
public static DataSet GDS(string sql) ,执行sql 查询语句,返回一个DataSet格式的结果
public DataSet hsggetdata(string sql),同上
public static void Dsql(string sql),执行不需要返回结果的sql语句,比如增删改什么的 。
public int hsgexucute(string sql),同上
具体连到哪个数据库,看Connection对象和Connection String
2.asp.net里面如何把这个方法封装成一个类供公共调用呢public void GetDrop() {
SqlConnection dropconn = new SqlConnection(Prolongation.GetconntoCRM().ToString());
dropconn.Open();
SqlDataAdapter ad = new SqlDataAdapter("select * from K_cdata0010 where Cprar_id='1'", dropconn);
DataTable dt = new DataTable();
ad.Fill(dt);
Session["DT"] = dt; //使用Session保存 dt 对象,以便于以后的 类、方法 使用
dropconn.Close();
}
【asp.net公共方法怎么写】//静态类PubClassDrop ,类的访问权限为 public
public static class PubClassDrop
{
//静态类的静态方法GetDropDataBind(),该方法的权限也是 public,不然其他类访问不到
public static GetDropDataBind()
{
//从这里开始
DataTable dt = Session["DT"] as DtatTable ; //注意这行,使用Session获取dt 对象
string smcolor=dt.Rows[0]["SMColor"].ToString();
string[] StrItem = smcolor.Split('\n');
for (int i = 0; i
3.asp.net里面如何把这个方法封装成一个类供公共调用public void GetData(DropDownList ddlList,DataTable dt)
{
string smcolor=dt.Rows[0]["SMColor"].ToString();
string[] StrItem = smcolor.Split('\n');
for (int i = 0; i < StrItem.Length; i++)
{
ddlList.Items.Add(StrItem[i]);
}
}
参数传DropDownList和DataTable
记事本里写的,可能细节上有点问题
4.asp.net里面如何把这个方法封装成一个类供公共调用public void GetData(DropDownList ddlList,DataTable dt) { string smcolor=dt.Rows[0]["SMColor"].ToString(); string[] StrItem = smcolor.Split('\n'); for (int i = 0; i < StrItem.Length; i++) { ddlList.Items.Add(StrItem[i]); } } 参数传DropDownList和DataTable 记事本里写的,可能细节上有点问题 。
5.asp.net中数据库连接的公共类的调用方法下面的例子就是调用通用类的数据库操作方法(数据库的链接与关闭都在通用类中),不懂得花可以发例子给你 。
using System;using System.Collections.Generic;using System.Text;using TroubledTimes.Models;using System.Data;using System.Data.SqlClient;namespace TroubledTimes.DAL{ /// /// 官方活动信息数据访问类 /// public static class FunctionsService { /// /// 1.根据不同情况查询活动信息 /// /// 活动类型 /// 设置状态 /// 活动名称 /// 控制变量 /// 活动信息对象的集合 public static IList GetAllFunctions(string type,string state,string name,int flag) { string sql = "Select * from Functions where State =1"; if(type!="" && flag==1) sql += " and FunState='" + type + "'"; else if (state != "" && flag == 2) sql += " and SetState='" + state + "'"; else if (name!="" && flag==3) sql += " and FunctionName like '%" + name + "%'"; else if (flag == 4) sql += " and FunState='" + type + "' and SetState='" + state + "'"; else if (flag == 5) sql += " and FunState='" + type + "' and FunctionName like '%" + name + "%'"; else if (flag == 6) sql += " and SetState='" + state + "' and FunctionName like '%" + name + "%'"; else if (flag == 7) sql += " and FunState='" + type + "' and SetState='" + state + "' and FunctionName like '%" + name + "%'"; sql += " order by FunNumber Desc"; IList list = new List(); try { // DataTable dt = DBHelper.GetScalar("up_SelectFunctions"); DataTable dt = DBHelper.GetDataTable(sql); foreach (DataRow row in dt.Rows) { Functions function = new Functions(); function.FunctionName = (string)row["FunctionName"]; function.FId = (int)row["FId"]; function.FunctionUrl = (string)row["FunctionUrl"]; function.FunctionImg = (string)row["FunctionImg"]; function.FunctionContent = (string)row["FunctionContent"]; function.FunctionTime = (DateTime)row["FunctionTime"]; function.FunAdminUrl = (string)row["FunAdminUrl"]; function.FunState = (int)row["FunState"]; //--活动类型(游戏活动/官网活动,0:游戏) function.SetState = (int)row["SetState"]; //--设置状态(设置中/预设置,0:预设置) function.FunNumber = (int)row["FunNumber"]; //--活动支持率(仅官网) function.State = (int)row["State"]; //--存贮状态(0/1) list.Add(function); } return list; } catch (Exception ex) { Console.WriteLine(ex.Message); return null; } } /// /// 2.根据活动类型获取活动信息 /// /// 活动类型 /// 该活动类型的数量 public static int GetFunctionsByType(int type) { IList list = new List(); try { string sql = "select count(*) from Functions where SetState = 1 and FunState='" + type+ "'"; return DBHelper.Sanlar(sql); } catch (Exception ex) { Console.WriteLine(ex.Message); return 0; } } /// /// 3.根据活动ID修改活动信息 /// /// 活动信息类对象 /// 数据库中受影响的行数 public static int ModifyFunctionsById(Functions f) { try { SqlParameter[] para = new SqlParameter[] { new SqlParameter("@FId",f.FId), new SqlParameter("@FunctionName",f.FunctionName), new SqlParameter("@FunctionUrl",f.FunctionUrl), new SqlParameter("@FunctionImg",f.FunctionImg), new SqlParameter("@FunctionContent",f.FunctionContent), new SqlParameter("@FunctionTime",f.FunctionTime), new SqlParameter("@function.FunAdminUrl",f.FunAdminUrl), new SqlParameter("@FunState",f.FunState), new SqlParameter("@FunNumber",f.FunNumber), new SqlParameter("@SetState",f.SetState), new SqlParameter("@State",f.State) }; return DBHelper.ExecuteProc("up_AmendFunctions", para); } catch (Exception ex) { Console.WriteLine(ex.Message); return 0; } } /// /// 4.添加活动信息 /// /// 活动信息类对象 /// 数据库中受影响的行数 public static int AddFunctions(Functions f) { try { SqlParameter[] para = new SqlParameter[] { new SqlParameter("@FunctionName",f.FunctionName), new SqlParameter("@FunctionUrl",f.FunctionUrl), new SqlParameter("@FunctionImg",f.FunctionImg), new SqlParameter("@FunctionContent",f.FunctionContent), new SqlParameter("@FunctionTime",f.FunctionTime), new SqlParameter("@FunAdminUrl",f.FunAdminUrl), new SqlParameter("@FunState",f.FunState), new SqlParameter("@FunNumber",f.FunNumber), new SqlParameter("@SetState",f.SetState), new SqlParameter("@State",f.State) }; return DBHelper.ExecuteProc("up_AddFunctions", para); } catch (Exception ex) { Console.WriteLine(ex.Message); return 0; } } /// /// 5、根据id批量删除活 。