我在 C# 中使用 N tiers tec 进行工作,试图使其易于使用并且能够更改任何数据库类型而无需重新编写所有 cod,我的代码在这里没有出现任何错误,但它没有得到我的文本框的任何值
(我试图从表中获取数据到许多文本框以便稍后更新它)以及代码的工作原理:{ 首先,我创建了一些函数来获取任何设置的任何类型的参数或设置任何命令,然后我创建其他函数来执行我设置或从数据库中获取的所有函数我在文件夹名称中构建它(数据访问层)然后我创建了其他文件夹(数据构建层)来使用所有这些功能来完成我想要在任何页面中执行的操作(插入、更新、删除、选择),最后我认为我这样做是为了调用我在(数据构建层)到我的页面或控件,我这样做是因为如果我更改数据库类型,我只更改一个类,其他类仍然相同我希望我解释得足够多(抱歉我的英语不够好)}
代码 :
类 DataAccessLayer
public static void Setcommand (SqlCommand cmd,CommandType type,string commandtext)
{
cmd.CommandType=type;
cmd.CommandText=commandtext;
}
public static void AddSQLparameter(SqlCommand cmd, int size,SqlDbType type,object value,string paramName,ParameterDirection direction)
{
if (cmd == null)
{
throw (new ArgumentException("cmd"));
}
if (paramName == null)
{
throw (new ArgumentException("paramName"));
}
SqlParameter param=new SqlParameter();
param.ParameterName= paramName;
param.SqlDbType=type;
param.Size=size;
param.Value=value;
param.Direction=direction;
cmd.Parameters.Add(param);
}
public static SqlDataReader ExecuteSelectCommand(SqlCommand cmd)
{
if (cmd == null)
{
throw (new ArgumentNullException("cmd"));
}
SqlConnection con = new SqlConnection();
cmd.Connection = con;
con.Open();
SqlDataReader dr = cmd.ExecuteReader();
con.Close();
return dr ;
}
类 DatabuildLayer
SqlCommand com;
public DatabuildLayer()
{
com = new SqlCommand();
//
// TODO: Add constructor logic here
//
}
public SqlDataReader SelectCatalog(int catid)
{
DataAccessLayer.Setcommand(com, CommandType.Text, "select catname,catdescription,photo from category where catid=@catid" );
DataAccessLayer.addSQLparameter(com,16,SqlDbType.Int,catid,"@catid",ParameterDirection.Input);
return DataAccessLayer.ExecuteSelectCommand(com);;
}
这是我最后一个将数据检索到某个文本框的代码
在我的页面加载中:
protected void Page_Load(object sender, EventArgs e)
{
DatabuildLayer= new DatabuildLayer();
SqlDataReader dr ;
dr = obj.SelectCatalog(catselectddl.SelectedIndex);
if (dr.Read())
{
catnametxt.Text = dr["catname"].ToString();
catdestxt.Text = dr["catdescription"].ToString();
}
}