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

推荐订阅源

酷 壳 – CoolShell
酷 壳 – CoolShell
G
Google Developers Blog
V
V2EX
美团技术团队
H
Help Net Security
月光博客
月光博客
爱范儿
爱范儿
Engineering at Meta
Engineering at Meta
The Cloudflare Blog
U
Unit 42
大猫的无限游戏
大猫的无限游戏
Recent Announcements
Recent Announcements
A
About on SuperTechFans
博客园 - Franky
The GitHub Blog
The GitHub Blog
N
Netflix TechBlog - Medium
人人都是产品经理
人人都是产品经理
博客园 - 司徒正美
MyScale Blog
MyScale Blog
B
Blog
雷峰网
雷峰网
Y
Y Combinator Blog
云风的 BLOG
云风的 BLOG
T
The Blog of Author Tim Ferriss

博客园 - 狂闪工作室

Subject类 Questions类 Products类 Members类 Member类 Info类 Images类 Category类 Dlh21数据库备份 文章集萃 网站方案 网址收集 TreeView小结 仿照csdn左面的菜单的ASP+数据库无限级树菜单代码分享 树型论坛的快速算法 如何在DataGrid前加一个列让其id按顺序排列,而非绑定的id字段的乱七八糟的排序??? 如何把两个并列文本框整合到一个下拉框中? 图片上传自动生成缩略图VB组件!
[原创]用ASP.NETN层结构封装的数据层访问基类
狂闪工作室 · 2005-03-02 · via 博客园 - 狂闪工作室

using System; 
using System.Data; 
using System.Data.SqlClient; 
using System.Configuration; 

namespace Db 

 
/// <summary> 
 
/// Base 的摘要说明。 
 
/// </summary> 

 public class Base 
 

  
public Base() 
  

   
// 
   
// TODO: 在此处添加构造函数逻辑 
   
// 
  }
 

  
protected static SqlConnection conn =new SqlConnection(ConfigurationSettings.AppSettings["dsn"]); 
  
protected static SqlCommand cmd = new SqlCommand(strSp,conn); 
  
protected static SqlDataAdapter da = new SqlDataAdapter(); 
  
protected static DataSet ds = new DataSet(); 
  
protected static DataView dv = new DataView(); 
  
protected static SqlDataReader dr; 
  
protected static SqlParameter[] prams; 

  
protected static string strSp;

  
protected static SqlDataReader drSelectAll(string strSp) 
  

   cmd.CommandType 
= CommandType.StoredProcedure; 

   conn.Open(); 
   dr 
= cmd.ExecuteReader(CommandBehavior.CloseConnection); 
   
return dr; 
  }
//返回一个SqlDataReader 

  
protected static DataSet dsSelectAll(string strSp) 
  

   da.SelectCommand 
= new SqlCommand(strSp,conn); 
   da.SelectCommand.CommandType 
= CommandType.StoredProcedure; 

   da.Fill(ds);

   conn.Open(); 
   
try 
   

    da.SelectCommand.ExecuteNonQuery(); 
    
return ds; 
   }
 
   
catch(Exception ex) 
   

    
throw new Exception(ex.Message); 
   }
 
   
finally 
   

    conn.Dispose(); 
    conn.Close(); 
   }
 
  }
//返回一个SqlDataSet 

  
protected static DataView dvSelectAll(string strSp) 
  

   cmd.CommandType 
= CommandType.StoredProcedure; 
   da.SelectCommand 
= new SqlCommand(strSp,conn); 

   da.Fill(ds); 
    
   conn.Open(); 
   
try 
   

    da.SelectCommand.ExecuteNonQuery(); 
    dv 
= ds.Tables[0].DefaultView; 
    
return dv; 
   }
 
   
catch(Exception ex) 
   

    
throw new Exception(ex.Message); 
   }
 
   
finally 
   

    conn.Dispose(); 
    conn.Close(); 
   }
 
  }
//返回一个DataView 

  
protected static string strCmd(string strSp,SqlParameter[] prams,SqlDataReader dr) 
  

   CreateCmd(strSp,prams,dr); 
   conn.Open(); 
   
try 
   

    cmd.ExecuteNonQuery(); 
    
return "1"
   }
 
   
catch(Exception ex) 
   

    
throw new Exception(ex.Message); 
   }
 
   
finally 
   

    conn.Dispose(); 
    conn.Close(); 
   }
 
  }
//返回一个数据库操作 

  
protected static SqlCommand CreateCmd(string strSp, SqlParameter[] prams,SqlDataReader dr) 
  

   cmd.CommandType 
= CommandType.StoredProcedure; 

   
if (prams != null
   

    
foreach (SqlParameter parameter in prams) 
     cmd.Parameters.Add(parameter); 
   }
 
   cmd.Parameters.Add( 
    
new SqlParameter("ReturnValue", SqlDbType.Int, 4
    ParameterDirection.ReturnValue, 
false00
    
string.Empty, DataRowVersion.Default, null)); 

   
return cmd; 
  }
//返回带参数的命令 

  
protected static bool ExecuteSQLs() 
  

   cmd.CommandType 
= CommandType.StoredProcedure; 

   conn.Open(); 
   
try 
   

    
int i = (int)cmd.ExecuteScalar(); 
    
if(i>0
    

     
return true
    }
 
    
else 
    

     
return false
    }
 
   }
 
   
catch(Exception ex) 
   

    
throw new Exception(ex.Message); 
   }
 
   
finally 
   

    conn.Dispose(); 
    conn.Close(); 
   }
 
  }
//返回第一行的数据操作 
 }
 
}