asp.net中数据库连接的公共类的调用方法

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了asp.net中数据库连接的公共类的调用方法相关的知识,希望对你有一定的参考价值。

我想建一个数据库连接、关闭的公共类!在类下写完后(class.cs中)写完后,我忘了如何在index.aspx.cs中调用了!有知道的速度给给个答案!谢谢!

下面的例子就是调用通用类的数据库操作方法(数据库的链接与关闭都在通用类中),不懂得花可以发例子给你。using System;
using System.Collections.Generic;
using System.Text;using TroubledTimes.Models;
using System.Data;
using System.Data.SqlClient;namespace TroubledTimes.DAL

/// <summary>
/// 官方活动信息数据访问类
/// </summary>
public static class FunctionsService

/// <summary>
/// 1.根据不同情况查询活动信息
/// </summary>
/// <param name="type">活动类型</param>
/// <param name="state">设置状态</param>
/// <param name="name">活动名称</param>
/// <param name="flag">控制变量</param>
/// <returns>活动信息对象的集合</returns>
public static IList<Functions> 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<Functions> list = new List<Functions>();
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;

/// <summary>
/// 2.根据活动类型获取活动信息
/// </summary>
/// <param name="id">活动类型</param>
/// <returns>该活动类型的数量</returns>
public static int GetFunctionsByType(int type)

IList<Functions> list = new List<Functions>();
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;

/// <summary>
/// 3.根据活动ID修改活动信息
/// </summary>
/// <param name="f">活动信息类对象</param>
/// <returns>数据库中受影响的行数</returns>
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;

/// <summary>
/// 4.添加活动信息
/// </summary>
/// <param name="f">活动信息类对象</param>
/// <returns>数据库中受影响的行数</returns>
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;

/// <summary>
/// 5、根据id批量删除活动信息(修改)
/// </summary>
/// <param name="id">活动信息id</param>
/// <returns>返回受影响的行数</returns>
public static int DeleteFunById(string ids)

//string sql = "update Functions set State = 0 where FId in(\'"+ids+"\')";
string sql = "update Functions set State = 0 where FId =\'" + ids + "\'";
try

return DBHelper.ExecuteCommand(sql);

catch(Exception ex)

throw ex;


/// <summary>
/// 6、根据id修改设置状态
/// </summary>
/// <param name="id">活动信息id</param>
/// <returns>返回受影响的行数</returns>
public static int UpdateSetById(int id, int setState)

string sql = "Update Functions set SetState = "+setState+" where FId="+id;
try

return DBHelper.ExecuteCommand(sql);

catch(Exception ex)

throw ex;



参考技术A public static class DBBase public static Connection GetConnection get; private set; static DBBase() Connection = new SqlConnection(....); Connection.Open(); public static void Close() Connection.Close();

asp.net Ajax调用Aspx后台方法

 

 

Ajax调用的前提(以aspx文件为例:)

1、首先需要在aspx文件后台中引用using System.Web.Services;

2、需要调用的方法必须是公共的(public)、静态的(static);如果不是会提示“500 Internal Server Error 问题”,代表找不到method。

3、方法定义需要加入[WebMethod]的声明

4、一般建议由返回类型,最起码可能知道调用成功不成功。

 

下面是简单的调用示例:

后台方法

[WebMethod]
public static string WriteLog(DateTime StartTime, DateTime EndTime) 
{
if (true) { return ""; } else { return "读取出错!"; } return ""; }

前台调用

$.ajax({
                url: ‘Index.aspx/WriteLog‘,
                type: ‘post‘,
                data: JSON2.stringify({ StartTime: $(‘#dtbStartTime‘).datetimebox("getValue"), EndTime: $(‘#dtbEndTime‘).datetimebox("getValue")}),
                cache: false, dataType: ‘json‘,
                contentType: "application/json;charset=utf-8",
                success: function (data) {
                    console.info(data);
                }
            });

  

注意前台调用时必须把json对象转为json字符串,否则会报错 “

无效的 JSON 基元: Types

 


以上是关于asp.net中数据库连接的公共类的调用方法的主要内容,如果未能解决你的问题,请参考以下文章

在 WPF、Silverlight 和 ASP.NET 之间共享一个公共 DAL

如何在ASP.net中调用webservice里的一个方法

asp.net 调用百度地图API,在JS里面会出现无法加载未定义或null引用的属性“addOverlay”该怎么解决?

在asp.net里怎样异步调用WebService方法

asp.net Ajax调用Aspx后台方法

[ASP.NET]ASP.NET中常用的26个优化性能方法