📄 dbsql.cs
字号:
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.OleDb;
/// <summary>
/// DBsql 的摘要说明
/// </summary>
public class DBsql
{
public OleDbConnection con = null;
public DBsql()
{
//
// TODO: 在此处添加构造函数逻辑
//
}
/// <summary>
/// 打开数据库连接
/// </summary>
public void Open()
{
Page newpage = new Page();
string Path_db = ConfigurationManager.AppSettings["DBPath"];
string Path_conn = "Provider=Microsoft.Jet.OleDb.4.0;Data Source=" + newpage.Server.MapPath("~") + "\\" + Path_db;
if (con == null)
{
con = new OleDbConnection(Path_conn);
}
if (con.State == ConnectionState.Closed)
{
try
{
///打开数据库连接
con.Open();
}
catch (Exception ex)
{
HttpContext.Current.Response.Write(ex.Message);
}
finally
{
///关闭已经打开的数据库连接
}
}
}
/// <summary>
/// 关闭数据库连接
/// </summary>
public void Close()
{
///判断连接是否已经创建
if (con != null)
{
///判断连接的状态是否打开
if (con.State == ConnectionState.Open)
{
con.Close();
}
}
}
/// <summary>
/// 释放资源
/// </summary>
public void Dispose()
{
// 确认连接是否已经关闭
if (con != null)
{
con.Dispose();
con = null;
}
}
/// <summary>
/// 返回OleDbDataReader对象
/// </summary>
/// <returns></returns>
public OleDbDataReader Re_dr(int ID, string tab)
{
string sql = "select * from " + tab + " where YX_ID=" + ID;
OleDbCommand Comm = new OleDbCommand(sql, con);
OleDbDataReader Dr = Comm.ExecuteReader();
return Dr;
}
public OleDbDataReader Re_dr(string sql)
{
OleDbCommand Comm = new OleDbCommand(sql, con);
OleDbDataReader Dr = Comm.ExecuteReader();
return Dr;
}
/// <summary>
/// 执行sql语句没有返回值
/// </summary>
/// <param name="sql"></param>
public void YX_ExecSql(string sql)
{
try
{
OleDbCommand Comm_S = new OleDbCommand(sql, con);
Comm_S.ExecuteNonQuery();
}
catch (Exception ee)
{
HttpContext.Current.Response.Write(ee.Message);
}
finally
{
Close();
}
}
/// <summary>
/// 返回首行首列
/// </summary>
/// <param name="sql"></param>
public int YX_Execint(string sql)
{
int num = 0;
try
{
OleDbCommand Comm_S = new OleDbCommand(sql, con);
num = Convert.ToInt16(Comm_S.ExecuteScalar());
}
catch (Exception ee)
{
HttpContext.Current.Response.Write(ee.Message);
}
finally
{
Close();
}
return num;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -