. Net connects to the ACCESS database, and an error is reported when the webpage is continuously refreshed.

Source: Internet
Author: User
PublicclassDB {publicstaticOleDbConnectionConn; publicstaticstringConnString; connection string publicDB () {TODO: add the constructor logic here} else () {ConnStringProviderMicrosoft. Jet. OLEDB.4

Public class DB {public static OleDbConnection Conn; public static string ConnString; // connection string public DB () {// TODO: add the constructor logic //} public static OleDbConnection Getconn () {ConnString = "Provider = Microsoft. jet. OLEDB.4

Public class DB
{
Public static OleDbConnection Conn;
Public static string ConnString; // connection string

Public DB ()
{
//
// TODO: add the constructor logic here
//
}

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;

}
// ================================================ ==============
// Function Description: Shut down the database
// Time: 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 ();
}
}
// ================================================ ==============
// Function Description: Execute an SQL statement
// Input parameter: SQL, The queried SQL statement
// Time: 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 ();
}

}
// ================================================ ==============
// Function Description: Get DATASET
// Input parameter: SQL, The queried SQL statement
// Return value: DataSet
// Time: 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 ();
}
}
// ================================================ ==============
// Function Description: Get DATASET1
// Input parameter: SQL, The queried SQL statement
// Return value: DataSet
// Time: 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 ();
}
}
// ================================================ ==============
// Function Description: obtains the data of a field.
// Input parameter: SQL, The queried SQL statement
// Return value: hang
// Time: 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 ();
}

}
// ================================================ ==============
// Function Description: bind data to the DATAGRIG without sorting
// Input parameters: SQL, The queried SQL statement; dg, the DATAGRID Control to be bound
// Return value: None
// Time: 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 ();
}
}
// ================================================ ==============
// Function Description: bind data to the DropDownList without sorting
// Input parameters: SQL, The queried SQL statement; dg, the DATAGRID Control to be bound
// Return value: None
// Time: 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 ();
}
}
// ================================================ ==============
// Function Description: bind data to the RadioButtonList without sorting.
// Input parameters: SQL, The queried SQL statement; dg, the DATAGRID Control to be bound
// Return value: None
// Time: 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 ();
}
}
// ================================================ ==============
// Function Description: bind data to the GridView without sorting
// Input parameters: SQL, The queried SQL statement; dg, the DATAGRID Control to be bound
// Return value: None
// Time: 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 ();
}
}
// ================================================ ==============
// Function Description: bind data to datalist without sorting
// Input parameters: SQL, The queried SQL statement; dl, The datalist control to be bound
// Return value: None
// Time: 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 ();
}
}
// ================================================ ==============
// Function Description: bind data to the repeater without sorting
// Input parameters: SQL, The queried SQL statement; dl, the repeater control to be bound
// Return value: None
// Time: 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 ();
}
}
// ================================================ ==============
// Function Description: bind data to listbox
// Input parameters: SQL, The queried SQL statement; listb, The listbox control to be bound
// Return value: None
// Time: 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 ();
}
}
///


/// Return the encoding result of the HTML string
///
/// String
/// Encoding result
Public static string HtmlEncode (string str)
{
Return HttpUtility. HtmlEncode (str );
}

///


/// Return the decoding result of the HTML string
///
/// String
/// Decoding result
Public static string HtmlDecode (string str)
{
Return HttpUtility. HtmlDecode (str );
}
///
/// Check for SQL dangerous characters
///
/// To judge a string
/// Judgment result
Public static bool IsSafeSqlString (string str)
{

Return! Regex. isMatch (str, @ "[-|; |, |\/ | \ (| \) | \ [| \] | \} | \ {| % | @ | \ * |! | \ '] ");
}
///


/// Detect user logon.
///
///
///
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;
}

}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.