惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

P
Proofpoint News Feed
云风的 BLOG
云风的 BLOG
Apple Machine Learning Research
Apple Machine Learning Research
Hugging Face - Blog
Hugging Face - Blog
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
Google DeepMind News
Google DeepMind News
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
雷峰网
雷峰网
B
Blog
月光博客
月光博客
博客园 - 【当耐特】
WordPress大学
WordPress大学
Microsoft Azure Blog
Microsoft Azure Blog
I
InfoQ
The GitHub Blog
The GitHub Blog
Engineering at Meta
Engineering at Meta
Jina AI
Jina AI
博客园 - Franky
MyScale Blog
MyScale Blog
H
Hackread – Cybersecurity News, Data Breaches, AI and More
Last Week in AI
Last Week in AI
B
Blog RSS Feed
H
Help Net Security

博客园 - DingJun

Javascript 继承方法3 Javascript 继承方法2 Javascript 继承方法1 Call web service from excel Cannot load type (加载页面出错) 几个主流的浏览器引擎及判定 防止数据库日志文件增长 配置发布数据库服务器时碰到错误18483 一些有关。NET界面处理与多线程的文章 不可恢复的生成错误 在.Net安装项目中如何判断操作系统的版本 修改dataConfiguration.config文件 SQL Server 使用外部连接 在.NET下利用目录服务操纵本机用户和用户组 System.windows.forms.datagrid控件使用技巧 读取配置文件中的自定义节 区域设置与格式化(1) 默认的 IIS MIME 类型关联 在.NET中使用XPath查找指定元素时遇到的麻烦(以dataConfiguration.config为例)
使用Data access block
DingJun · 2005-07-17 · via 博客园 - DingJun

一、配置Data block所需参数
1,应用程序的配置文件(*.exe.config或者*.dll.config或者Web.config)

2,配置数据库连接串(dataConfiguration.config)

二、执行Sql语句

三、调用存储过程
1、插入新记录并从存储过程获取返回值
存储过程:

调用代码:

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

2、返回记录集并获取存储过程返回值
存储过程:

调用代码:

3、返回记录集并通过输出参数获取返回值
存储过程:

调用代码:

4、DataRearder与输出参数
存储过程:

调用代码:

5、DataRearder与返回值
存储过程:

调用代码:

四、事务处理:

public static bool OpretRapportFraskabelon (string CVR, int maanedValoer)
        
{
            
try
            
{
                db 
= DatabaseFactory.CreateDatabase();
            }

            
catch(Exception ex)
            
{
                
throw new PensamDBException("Fejl i opret databasen",ex); 
            }

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

                
catch(PensamDBException ex)
                
{
                    
throw ex;
                }

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

            }

        }