c#中三层架构的Framework(DAL)层与数据库连接的代码是什么?
4个回答
展开全部
强烈建议使用微软企业库,而不要用什么SqlHelper之类的东东,在引用了Microsoft.Practices.EnterpriseLibrary.Data.dll和Microsoft.Practices.EnterpriseLibrary.Common.dll后,在webconfig里面配置节点
<configSections>
<section name="dataConfiguration" type="Microsoft.Practices.EnterpriseLibrary.Data.Configuration.DatabaseSettings, Microsoft.Practices.EnterpriseLibrary.Data, Version=4.1.0.0, Culture=neutral" />
</configSections>
<!-- 数据库连接配置 -->
<dataConfiguration defaultDatabase="KDSWDBConnection" />
<connectionStrings>
<add name="KDSWDBConnection" connectionString="server=10.192.56.112; user id=sa; password =sa; database =KDSW" providerName="System.Data.SqlClient" />
</connectionStrings>
然后在代码里使用
//创建Database对象
Database db = DatabaseFactory.CreateDatabase();
//创建DbCommand对象
DbCommand dbCommand = db.GetStoredProcCommand("UP_APKeyHistory_Query");
//添加输入输出参数
db.AddInParameter(dbCommand, "@APID", DbType.String, apq.APID);
db.AddInParameter(dbCommand, "@CommunicationKey", DbType.String, apq.CommunicationKey);
db.AddInParameter(dbCommand, "@MaxEffectiveTime", DbType.DateTime, apq.MaxEffectiveTime);
db.AddInParameter(dbCommand, "@MinEffectiveTime", DbType.DateTime, apq.MinEffectiveTime);
db.AddInParameter(dbCommand, "@MaxExpiredTime", DbType.String, apq.MaxExpiredTime);
db.AddInParameter(dbCommand, "@MinExpiredTime", DbType.String, apq.MinExpiredTime);
db.AddInParameter(dbCommand, "@KeyStatus", DbType.String, apq.KeyStatus);
List<APKeyHistoryInfo> APKeyList = new List<APKeyHistoryInfo>();
//执行命令返回DataReader对象
using (IDataReader dr = db.ExecuteReader(dbCommand))
{
while (dr.Read())
{
APKeyList.Add(CreateAPKeyHistoryInfo(dr));
}
}
return APKeyList;
<configSections>
<section name="dataConfiguration" type="Microsoft.Practices.EnterpriseLibrary.Data.Configuration.DatabaseSettings, Microsoft.Practices.EnterpriseLibrary.Data, Version=4.1.0.0, Culture=neutral" />
</configSections>
<!-- 数据库连接配置 -->
<dataConfiguration defaultDatabase="KDSWDBConnection" />
<connectionStrings>
<add name="KDSWDBConnection" connectionString="server=10.192.56.112; user id=sa; password =sa; database =KDSW" providerName="System.Data.SqlClient" />
</connectionStrings>
然后在代码里使用
//创建Database对象
Database db = DatabaseFactory.CreateDatabase();
//创建DbCommand对象
DbCommand dbCommand = db.GetStoredProcCommand("UP_APKeyHistory_Query");
//添加输入输出参数
db.AddInParameter(dbCommand, "@APID", DbType.String, apq.APID);
db.AddInParameter(dbCommand, "@CommunicationKey", DbType.String, apq.CommunicationKey);
db.AddInParameter(dbCommand, "@MaxEffectiveTime", DbType.DateTime, apq.MaxEffectiveTime);
db.AddInParameter(dbCommand, "@MinEffectiveTime", DbType.DateTime, apq.MinEffectiveTime);
db.AddInParameter(dbCommand, "@MaxExpiredTime", DbType.String, apq.MaxExpiredTime);
db.AddInParameter(dbCommand, "@MinExpiredTime", DbType.String, apq.MinExpiredTime);
db.AddInParameter(dbCommand, "@KeyStatus", DbType.String, apq.KeyStatus);
List<APKeyHistoryInfo> APKeyList = new List<APKeyHistoryInfo>();
//执行命令返回DataReader对象
using (IDataReader dr = db.ExecuteReader(dbCommand))
{
while (dr.Read())
{
APKeyList.Add(CreateAPKeyHistoryInfo(dr));
}
}
return APKeyList;
光点科技
2023-08-15 广告
2023-08-15 广告
通常情况下,我们会按照结构模型把系统产生的数据分为三种类型:结构化数据、半结构化数据和非结构化数据。结构化数据,即行数据,是存储在数据库里,可以用二维表结构来逻辑表达实现的数据。最常见的就是数字数据和文本数据,它们可以某种标准格式存在于文件...
点击进入详情页
本回答由光点科技提供
展开全部
强烈建议使用微软企业库,而不要用什么SqlHelper之类的东东,在引用了Microsoft.Practices.EnterpriseLibrary.Data.dll和Microsoft.Practices.EnterpriseLibrary.Common.dll后,在webconfig里面配置节点
<configSections>
<section
name="dataConfiguration"
type="Microsoft.Practices.EnterpriseLibrary.Data.Configuration.DatabaseSettings,
Microsoft.Practices.EnterpriseLibrary.Data,
Version=4.1.0.0,
Culture=neutral"
/>
</configSections>
<!--
数据库连接配置
-->
<dataConfiguration
defaultDatabase="KDSWDBConnection"
/>
<connectionStrings>
<add
name="KDSWDBConnection"
connectionString="server=10.192.56.112;
user
id=sa;
password
=sa;
database
=KDSW"
providerName="System.Data.SqlClient"
/>
</connectionStrings>
然后在代码里使用
//创建Database对象
Database
db
=
DatabaseFactory.CreateDatabase();
//创建DbCommand对象
DbCommand
dbCommand
=
db.GetStoredProcCommand("UP_APKeyHistory_Query");
//添加输入输出参数
db.AddInParameter(dbCommand,
"@APID",
DbType.String,
apq.APID);
db.AddInParameter(dbCommand,
"@CommunicationKey",
DbType.String,
apq.CommunicationKey);
db.AddInParameter(dbCommand,
"@MaxEffectiveTime",
DbType.DateTime,
apq.MaxEffectiveTime);
db.AddInParameter(dbCommand,
"@MinEffectiveTime",
DbType.DateTime,
apq.MinEffectiveTime);
db.AddInParameter(dbCommand,
"@MaxExpiredTime",
DbType.String,
apq.MaxExpiredTime);
db.AddInParameter(dbCommand,
"@MinExpiredTime",
DbType.String,
apq.MinExpiredTime);
db.AddInParameter(dbCommand,
"@KeyStatus",
DbType.String,
apq.KeyStatus);
List<APKeyHistoryInfo>
APKeyList
=
new
List<APKeyHistoryInfo>();
//执行命令返回DataReader对象
using
(IDataReader
dr
=
db.ExecuteReader(dbCommand))
{
while
(dr.Read())
{
APKeyList.Add(CreateAPKeyHistoryInfo(dr));
}
}
return
APKeyList;
<configSections>
<section
name="dataConfiguration"
type="Microsoft.Practices.EnterpriseLibrary.Data.Configuration.DatabaseSettings,
Microsoft.Practices.EnterpriseLibrary.Data,
Version=4.1.0.0,
Culture=neutral"
/>
</configSections>
<!--
数据库连接配置
-->
<dataConfiguration
defaultDatabase="KDSWDBConnection"
/>
<connectionStrings>
<add
name="KDSWDBConnection"
connectionString="server=10.192.56.112;
user
id=sa;
password
=sa;
database
=KDSW"
providerName="System.Data.SqlClient"
/>
</connectionStrings>
然后在代码里使用
//创建Database对象
Database
db
=
DatabaseFactory.CreateDatabase();
//创建DbCommand对象
DbCommand
dbCommand
=
db.GetStoredProcCommand("UP_APKeyHistory_Query");
//添加输入输出参数
db.AddInParameter(dbCommand,
"@APID",
DbType.String,
apq.APID);
db.AddInParameter(dbCommand,
"@CommunicationKey",
DbType.String,
apq.CommunicationKey);
db.AddInParameter(dbCommand,
"@MaxEffectiveTime",
DbType.DateTime,
apq.MaxEffectiveTime);
db.AddInParameter(dbCommand,
"@MinEffectiveTime",
DbType.DateTime,
apq.MinEffectiveTime);
db.AddInParameter(dbCommand,
"@MaxExpiredTime",
DbType.String,
apq.MaxExpiredTime);
db.AddInParameter(dbCommand,
"@MinExpiredTime",
DbType.String,
apq.MinExpiredTime);
db.AddInParameter(dbCommand,
"@KeyStatus",
DbType.String,
apq.KeyStatus);
List<APKeyHistoryInfo>
APKeyList
=
new
List<APKeyHistoryInfo>();
//执行命令返回DataReader对象
using
(IDataReader
dr
=
db.ExecuteReader(dbCommand))
{
while
(dr.Read())
{
APKeyList.Add(CreateAPKeyHistoryInfo(dr));
}
}
return
APKeyList;
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
webconfig里面
<add key="ConnectionString" value="server=127.0.0.1;database=codematic;uid=sa;pwd=1"/>
string connectionString = ConfigurationManager.AppSettings[configName];
<add key="ConnectionString" value="server=127.0.0.1;database=codematic;uid=sa;pwd=1"/>
string connectionString = ConfigurationManager.AppSettings[configName];
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
写在webconfig里面
<add key="cn" value=“”>
<add key="cn" value=“”>
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询