C # writes the database operation Class!

Source: Internet
Author: User
Data | database
For some reason, software-dependent database software will be replaced! If the database's open and operational code appears on each page, the code changes that come with the replacement of the database software will be quite cumbersome. So write the database operation code as a class, this will not happen and can reduce the amount of code. The following is the source code

Using System;
Using System.Data;
Using System.Data.SqlClient;

Namespace News.common
{
/**
* -----------------
* Database Join/Action class
* Created in 2005-03-18
* Ryan_bin@126.com
* ----------------
*/

public class Dbclass
{
/*----global variable definition------* *

Private SqlConnection Conn;
Private SqlCommand comm;
Public SqlDataReader Dr;
Public DataSet DS;
Public SqlDataAdapter dad;
private String sql;
private string ConnStr; /* Database Join String * *
private string errinfo = "";
/*----------------------*/

Public Dbclass ()
{
}
/* Database operation exception Information read-only property * *
public string Errinfo
{
Get
{
return errinfo;
}
}
/* The SQL statement to be manipulated * *
public string SQL
{
get{
return SQL;
}
set{
sql = value;
}
}
/* Database Link String * *
public string ConnStr
{
Get
{
return connstr;
}
Set
{
ConnStr = value;
}
}
private void Conndb ()
{
conn = new SqlConnection (CONNSTR);
Try
{
Conn. Open ();
}
catch (SqlException e)
{
for (int i=0;i<e.errors.count;i++)
{
Errinfo + = "Error serial number:" +i+ "\ n" +
"Error message:" +e.errors[i]. Message+ "\ n" +
"Source of error:" +e.errors[i]. Source+ "\ n" +
"Program:" +e.errors[i]. Procedure;
}
Conn. Close ();
}
}
/* for form bindings/*
public void DataView ()
{
Conndb ();
Dad = New SqlDataAdapter (Sql,conn);
ds = new DataSet ();
Dad. Fill (DS);
DataView dv = new DataView (ds. Tables[0]);
}
/* Execute SQL statement, return result * *
public void Readerdata ()
{
Conndb ();
Comm = new SqlCommand (sql,conn);
Dr = Comm. ExecuteReader ();
}
/* Execute SQL statement, do not return result * *
public void Exesql ()
{
Conndb ();
Comm = new SqlCommand (sql,conn);
Comm. ExecuteNonQuery ();
}
/* Close Link * *
public void Clear ()
{
Conn. Close ();
}
}
}



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.