基于.net平台下数据库访问类(总结)
2014-05-20 20:58
316 查看
基于.net平台下的开发操作数据库方面都会用到数据库,以下介绍几种标准的数据库访问类。
1.组件式开发常用的数据库访问类
namespace Demo
{
class SQLHelp/////数据库访问类
{
//链接数据库字符串
public static string connString = @"server=.; database = *****(数据库名) ;User ID = sa(用户名); Pwd = 123(密码)";
public static readonly SqlConnection connection = new SqlConnection();
//链接数据库,打开并检查
public static void Connect()
{
try
{
connection.ConnectionString = connString;
connection.Open();
}
catch (System.Exception ex)
{
XtraMessageBox.Show(ex.Message, "操作数数据库出错", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
}
}
//关闭数据库链接
public static void CloseConnect()
{
try
{
connection.Close();
connection.Dispose();
}
catch (System.Exception ex)
{
XtraMessageBox.Show(ex.Message, "操作数数据库出错", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
}
}
public static bool ExitSearch(string str)
{
try
{
Connect();
SqlCommand command = new SqlCommand(str, connection);
SqlDataReader dr = command.ExecuteReader();
return dr.Read();
}
catch (System.Exception ex)
{
CloseConnect();
XtraMessageBox.Show(ex.Message, "操作数数据库出错", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
throw ex;
}
finally
{
CloseConnect();
}
}
public static DataTable TableSearch(string str)
{
DataTable dt = new DataTable();
try
{
Connect();
SqlDataAdapter adp = new SqlDataAdapter(str, connection);
adp.Fill(dt);
return dt;
}
catch (System.Exception ex)
{
CloseConnect();
XtraMessageBox.Show(ex.Message, "操作数数据库出错", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
throw ex;
}
finally
{
CloseConnect();
}
}
public static void SqlExecute(string str)
{
try
{
Connect();
SqlCommand command = new SqlCommand(str, connection);
command.ExecuteNonQuery();
}
catch (System.Exception ex)
{
CloseConnect();
XtraMessageBox.Show(ex.Message, "操作数数据库出错", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
throw ex;
}
finally
{
CloseConnect();
}
}
public static int ExecuteRows(string str)
{
try
{
Connect();
SqlCommand command = new SqlCommand(str, connection);
int Rows = Convert.ToInt32(command.ExecuteScalar());
return Rows;
}
catch (System.Exception ex)
{
CloseConnect();
XtraMessageBox.Show(ex.Message, "操作数数据库出错", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
throw ex;
}
finally
{
CloseConnect();
}
}
}
}
2.基于.net平台下的三层架构下的ASP.NET开发用的的数据库访问类
1.组件式开发常用的数据库访问类
namespace Demo
{
class SQLHelp/////数据库访问类
{
//链接数据库字符串
public static string connString = @"server=.; database = *****(数据库名) ;User ID = sa(用户名); Pwd = 123(密码)";
public static readonly SqlConnection connection = new SqlConnection();
//链接数据库,打开并检查
public static void Connect()
{
try
{
connection.ConnectionString = connString;
connection.Open();
}
catch (System.Exception ex)
{
XtraMessageBox.Show(ex.Message, "操作数数据库出错", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
}
}
//关闭数据库链接
public static void CloseConnect()
{
try
{
connection.Close();
connection.Dispose();
}
catch (System.Exception ex)
{
XtraMessageBox.Show(ex.Message, "操作数数据库出错", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
}
}
public static bool ExitSearch(string str)
{
try
{
Connect();
SqlCommand command = new SqlCommand(str, connection);
SqlDataReader dr = command.ExecuteReader();
return dr.Read();
}
catch (System.Exception ex)
{
CloseConnect();
XtraMessageBox.Show(ex.Message, "操作数数据库出错", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
throw ex;
}
finally
{
CloseConnect();
}
}
public static DataTable TableSearch(string str)
{
DataTable dt = new DataTable();
try
{
Connect();
SqlDataAdapter adp = new SqlDataAdapter(str, connection);
adp.Fill(dt);
return dt;
}
catch (System.Exception ex)
{
CloseConnect();
XtraMessageBox.Show(ex.Message, "操作数数据库出错", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
throw ex;
}
finally
{
CloseConnect();
}
}
public static void SqlExecute(string str)
{
try
{
Connect();
SqlCommand command = new SqlCommand(str, connection);
command.ExecuteNonQuery();
}
catch (System.Exception ex)
{
CloseConnect();
XtraMessageBox.Show(ex.Message, "操作数数据库出错", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
throw ex;
}
finally
{
CloseConnect();
}
}
public static int ExecuteRows(string str)
{
try
{
Connect();
SqlCommand command = new SqlCommand(str, connection);
int Rows = Convert.ToInt32(command.ExecuteScalar());
return Rows;
}
catch (System.Exception ex)
{
CloseConnect();
XtraMessageBox.Show(ex.Message, "操作数数据库出错", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
throw ex;
}
finally
{
CloseConnect();
}
}
}
}
2.基于.net平台下的三层架构下的ASP.NET开发用的的数据库访问类
相关文章推荐
- .NET平台访问远程SQL Server 2000数据库的方法
- 基于.NET平台的Windows编程实战(四)—— 数据库操作类的编写
- WinCE平台搭建--基于数据库的.NET应用程序开发(wince6.0+vs2008+sqlce)
- 艾伟_转载:基于.NET平台的Windows编程实战(四)—— 数据库操作类的编写
- 基于.NET平台的分层架构实战(二)——需求分析与数据库设计
- .Net平台下基于角色的访问控制系统(转)
- WinCE平台搭建--基于数据库的.NET应用程序开发(wince6.0+vs2008+sqlce)
- 艾伟:基于.NET平台的Windows编程实战(四)—— 数据库操作类的编写
- 基于.NET平台的分层架构实战(二)——需求分析与数据库设计
- 基于.NET平台的分层架构实战(二)——需求分析与数据库设计
- 基于.NET平台的Windows编程实战(八)— 数据库管理及其他辅助功能的实现
- .NET跨平台:在Linux上基于ASP.NET 5用EF7生成数据库
- 基于.NET平台的Web应用中数据访问技术的深入探索(转)
- 基于.NET平台的Windows编程实战(二)—— 需求分析与数据库设计
- .NET 部署问题之完美解决方案总结(访问IIS元数据库失败,找不到页面,应用程序错误解决)
- 基于.NET平台的分层架构实战(二)——需求分析与数据库设计
- 创建基于.NET的通用数据库访问层
- 基于.NET平台的分层架构实战(二)——需求分析与数据库设计
- 艾伟:基于.NET平台的Windows编程实战(二)—— 需求分析与数据库设计
- 基于.NET平台的Windows编程实战(八)— 数据库管理及其他辅助功能的实现