.net连接ACCESS数据库,网页上不停的刷新就报错
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了.net连接ACCESS数据库,网页上不停的刷新就报错,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含10344字,纯文字阅读大概需要15分钟。
内容图文
![.net连接ACCESS数据库,网页上不停的刷新就报错](/upload/InfoBanner/zyjiaocheng/564/a5a43e73c3d3454cafe8b6e92c0c6147.jpg)
public class DB { public static OleDbConnection Conn; public static string ConnString;//连接字符串 public DB() { // // TODO: 在此处添加构造函数逻辑 // } public static OleDbConnection Getconn() { ConnString = "Provider=Microsoft.Jet.OLEDB.4
public class DB
{
public static OleDbConnection Conn;
public static string ConnString;//连接字符串
public DB()
{
//
// TODO: 在此处添加构造函数逻辑
//
}
public static OleDbConnection Getconn()
{
ConnString = "Provider=Microsoft.Jet.OLEDB.4.0;Data source=" + System.Web.HttpContext.Current.Server.MapPath(ConfigurationManager.AppSettings["ConnectionString"].ToString());
Conn = new OleDbConnection(ConnString);
//if (Conn.State.Equals(ConnectionState.Closed))
//{
// Conn.Open();
//}
if (Conn == null)
{
Conn = new OleDbConnection(ConnString);
Conn.Open();
}
else if (Conn.State == System.Data.ConnectionState.Closed)
{
Conn.Open();
}
else if (Conn.State == System.Data.ConnectionState.Broken)
{
Conn.Close();
Conn.Open();
}
return Conn;
}
//=================================================
//功能描述:关闭数据库
//时间:2010.11.10
//=================================================
private static void closeConnection()
{
OleDbConnection conn = DB.Getconn();
OleDbCommand cmd = new OleDbCommand();
if (conn.State == ConnectionState.Open)
{
conn.Close();
conn.Dispose();
cmd.Dispose();
}
}
//=================================================
//功能描述:执行SQL语句
//输入参数:sql,查询的SQL语句
//时间:2010.11.10
//=================================================
public static void execnonsql(string sql)
{
try
{
closeConnection();
OleDbConnection conn = DB.Getconn();
OleDbCommand com = new OleDbCommand(sql, conn);
com.ExecuteNonQuery();
}
catch (Exception e)
{
throw new Exception(e.Message);
}
finally
{
closeConnection();
}
}
//=================================================
//功能描述:获取DATASET
//输入参数:sql,查询的SQL语句
//返回值:DataSet
//时间:2010.11.10
//=================================================
public static DataSet getdataset(string sql)
{
try
{
closeConnection();
OleDbConnection conn = DB.Getconn();
OleDbDataAdapter adp = new OleDbDataAdapter(sql, conn);
DataSet ds = new DataSet();
adp.Fill(ds, "ds");
return ds;
}
catch (Exception e)
{
throw new Exception(e.Message);
}
finally
{
closeConnection();
}
}
//=================================================
//功能描述:获取DATASET1
//输入参数:sql,查询的SQL语句
//返回值:DataSet
//时间:2010.11.10
//=================================================
public static DataSet select(string sql, string tablename)
{
try
{
closeConnection();
OleDbConnection conn = DB.Getconn();
OleDbDataAdapter adp = new OleDbDataAdapter(sql, conn);
DataSet ds = new DataSet();
adp.Fill(ds, tablename);
return ds;
}
catch (Exception e)
{
throw new Exception(e.Message);
}
finally
{
closeConnection();
}
}
//=================================================
//功能描述:获取某个字段数据
//输入参数:sql,查询的SQL语句
//返回值:hang
//时间:2010.11.10
//=================================================
public static string FindString(string sql)
{
try
{
closeConnection();
OleDbConnection conn = DB.Getconn();
OleDbCommand com = new OleDbCommand(sql, conn);
string hang = Convert.ToString(com.ExecuteScalar());
return hang;
}
catch (Exception e)
{
throw new Exception(e.Message);
}
finally
{
closeConnection();
}
}
//=================================================
//功能描述:对DATAGRIG进行数据绑定,无排序
//输入参数:sql,查询的SQL语句;dg,需要绑定的DATAGRID控件
//返回值:无
//时间:2010.11.10
//=================================================
public static void binddatagrid(string sql, DataGrid dg)
{
try
{
DataSet ds = getdataset(sql);
dg.DataSource = ds.Tables[0].DefaultView;
dg.DataBind();
}
catch (Exception e)
{
throw new Exception(e.Message);
}
finally
{
closeConnection();
}
}
//=================================================
//功能描述:对DropDownList进行数据绑定,无排序
//输入参数:sql,查询的SQL语句;dg,需要绑定的DATAGRID控件
//返回值:无
//时间:2010.11.10
//=================================================
public static void bindDropDownList(string sql, DropDownList dl, string class_name, string id)
{
try
{
DataSet ds = getdataset(sql);
dl.DataSource = ds.Tables[0].DefaultView;
dl.DataTextField = class_name;
dl.DataValueField = id;
dl.DataBind();
}
catch (Exception e)
{
throw new Exception(e.Message);
}
finally
{
closeConnection();
}
}
//=================================================
//功能描述:对RadioButtonList进行数据绑定,无排序
//输入参数:sql,查询的SQL语句;dg,需要绑定的DATAGRID控件
//返回值:无
//时间:2010.11.10
//=================================================
public static void bindRadioButtonList(string sql, RadioButtonList rl, string class_name, string id)
{
try
{
DataSet ds = getdataset(sql);
rl.DataSource = ds.Tables[0].DefaultView;
rl.DataTextField = class_name;
rl.DataValueField = id;
rl.SelectedIndex = 0;
rl.DataBind();
}
catch (Exception e)
{
throw new Exception(e.Message);
}
finally
{
closeConnection();
}
}
//=================================================
//功能描述:对GridView进行数据绑定,无排序
//输入参数:sql,查询的SQL语句;dg,需要绑定的DATAGRID控件
//返回值:无
//时间:2010.11.10
//=================================================
public static void bindGridView(string sql, GridView dg)
{
try
{
closeConnection();
OleDbConnection conn = DB.Getconn();
DataSet ds = getdataset(sql);
dg.DataSource = ds.Tables[0].DefaultView;
dg.DataBind();
}
catch (Exception e)
{
throw new Exception(e.Message);
}
finally
{
closeConnection();
}
}
//=================================================
//功能描述:对datalist进行数据绑定,无排序
//输入参数:sql,查询的SQL语句;dl,需要绑定的datalist控件
//返回值:无
//时间:2010.11.10
//=================================================
public static void binddatalist(string sql, DataList dl)
{
try
{
closeConnection();
OleDbConnection conn = DB.Getconn();
DataSet ds = getdataset(sql);
dl.DataSource = ds.Tables[0].DefaultView;
dl.DataBind();
}
catch (Exception e)
{
throw new Exception(e.Message);
}
finally
{
closeConnection();
}
}
//=================================================
//功能描述:对repeater进行数据绑定,无排序
//输入参数:sql,查询的SQL语句;dl,需要绑定的repeater控件
//返回值:无
//时间:2010.11.10
//=================================================
public static void bindrepeater(string sql, Repeater rp)
{
try
{
closeConnection();
OleDbConnection conn = DB.Getconn();
DataSet ds = getdataset(sql);
rp.DataSource = ds.Tables[0].DefaultView;
rp.DataBind();
}
catch (Exception e)
{
throw new Exception(e.Message);
}
finally
{
closeConnection();
}
}
//=================================================
//功能描述:对listbox进行数据绑定
//输入参数:sql,查询的SQL语句;listb,需要绑定的listbox控件
//返回值:无
//时间:2010.11.10
//=================================================
public static void bindlistbox(string sql, ListBox listb, string class_name, string id)
{
try
{
closeConnection();
OleDbConnection conn = DB.Getconn();
DataSet ds = getdataset(sql);
listb.DataSource = ds.Tables[0].DefaultView;
listb.DataTextField = class_name;
listb.DataValueField = id;
listb.DataBind();
}
catch (Exception e)
{
throw new Exception(e.Message);
}
finally
{
closeConnection();
}
}
///
/// 返回 HTML 字符串的编码结果
///
/// 字符串
///
public static string HtmlEncode(string str)
{
return HttpUtility.HtmlEncode(str);
}
///
/// 返回 HTML 字符串的解码结果
///
/// 字符串
///
public static string HtmlDecode(string str)
{
return HttpUtility.HtmlDecode(str);
}
///
/// 检测是否有Sql危险字符
///
/// 要判断字符串
///
public static bool IsSafeSqlString(string str)
{
return !Regex.IsMatch(str, @"[-|;|,|\/|\(|\)|\[|\]|\}|\{|%|@|\*|!|\']");
}
///
/// 检测用户登录。
///
///
///
public static string UserCheck(string username, string userpass)
{
string strsql = "select count(*) from Member where mem_Name='" + username + "' and mem_Password='" + userpass + "'";
OleDbConnection conn = DB.Getconn();
OleDbCommand com = new OleDbCommand(strsql, conn);
string hang = Convert.ToString(com.ExecuteScalar());
return hang;
}
}
内容总结
以上是互联网集市为您收集整理的.net连接ACCESS数据库,网页上不停的刷新就报错全部内容,希望文章能够帮你解决.net连接ACCESS数据库,网页上不停的刷新就报错所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。