建立一个静态的数据访问类,是否可行

baddot 2004-01-11 09:07:28
我想建立如下一个用于访问数据库的类,若全部属性和方法全部建成静态的,这样会不会提高效率。

静态类:
public class DataAccess{
public static ConnString = "数据库连接字符串";
public static SelectData(){查询语句};
public static InsertData(){插入语句};
public static DeleteData(){删除语句};
public static UpdateData(){更新语句};
}
这样,调用时,只需 命名空间.DataAccess.SelectData()就可以引用了,而不需要先生成对象,再调用对象的方法了。
这样应该可以节省资源吧。
...全文
53 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
活靶子哥哥 2004-01-20
  • 打赏
  • 举报
回复
SqlHelper

public static int ExecuteNonQuery(string connectionString, CommandType commandType, string commandText)
{
//pass through the call providing null for the set of SqlParameters
return ExecuteNonQuery(connectionString, commandType, commandText, (SqlParameter[])null);
}

public static int ExecuteNonQuery(string connectionString, CommandType commandType, string commandText, params SqlParameter[] commandParameters)
{
//create & open a SqlConnection, and dispose of it after we are done.
using (SqlConnection cn = new SqlConnection(connectionString))
{
cn.Open();

//call the overload that takes a connection in place of the connection string
return ExecuteNonQuery(cn, commandType, commandText, commandParameters);
}
}
public static int ExecuteNonQuery(string connectionString, string spName, params object[] parameterValues)
{
//if we receive parameter values, we need to figure out where they go
if ((parameterValues != null) && (parameterValues.Length > 0))
{
//pull the parameters for this stored procedure from the parameter cache (or discover them & populate the cache)
SqlParameter[] commandParameters = SqlHelperParameterCache.GetSpParameterSet(connectionString, spName);

//assign the provided values to these parameters based on parameter order
AssignParameterValues(commandParameters, parameterValues);

//call the overload that takes an array of SqlParameters
return ExecuteNonQuery(connectionString, CommandType.StoredProcedure, spName, commandParameters);
}
//otherwise we can just call the SP without params
else
{
return ExecuteNonQuery(connectionString, CommandType.StoredProcedure, spName);
}
}

活靶子哥哥 2004-01-20
  • 打赏
  • 举报
回复
up 下
祝楼上的楼下的新年好 我要回家了
1周后听各位高论
baddot 2004-01-20
  • 打赏
  • 举报
回复
二星上将,请告诉我为什么不行,请点明原因。。不解中。。。。。
newnan 2004-01-12
  • 打赏
  • 举报
回复
好像有点舍本逐末的感觉……
cnlamar 2004-01-12
  • 打赏
  • 举报
回复
并发的时候可以用锁吗?但我感觉估计会有问题,旁观。。。
henryfan1 2004-01-12
  • 打赏
  • 举报
回复
可行,不过你的方法必须是没状态的,
SQLHelper就是。
codinghello 2004-01-12
  • 打赏
  • 举报
回复
不可行
baddot 2004-01-12
  • 打赏
  • 举报
回复
这只是静态的操作数据库的方法,没有用到事务。
我不明白这和并发有什么关系。。

我只是想知道这种方法是否可行。请大家说一下自己的想法。。
delphi1998 2004-01-12
  • 打赏
  • 举报
回复
倒底是几个问题?
elite2018 2004-01-12
  • 打赏
  • 举报
回复
不可行.
lx1920 2004-01-11
  • 打赏
  • 举报
回复
那在并发的情况下怎么办?

62,025

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