使用Data access block

一、配置Data block所需参数
1,应用程序的配置文件(*.exe.config或者*.dll.config或者Web.config)
None.gif <? xml version="1.0" encoding="utf-8"  ?>
None.gif
< configuration >
None.gif  
< configSections >
None.gif    
< section  name ="enterpriselibrary.configurationSettings"  type ="Microsoft.Practices.EnterpriseLibrary.Configuration.ConfigurationManagerSectionHandler, Microsoft.Practices.EnterpriseLibrary.Configuration"   />
None.gif  
</ configSections >
None.gif  
< enterpriselibrary .configurationSettings xmlns:xsd ="http://www.w3.org/2001/XMLSchema"  xmlns:xsi ="http://www.w3.org/2001/XMLSchema-instance"  defaultSection =""  applicationName ="Application"  xmlns ="http://www.microsoft.com/practices/enterpriselibrary/08-31-2004/configuration" >
None.gif  
< configurationSections >
None.gif    
< configurationSection  name ="dataConfiguration"  encrypt ="false" >
None.gif      
< storageProvider  xsi:type ="XmlFileStorageProviderData"  name ="XML File Storage Provider"  path ="dataConfiguration.config"   />
None.gif      
< dataTransformer  xsi:type ="XmlSerializerTransformerData"  name ="Xml Serializer Transformer" >
None.gif        
< includeTypes  />
None.gif      
</ dataTransformer >
None.gif    
</ configurationSection >
None.gif  
</ configurationSections >
None.gif  
< keyAlgorithmStorageProvider  xsi:nil ="true"   />
None.gif
</ enterpriselibrary.configurationSettings >  
None.gif
</ configuration >

2,配置数据库连接串(dataConfiguration.config)
None.gif <? xml version="1.0" encoding="utf-8" ?>
None.gif
< dataConfiguration >
None.gif  
< xmlSerializerSection  type ="Microsoft.Practices.EnterpriseLibrary.Data.Configuration.DatabaseSettings, Microsoft.Practices.EnterpriseLibrary.Data, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" >
None.gif    
< enterpriseLibrary .databaseSettings xmlns:xsd ="http://www.w3.org/2001/XMLSchema"  xmlns:xsi ="http://www.w3.org/2001/XMLSchema-instance"  defaultInstance ="InstanceWebinpuy"  xmlns ="http://www.microsoft.com/practices/enterpriselibrary/08-31-2004/data" >
None.gif      
< databaseTypes >
None.gif        
< databaseType  name ="Sql Server"  type ="Microsoft.Practices.EnterpriseLibrary.Data.Sql.SqlDatabase, Microsoft.Practices.EnterpriseLibrary.Data, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null"   />
None.gif      
</ databaseTypes >
None.gif      
< instances >
None.gif        
< instance  name ="InstanceWebinpuy"  type ="Sql Server"  connectionString ="Sql Connection String"   />
None.gif      
</ instances >
None.gif      
< connectionStrings >
None.gif        
< connectionString  name ="Sql Connection String" >
None.gif          
< parameters >
None.gif            
< parameter  name ="database"  value ="BMS_Webinput"  isSensitive ="false"   />
None.gif            
< parameter  name ="Integrated Security"  value ="False"  isSensitive ="false"   />
None.gif            
< parameter  name ="server"  value ="192.168.1.28"  isSensitive ="false"   />
None.gif          
</ parameters >
None.gif        
</ connectionString >
None.gif      
</ connectionStrings >
None.gif    
</ enterpriseLibrary.databaseSettings >
None.gif  
</ xmlSerializerSection >
None.gif
</ dataConfiguration >

二、执行Sql语句
None.gif public   string  GetCustomerList()
ExpandedBlockStart.gifContractedBlock.gif        
dot.gif {
InBlock.gif            
// DataReader that will hold the returned results        
InBlock.gif            
// Create the Database object, using the default database service. The
InBlock.gif            
// default database service is determined through configuration.
InBlock.gif
            Database db = DatabaseFactory.CreateDatabase();
InBlock.gif
InBlock.gif            
string sqlCommand = "Select CustomerID, Name, Address, City, Country, PostalCode " +
InBlock.gif                
"From Customers";
InBlock.gif            DBCommandWrapper dbCommandWrapper 
= db.GetSqlStringCommandWrapper(sqlCommand);
InBlock.gif
InBlock.gif            StringBuilder readerData 
= new StringBuilder();
InBlock.gif
InBlock.gif            
// The ExecuteReader call will request the connection to be closed upon
InBlock.gif            
// the closing of the DataReader. The DataReader will be closed 
InBlock.gif            
// automatically when it is disposed.
InBlock.gif
            using (IDataReader dataReader = db.ExecuteReader(dbCommandWrapper))
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
// Iterate through DataReader and put results to the text box.
InBlock.gif                
// DataReaders cannot be bound to Windows Form controls (e.g. the
InBlock.gif                
// resultsDataGrid), but may be bound to Web Form controls.
InBlock.gif
                while (dataReader.Read())
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    
// Get the value of the 'Name' column in the DataReader
InBlock.gif
                    readerData.Append(dataReader["Name"]);
InBlock.gif                    readerData.Append(Environment.NewLine);
ExpandedSubBlockEnd.gif                }

