ASP.NET中如何调用APP_CODE中的data.cs来实现页面default.aspx的访问?

2024-11-29 15:58:37
推荐回答(3个)
回答(1):

在app_code中添加类,取名为TBOperate.cs,内容如下
using System;
using System.Collections.Generic;
using System.Text;
using System.Data.SqlClient;
using System.Data;

namespace DesignTBOperate
{
public class TBOperate
{
protected SqlConnection connection = null;
public TBOperate()
{
string databasestr = System.Configuration.ConfigurationSettings.AppSettings["connectionStrings"];
connection = new SqlConnection(databasestr);
}
public TBOperate(string iConnectionString)
{
connection = new SqlConnection(iConnectionString);
}
public Boolean hasUser(string nameField, string username, string pswField,string Psw,string Tbname)
{
string sql = "Select Count(*) From " + Tbname + " Where " + nameField + "='" + username + "' And " + pswField + "='" + Psw + "'";
if (isData(sql) > 0)
return true;
else
return false;
}
}
}

在页面对应CS文件中的调用方法:
using DesignTBOperate;

TBOperate myOperate = new TBOperate();

if (myOperate.hasUser("name",Textbox_name.text,"psw",TextBox2_psw.text,"users"))
{//登陆成功
}
else
{//登陆失败
}

回答(2):

在APP_CODE里面新建类data,在data类里面添加数据库操作的静态方法。
在default.asp.cs直接通过类名点方法名称。

或者data类里添加非静态方法。在default.asp.cs中new实例化对象点方法。

回答(3):

Data.cs
public Class A
{
public void Add()
{}
}

default.cs

A a=new A();
a.Add();