|
using System;
using System.Data;
using System.Data.SqlClient;
namespace news.common
{
/ **
* -----------------
* Database connection / operation class
* ----------------
* /
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 connection string * /
private string errInfo = "";
/ * ---------------------- * /
public DBClass ()
{
}
/ * Database operation exception information read-only attribute * /
public string ErrInfo
{
get
{
return errInfo;
}
}
/ * Sql statement to operate * /
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" +
"Error source:" + e.Errors [i] .Source + "\n" +
"Procedure:" + e.Errors [i] .Procedure;
}
conn.Close ();
}
}
/ * For form binding * /
public void dataView ()
{
connDb ();
dad = new SqlDataAdapter (sql, conn);
ds = new DataSet ();
dad.Fill (ds);
DataView dv = new DataView (ds.Tables [0]);
}
/ * Execute SQL statements and return results * /
public void readerData ()
{
connDb ();
comm = new SqlCommand (sql, conn);
dr = comm.ExecuteReader ();
}
/ * Execute SQL statement without returning results * /
public void exeSql ()
{
connDb ();
comm = new SqlCommand (sql, conn);
comm.ExecuteNonQuery ();
}
/ * Close link * /
public void clear ()
{
conn.Close ();
}
}
} |
|