private static string Constr;
public static OleDbConnection CNN;
OleDbDataAdapter da;
OleDbCommandBuilder CB;
OleDbCommand cmd;
Constructors
#region Initialize
Public Operatedb ()
{
//
TODO: Add constructor logic here
//
Cnn=new OleDbConnection ();
Da=new OleDbDataAdapter ();
You must have the following row when you do not need to update the OleDbCommand object to the database
Cb=new OleDbCommandBuilder (DA);
Cmd=new OleDbCommand ();
}
#endregion Initialize
Connection string
#region get&setconnectionstring
<summary>
Get connection string
</summary>
public string Myconstr
{
get {return constr;}
set {constr = value;}
}
#endregion get&setconnectionstring
Get the name of the table
#region Acquiretablenames
<summary>
Gets the collection of table names in the database
</summary>
<returns></returns>
Public DataTable tablescollection ()
{
DataTable tbl=new DataTable ();
Try
{
<summary>
Queries that populate a DataTable
</summary>
<param name= "Tblname" > Datasheet (you must enter a name or view that exists in the database) </param>
<param name= "sqlstr" >sql statement </param>
<returns> record number of bars </returns>
public int Select (DataTable tblname,string sqlstr)
{
int i=0;
Try
// {
//
Tblname.clear ();
Da. Dispose ();
if (CNN). connectionstring== "")
Cnn. CONNECTIONSTRING=CONSTR;
if (CNN). State!=connectionstate.open)
Cnn. Open ();
OleDbCommand cmd=new OleDbCommand ("select * from" +tblname.tablename+ "where" +sqlstr,cnn);
Cmd. CONNECTION=CNN;
Cmd.commandtype=commandtype.text;
cmd.commandtext= "SELECT * from" +tblname.tablename+ "where" +SQLSTR;
Da. Selectcommand=cmd;
Insert Record
#region Insert (use CommandBuilder)
<summary>
Insert record (with OleDbCommandBuilder)
</summary>
<param name= "Tblname" > Datasheet </param>
<param name= "NewRow" > New row corresponding to the field in the table </param>
Number of rows affected by <returns> </returns>
public int Insert (DataTable tblname,datarow newrow)
{
Cnn. Open ();
int i=0;
//
Try
// {
How to tell if OleDbDataAdapter has been dispose
If you do not generate a new OleDbDataAdapter, OleDbCommandBuilder, OleDbCommand below,
With the original global da,cb,cmd, only one table can be updated in one operation
OleDbDataAdapter dain=new OleDbDataAdapter ();
OleDbCommandBuilder cbin=new OleDbCommandBuilder (Dain);
OleDbCommand cmdin=new OleDbCommand ("select * from" +TBLNAME.TABLENAME,CNN);
Dain.selectcommand=cmdin;
foreach (DataTable dt in da.) TableMappings)
// {
if (dt. Tablename!=tblname.tablename)
Dt. Clear ();
// }
TBLNAME.ROWS.ADD (NewRow);
Modify
#region Update
<summary>
Modify Records
</summary>
<param name= "Tblname" > Datasheet </param>
<param name= "Strup" >sql statement </param>
Number of rows affected by <returns> </returns>
public int update (DataTable tblname,string Strup)
{
Cnn. Close ();
return i;
}
#endregion Update
Delete
#region del (use CommandBuilder)
<summary>
Delete a record
</summary>
<param name= "Tblname" > Datasheet </param>
<param name= "Strdel" >sql statement </param>
Number of rows affected by <returns> </returns>
public int Delete (DataTable tblname,string strdel)//strdel is delete condition
{
int rows=0;
Automatic updating with the Oledbdataadapter.update method a primary key or unique value must exist in the where
Try
// {
//
Cnn. Open ();
Rows=tblname.rows.count;
for (int i=0;i< tblname.rows.count;i++)
{
Tblname.rows[i]. Delete ();
}
Note, as in Da. The following AcceptChanges method was used before update because the record was deleted--failed to update to the database
Tblname.acceptchanges ();
Da. Update (tblname);
//
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.