ExpandedSubBlockEnd.gif            }

InBlock.gif
InBlock.gif            
return readerData.ToString();
ExpandedBlockEnd.gif        }

三、调用存储过程
1、插入新记录并从存储过程获取返回值
存储过程:
None.gif Create     PROCEDURE  usp_AddGroup
None.gif 
@StaffID   VARCHAR ( 36 ),
None.gif 
@GroupName   VARCHAR ( 40 ),
None.gif 
@Count    INT
None.gif
AS
None.gif 
IF   EXISTS ( SELECT   *   FROM  StaffGroup  WHERE  StaffID = @StaffID   AND  GroupName = @GroupName )
None.gif  
RETURN   1
None.gif 
ELSE
None.gif  
INSERT  StaffGroup (GroupName,StaffID,MaxCount)  VALUES ( @GroupName , @StaffID , @Count )
None.gif 
RETURN   @@ERROR
None.gif
GO

调用代码:
None.gif private   void  button4_Click( object  sender, System.EventArgs e)
ExpandedBlockStart.gifContractedBlock.gif        
dot.gif {
InBlock.gif            
//@RETURN_VALUE,RETURN_VALUE,
InBlock.gif
            Database db = DatabaseFactory.CreateDatabase();
InBlock.gif            DBCommandWrapper cmd 
= db.GetStoredProcCommandWrapper("usp_AddGroup");
InBlock.gif            cmd.AddInParameter(
"@StaffID",DbType.String,"3290F849-031F-49B5-8CEE-0F98AA789731");
InBlock.gif            cmd.AddInParameter(
"@GroupName",DbType.String,"yyyooo");
InBlock.gif            cmd.AddInParameter(
"@Count",DbType.Int32,10);
InBlock.gif            cmd.AddParameter(
"RetVal7",DbType.Int32,ParameterDirection.ReturnValue,"",DataRowVersion.Current,null);
InBlock.gif            db.ExecuteNonQuery(cmd);
InBlock.gif            
int a = (int)cmd.GetParameterValue("RetVal7");
InBlock.gif            MessageBox.Show(a.ToString());
ExpandedBlockEnd.gif        }

2、返回记录集并获取存储过程返回值
存储过程:
None.gif CREATE   procedure  usp_GetValidStaffs
None.gif            
AS
None.gif                
Select   *   from  staff  where  Isdelete  =   0   and  ShowOnHomePage = 1
None.gif                
RETURN   8
None.gif            
GO

调用代码:
None.gif private   void  button5_Click( object  sender, System.EventArgs e)
ExpandedBlockStart.gifContractedBlock.gif        
dot.gif {
InBlock.gif            Database db 
= DatabaseFactory.CreateDatabase();
InBlock.gif            DBCommandWrapper cmd 
= db.GetStoredProcCommandWrapper("usp_GetValidStaffs");
InBlock.gif            cmd.AddParameter(
"RetVal7",DbType.Int32,ParameterDirection.ReturnValue,"",DataRowVersion.Current,null);
InBlock.gif            DataSet ds 
= db.ExecuteDataSet(cmd);
InBlock.gif            dataGrid1.SetDataBinding(ds,
"Table");
InBlock.gif            
int a = (int)cmd.GetParameterValue("RetVal7");
InBlock.gif            MessageBox.Show(a.ToString());
ExpandedBlockEnd.gif        }

3、返回记录集并通过输出参数获取返回值
存储过程:
None.gif CREATE   procedure  usp_GetValidStaffs
None.gif                
@Count   INT  OUTPUT
None.gif            
AS
None.gif                
Select   *   from  staff  where  Isdelete  =   0   and  ShowOnHomePage = 1
None.gif                
SET    @Count   =   8
None.gif            
GO

调用代码:
None.gif private   void  button6_Click( object  sender, System.EventArgs e)
ExpandedBlockStart.gifContractedBlock.gif        
dot.gif {
InBlock.gif            Database db 
= DatabaseFactory.CreateDatabase();
InBlock.gif            DBCommandWrapper cmd 
= db.GetStoredProcCommandWrapper("usp_GetValidStaffs");
InBlock.gif            cmd.AddOutParameter(
"@Count",DbType.Int32,4);
InBlock.gif            DataSet ds 
= db.ExecuteDataSet(cmd);
InBlock.gif            dataGrid1.SetDataBinding(ds,
"Table");
InBlock.gif            
int a = (int)cmd.GetParameterValue("@Count");
InBlock.gif            MessageBox.Show(a.ToString());
ExpandedBlockEnd.gif        }

4、DataRearder与输出参数
存储过程:
None.gif CREATE   procedure  usp_GetValidStaffs
None.gif                
@Count   INT  OUTPUT
None.gif            
AS
None.gif                
Select   *   from  staff  where  Isdelete  =   0   and  ShowOnHomePage = 1
None.gif                
SET    @Count   =   8
None.gif            
GO

调用代码:
None.gif private   void  button8_Click( object  sender, System.EventArgs e)
ExpandedBlockStart.gifContractedBlock.gif        
dot.gif {
InBlock.gif            Database db 
= DatabaseFactory.CreateDatabase();
InBlock.gif            DBCommandWrapper cmd 
= db.GetStoredProcCommandWrapper("usp_GetValidStaffs");
InBlock.gif            cmd.AddOutParameter(
"@Count",DbType.Int32,4);
InBlock.gif            
using(IDataReader dr = db.ExecuteReader(cmd))
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
while (dr.Read()) 
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    MessageBox.Show(dr.GetString (
1));
ExpandedSubBlockEnd.gif                }
;
ExpandedSubBlockEnd.gif            }

InBlock.gif            
object o = cmd.GetParameterValue("@Count");
InBlock.gif            MessageBox.Show(o.ToString());
ExpandedBlockEnd.gif        }

5、DataRearder与返回值
存储过程:
None.gif CREATE   procedure  usp_GetValidStaffs
None.gif            
AS
None.gif                
Select   *   from  staff  where  Isdelete  =   0   and  ShowOnHomePage = 1
None.gif                
RETURN   8
None.gif            
GO

调用代码:
None.gif private   void  button7_Click( object  sender, System.EventArgs e)
ExpandedBlockStart.gifContractedBlock.gif        
dot.gif {
InBlock.gif            Database db 
= DatabaseFactory.CreateDatabase();
InBlock.gif            DBCommandWrapper cmd 
= db.GetStoredProcCommandWrapper("usp_GetValidStaffs");
InBlock.gif            cmd.AddParameter(
"RetVal7",DbType.Int32,ParameterDirection.ReturnValue,"",DataRowVersion.Current,null);
InBlock.gif            
using(IDataReader dr = db.ExecuteReader(cmd))
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
while (dr.Read()) 
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    MessageBox.Show(dr.GetString (
1));
ExpandedSubBlockEnd.gif                }
;
ExpandedSubBlockEnd.gif            }

InBlock.gif            
object o = cmd.GetParameterValue("RetVal7");
InBlock.gif            MessageBox.Show(o.ToString());
ExpandedBlockEnd.gif        }

四、事务处理:
None.gif public   static   bool  OpretRapportFraskabelon ( string  CVR,  int  maanedValoer)
ExpandedBlockStart.gifContractedBlock.gif        
dot.gif {
InBlock.gif            
try
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                db 
= DatabaseFactory.CreateDatabase();
ExpandedSubBlockEnd.gif            }

InBlock.gif            
catch(Exception ex)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
throw new PensamDBException("Fejl i opret databasen",ex); 
ExpandedSubBlockEnd.gif            }

InBlock.gif            
using (IDbConnection connection = db.GetConnection())
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                connection.Open();
InBlock.gif                IDbTransaction transaction 
= connection.BeginTransaction();
InBlock.gif                
try
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    
int year = maanedValoer / 12 + 1800;
InBlock.gif                    
int month = maanedValoer % 12 + 1;
InBlock.gif                    DBCommandWrapper cmdWrapper 
= db.GetStoredProcCommandWrapper("sp_OpretRapportFraSkabelon");
InBlock.gif                    cmdWrapper.AddInParameter(
"@CVR", DbType.String,CVR);
InBlock.gif                    cmdWrapper.AddInParameter(
"@Year", DbType.Int32,year);
InBlock.gif                    cmdWrapper.AddInParameter(
"@Month", DbType.Int32,month);
InBlock.gif                    cmdWrapper.AddParameter  (
"RetVal",DbType.Int32,ParameterDirection.ReturnValue,"",DataRowVersion.Current,null);
InBlock.gif                    db.ExecuteNonQuery(cmdWrapper);
InBlock.gif                    
int result = (int)cmdWrapper.GetParameterValue("RetVal");
InBlock.gif                    
return (result == 0);
ExpandedSubBlockEnd.gif                }

InBlock.gif                
catch(PensamDBException ex)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    
throw ex;
ExpandedSubBlockEnd.gif                }

InBlock.gif                
catch(Exception ex)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    
// Rollback transaction 
InBlock.gif
                    transaction.Rollback();
InBlock.gif                    
throw new PensamDBException("Fejl i opret ny indebertning fra a older one",ex);
ExpandedSubBlockEnd.gif                }
 
InBlock.gif                
finally
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    connection.Close(); 
ExpandedSubBlockEnd.gif                }

ExpandedSubBlockEnd.gif            }

ExpandedBlockEnd.gif        }

转载于:https://www.cnblogs.com/zengdj/archive/2005/07/17/194713.html

python023基于Python旅游景点推荐系统带vue前后端分离毕业源码案例设计 1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。 1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。 1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值