Shoppingplatform -- database layer

Source: Internet
Author: User
Tags connectionstrings
I. Web. config configuration page

<? XML version = "1.0" encoding = "UTF-8"?>
<Configuration xmlns = "http://schemas.microsoft.com/.NetConfiguration/v2.0">

<Deleetask>
<Add key = "xmlpath" value = "xmlcount. xml"> </Add>
</Appsettings>
<Connectionstrings>
<Add name ="Shopconnectionstring"Connectionstring =" provider = Microsoft. Jet. oledb.4.0; Data Source = G: \ work \ database \ shop. mdb"
Providername = "system. Data. oledb"/>
<System. Web>

Ii. olebase. CS class
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 olebase
/// </Summary>
Public class olebase
{
Protected static string m_strconnection = configurationmanager. connectionstrings ["Shopconnectionstring"]. Tostring ();

Public olebase ()
{

}

///


// execute an SQL statement that does not return a dataset
///
//
//
Public static int executesql (string p_strsql)
{< br> oledbconnection myconnection = New oledbconnection (m_strconnection);
oledbcommand mycmd = new oledbcommand (p_strsql, myconnection);

Try
{
Myconnection. open ();
Mycmd.Executenonquery ();

Return 0;

}< br> catch ( oledbexception E)
{< br> throw new exception (E. message);
}< br> finally
{< br> mycmd. dispose ();
myconnection. close ();
}< BR >}// end executesql

/// <Summary>
/// Execute an SQL statement to determine whether data is returned: If no data is returned, valule unavailable is thrown! "Exception
/// </Summary>
/// <Param name = "p_strsql"> </param>
/// <Returns> </returns>
Public staticIntExecutesqlex (string p_strsql)
{
Oledbconnection myconnection = new oledbconnection (m_strconnection );
Oledbcommand mycmd = new oledbcommand (p_strsql, myconnection );

Try
{
Myconnection. open ();
Oledbdatareader Myreader = Mycmd. executereader ();
If ( Myreader. Read ())
{
Return 0;
}
Else
{
Throw new exception ("valule unavailable! ");
}

}
Catch (oledbexception E)
{
Throw new exception (E. Message );
}
Finally
{
Mycmd. Dispose ();
Myconnection. Close ();
}
} // End executesqlex

/// <Summary>
/// Execute the SQL statement and return the dataset
/// </Summary>
/// <Param name = "p_strsql"> </param>
/// <Returns> </returns>
Public staticDatasetExecutesql4ds (string p_strsql)
{
Oledbconnection myconnection = new oledbconnection (m_strconnection );
Oledbcommand mycmd = new oledbcommand (p_strsql, myconnection );

Try
{
Myconnection. open ();
Oledbdataadapter SDA = new Oledbdataadapter (p_strsql, myconnection );
Dataset DS = new dataset ("ds ");
SDA. Fill (DS );
Return Ds;
}
Catch (oledbexception E)
{
Throw new exception (E. Message );
}
Finally
{
Myconnection. Close ();
}
} // End executesql4ds

/// <Summary>
/// Execute an SQL statement that returns only one numeric value
/// </Summary>
/// <Param name = "p_strsql"> </param>
/// <Returns> </returns>
Public staticIntExecutesql4value (string p_strsql)
{
Oledbconnection myconnection = new oledbconnection (m_strconnection );
Oledbcommand mycmd = new oledbcommand (p_strsql, myconnection );

Try
{
Myconnection. open ();
Object r = mycmd. Executescalar ();
If (object. Equals (R, null ))
{
Throw new exception ("value unavailable! ");
}
Else
{
Return (INT) R;
}

}
Catch (oledbexception E)
{
Throw new exception (E. Message );
}
Finally
{
Mycmd. Dispose ();
Myconnection. Close ();
}
} // End executesql4value

/// <Summary>
/// Execute an SQL statement that returns only one data. The returned data can be of any type.
/// </Summary>
/// <Param name = "p_strsql"> </param>
/// <Returns> </returns>
Public staticObjectExecutesql4valueex (string p_strsql)
{
Oledbconnection myconnection = new oledbconnection (m_strconnection );
Oledbcommand mycmd = new oledbcommand (p_strsql, myconnection );

Try
{
Myconnection. open ();
Object r = mycmd. executescalar ();
If (object. Equals (R, null ))
{
Throw new exception ("value unavailable! ");
}
Else
{
Return R;
}

}
Catch (oledbexception E)
{
Throw new exception (E. Message );
}
Finally
{
Mycmd. Dispose ();
Myconnection. Close ();
}
} // End executesql4valueex

/// <Summary>
/// Execute a set of SQL statements that do not return a dataset
/// </Summary>
/// <Param name = "p_strsql"> </param>
/// <Returns> </returns>
Public staticIntExecutesql (String []P_strsql)
{
Oledbconnection myconnection = new oledbconnection (m_strconnection );
Oledbcommand mycmd = new oledbcommand ();
Int J = p_strsql.length;

Try
{
Myconnection. open ();

}
Catch (oledbexception E)
{
Throw new exception (E. Message );
}
Oledbtransaction mytrans = myconnection. begintransaction ();
Try
{
Mycmd. Connection = myconnection;
Mycmd. Transaction = mytrans;
Foreach (string STR in p_strsql)
{
Mycmd. commandtext = STR;
Mycmd. executenonquery ();
}
Mytrans. Commit ();
Return 0;
}
Catch (oledbexception E)
{
Mytrans. rollback ();
Throw new exception (E. Message );
}

Finally
{
Mycmd. Dispose ();
Myconnection. Close ();
}
} // End executesql

}

Iii. shujuku. CS class

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>
/// Shujuku Summary
/// </Summary>
Public class shujuku
{
PrivateOledbconnectionM_myconnection = NULL;

Public shujuku ()
{

}

Private void open ()
{
// Open the database connection
If (m_myconnection = NULL)
{
M_myconnection = new oledbconnection(Configurationsettings.Appsettings["Shopconnectionstring"]);

}< br> If (m_myconnection.state = connectionstate. closed)
{< br> m_myconnection.open ();

}< BR >}// end open

private void close ()
{< br> If (m_myconnection! = NULL)
{< br> If (m_myconnection.state = connectionstate. open)
{< br> m_myconnection.close ();
}< BR >}// end close

Private void disponse ()
{
If (m_myconnection! = NULL)
{
M_myconnection.dispose ();
M_myconnection = NULL;
}
}
}

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.