Access generic data category (Asp.net 2.0 C #)

Source: Internet
Author: User
From: http://www.cnblogs.com/enuosky/archive/2006/05/15/400879.aspx

Modify the data volume of a classic SQL server that was previously collected.
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>
/// Summary of dataaccess
/// </Summary>
Public class dataaccess
{
Protected static oledbconnection conn = new oledbconnection ();
Protected static oledbcommand comm = new oledbcommand ();
Public dataaccess ()
{
// Init
}
Private Static void openconnection ()
{
If (conn. State = connectionstate. Closed)
{
Conn. connectionstring = @ "provider = Microsoft. Jet. oledb.4.0; Data Source =" + configurationmanager. etettings ["myconn"]; // set in the web. config file.
Comm. Connection = conn;
Try
{
Conn. open ();
}
Catch (exception E)
{Throw new exception (E. Message );}

}

} // Open the database

Private Static void closeconnection ()
{
If (conn. State = connectionstate. open)
{
Conn. Close ();
Conn. Dispose ();
Comm. Dispose ();
}
} // Close the database

Public static void excutesql (string sqlstr)
{
Try
{
Openconnection ();
Comm. commandtype = commandtype. text;
Comm. commandtext = sqlstr;
Comm. executenonquery ();
}
Catch (exception E)
{
Throw new exception (E. Message );
}
Finally
{Closeconnection ();}
} // Execute the SQL statement

Public static oledbdatareader datareader (string sqlstr)
{
Oledbdatareader DR = NULL;
Try
{
Openconnection ();
Comm. commandtext = sqlstr;
Comm. commandtype = commandtype. text;

Dr = comm. executereader (commandbehavior. closeconnection );
}
Catch
{
Try
{
Dr. Close ();
Closeconnection ();
}
Catch {}
}
Return Dr;
} // Return the oledbdatareader object of the specified SQL statement. Close this object when using it.
Public static void datareader (string sqlstr, ref oledbdatareader Dr)
{
Try
{
Openconnection ();
Comm. commandtext = sqlstr;
Comm. commandtype = commandtype. text;
Dr = comm. executereader (commandbehavior. closeconnection );
}
Catch
{
Try
{
If (Dr! = NULL &&! Dr. isclosed)
Dr. Close ();
}
Catch
{
}
Finally
{
Closeconnection ();
}
}
} // Return the oledbdatareader object of the specified SQL statement. Disable it when using it.

Public static dataset (string sqlstr)
{
Dataset DS = new dataset ();
Oledbdataadapter da = new oledbdataadapter ();
Try
{
Openconnection ();
Comm. commandtype = commandtype. text;
Comm. commandtext = sqlstr;
Da. selectcommand = comm;
Da. Fill (DS );

}
Catch (exception E)
{
Throw new exception (E. Message );
}
Finally
{
Closeconnection ();
}
Return Ds;
} // Return the dataset of the specified SQL statement

Public static void dataset (string sqlstr, ref dataset DS)
{
Oledbdataadapter da = new oledbdataadapter ();
Try
{
Openconnection ();
Comm. commandtype = commandtype. text;
Comm. commandtext = sqlstr;
Da. selectcommand = comm;
Da. Fill (DS );
}
Catch (exception E)
{
Throw new exception (E. Message );
}
Finally
{
Closeconnection ();
}
} // Return the dataset of the specified SQL statement

Public static datatable (string sqlstr)
{
Datatable dt = new datatable ();
Oledbdataadapter da = new oledbdataadapter ();
Try
{
Openconnection ();
Comm. commandtype = commandtype. text;
Comm. commandtext = sqlstr;
Da. selectcommand = comm;
Da. Fill (DT );
}
Catch (exception E)
{
Throw new exception (E. Message );
}
Finally
{
Closeconnection ();
}
Return DT;
} // Return the datatable of the specified SQL statement
Public static void datatable (string sqlstr, ref datatable DT)
{
Oledbdataadapter da = new oledbdataadapter ();
Try
{
Openconnection ();
Comm. commandtype = commandtype. text;
Comm. commandtext = sqlstr;
Da. selectcommand = comm;
Da. Fill (DT );
}
Catch (exception E)
{
Throw new exception (E. Message );
}
Finally
{
Closeconnection ();
}
} // Return the datatable of the specified SQL statement

Public static dataview (string sqlstr)
{
Oledbdataadapter da = new oledbdataadapter ();
Dataview DV = new dataview ();
Dataset DS = new dataset ();
Try
{
Openconnection ();
Comm. commandtype = commandtype. text;
Comm. commandtext = sqlstr;
Da. selectcommand = comm;
Da. Fill (DS );
DV = Ds. Tables [0]. defaultview;
}
Catch (exception E)
{
Throw new exception (E. Message );
}
Finally
{
Closeconnection ();
}
Return DV;
}
// Return the dataview of the specified SQL statement

}

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.