C # Database access method, used during an interview.

Source: Internet
Author: User

A database connection string that adds a SqlDataSource control to the page vs can be automatically generated and saved in a webconfig file.

<summary>
Description: Execsql is used to execute SQL statements. Used to perform query operations.
Return value: Whether the operation succeeded (True/false).
Parameters: SQL SQL string
Date Created: 2008-04-08
Create Person: Tianyu
</summary>
Public Boolean Execsql (String sql)
{

using (OleDbConnection con = new OleDbConnection (Sqlcon))
{
Con. Open ();
OleDbCommand DbCommand = new OleDbCommand (sql, con);
Try
{
Dbcommand.executenonquery ();
Con. Close ();
}
Catch
{
Con. Close ();
return false;
}
return true;
}
}

<summary>
Description: GetDataSet data set, return data set
Return Value: DataSet DataSet
Parameters: SQL SQL string, tablename temporary data table name
Date Created: 2008-04-08
Create Person: Tianyu
</summary>
Public System.Data.DataSet GetDataSet (String sql, string tablename)
{
using (OleDbConnection con = new OleDbConnection (Sqlcon))
{
Try
{
Con. Open ();
OleDbDataAdapter dbadapter = new OleDbDataAdapter (sql, con);
DataSet DataSet = new DataSet ();
Dbadapter.fill (DataSet, TableName);
Con. Close ();
return dataset;
}
Catch
{
Con. Close ();
return null;
}
}
}

<summary>
Get a dataset that intercepts a fixed row
</summary>
<param name= "SQL" > executed SQL statements </param>
<param name= "Start" > Start line </param>
<param name= "End" > Ending line </param>
<param name= "TableName" > The name of the temporary table generated </param>
<returns></returns>
Public System.Data.DataSet Getcutdataset (String sql, int start, int end, string tablename)
{
using (OleDbConnection con = new OleDbConnection (Sqlcon))
{
Try
{
Con. Open ();
OleDbDataAdapter dbadapter = new OleDbDataAdapter (sql, con);
DataSet DataSet = new DataSet ();
Dbadapter.fill (DataSet, Start, end, TableName);
Con. Close ();
return dataset;
}
Catch
{
Con. Close ();
return null;
}
}
}

<summary>
Determine whether data exists in the database based on the SQL statement.
The SQL statement to execute cannot include the count () statement.
</summary>
<param name= "SQL" > string to Query </param>
<returns> returns false </returns> if there is data return true
public bool HasData (String sql)
{
using (OleDbConnection con = new OleDbConnection (Sqlcon))
{
OleDbDataReader Dr;
Con. Open ();
OleDbCommand DbCommand = new OleDbCommand (sql, con);
Dr= Dbcommand.executereader ();
Try
{

if (Dr. Read ())
{
Dr. Close ();
Con. Close ();
return true;
}
Else
{
Dr. Close ();
Con. Close ();
return false;
}
}
Catch
{
Dr. Close ();
Con. Close ();
return false;
}

}

}

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.