Register Register Member Login Member Login Member Login Forgot Password ??
PHP , ASP , ASP.NET, VB.NET, C#, Java , jQuery , Android , iOS , Windows Phone
 

Registered : 109,027

HOME > C# (.NET) > ASP.NET Microsoft SQL Server (System.Data.SqlClient) > (C#) ASP.NET SQL Server Database Class (Visual Studio .Net 2003 - .NET 1.1)



Clound SSD Virtual Server

(C#) ASP.NET SQL Server Database Class (Visual Studio .Net 2003 - .NET 1.1)

(C#) ASP.NET SQL Server Database Class (Visual Studio .Net 2003 - .NET 1.1) สำหรับ Database Class ตัวนี้เป็นของ Framework 1.1 ซึ่งผมได้พัฒนาบน Visual Studio .Net 2003 เหมาะสมกับผู้ที่ต้องการสร้าง Database Class ไว้สำหรับจัดการฐานข้อมูล SQL Server ซึ่งผมได้ทำการออกแบบไว้มีฟังก์ชั่นที่ครบครับ สามารถทำการ เพิ่ม/ลบ/แก้ไข/อ่าน ข้อมูลจากฐานข้อมูลได้อย่างง่ายดายครับ

Instance NameSpace

C#
Using System.Data; 
Using System.Data.SqlClient;


ASP.NET & System.Data.SqlClient

Language Code : VB.NET || C#

clsDatabase.cs

using System;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;


public class clsDatabase
{

	private SqlConnection objConn;
	private SqlCommand objCmd;
	private SqlTransaction Trans;
	private String strConnString;

	public clsDatabase()
	{
		strConnString = System.Configuration.ConfigurationSettings.AppSettings["ConnectionString"];
	}

	public SqlDataReader QueryDataReader(String strSQL)
	{
		SqlDataReader dtReader;
		objConn = new SqlConnection();
		objConn.ConnectionString = strConnString;
		objConn.Open();

		objCmd = new SqlCommand(strSQL, objConn);
		dtReader = objCmd.ExecuteReader();
		return dtReader; //*** Return DataReader ***//
	}

	public DataSet QueryDataSet(String strSQL)
	{
		DataSet ds = new DataSet();
		SqlDataAdapter dtAdapter = new SqlDataAdapter();
		objConn = new SqlConnection();
		objConn.ConnectionString = strConnString;
		objConn.Open();

		objCmd = new SqlCommand();
		objCmd.Connection = objConn;
		objCmd.CommandText = strSQL;
		objCmd.CommandType = CommandType.Text;

		dtAdapter.SelectCommand = objCmd;
		dtAdapter.Fill(ds);
		return ds;   //*** Return DataSet ***//
	}

	public DataTable QueryDataTable(String strSQL)
	{
		SqlDataAdapter dtAdapter;
		DataTable dt = new DataTable();
		objConn = new SqlConnection();
		objConn.ConnectionString = strConnString;
		objConn.Open();

		dtAdapter = new SqlDataAdapter(strSQL, objConn);
		dtAdapter.Fill(dt);
		return dt; //*** Return DataTable ***//
	}

	public Boolean QueryExecuteNonQuery(String strSQL)
	{
		objConn = new SqlConnection();
		objConn.ConnectionString = strConnString;
		objConn.Open();

		try
		{
			objCmd = new SqlCommand();
			objCmd.Connection = objConn;
			objCmd.CommandType = CommandType.Text;
			objCmd.CommandText = strSQL;

			objCmd.ExecuteNonQuery();
			return true; //*** Return True ***//
		}
		catch (Exception)
		{
			return false; //*** Return False ***//
		}
	}


	public Object QueryExecuteScalar(String strSQL)
	{
		Object obj;
		objConn = new SqlConnection();
		objConn.ConnectionString = strConnString;
		objConn.Open();

		try
		{
			objCmd = new SqlCommand();
			objCmd.Connection = objConn;
			objCmd.CommandType = CommandType.Text;
			objCmd.CommandText = strSQL;

			obj = objCmd.ExecuteScalar();  //*** Return Scalar ***//
			return obj;
		}
		catch (Exception)
		{
			return null; //*** Return Nothing ***//
		}
	}

	public void TransStart()
	{
		objConn = new SqlConnection();
		objConn.ConnectionString = strConnString;
		objConn.Open();
		Trans = objConn.BeginTransaction(IsolationLevel.ReadCommitted);
	}


	public void TransExecute(String strSQL)
	{
		objCmd = new SqlCommand();
		objCmd.Connection = objConn;
		objCmd.Transaction = Trans;
		objCmd.CommandType = CommandType.Text;
		objCmd.CommandText = strSQL;
		objCmd.ExecuteNonQuery();
	}


	public void TransRollBack()
	{
		Trans.Rollback();
	}

	public void TransCommit()
	{
		Trans.Commit();
	}

	public void Close()
	{
		objConn.Close();
		objConn = null;
	}

}









Default.aspx.cs

using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;


public class _Default : System.Web.UI.Page
{
	protected System.Web.UI.WebControls.DataGrid myDataGrid1;
	protected System.Web.UI.WebControls.DataGrid myDataGrid2;
	protected System.Web.UI.WebControls.DataGrid myDataGrid3;
	protected System.Web.UI.WebControls.Label lblHeaderCustomerID;
	protected System.Web.UI.WebControls.Label lblCustomerID;
	protected System.Web.UI.WebControls.Label lblHeaderName;
	protected System.Web.UI.WebControls.Label lblName;
	protected System.Web.UI.WebControls.Label lblHeaderEmail;
	protected System.Web.UI.WebControls.Label lblEmail;
	protected System.Web.UI.WebControls.Label lblHeaderCountryCode;
	protected System.Web.UI.WebControls.Label lblCountryCode;
	protected System.Web.UI.WebControls.Label lblHeaderBudget;
	protected System.Web.UI.WebControls.Label lblBudget;
	protected System.Web.UI.WebControls.Label lblHeaderUsed;
	protected System.Web.UI.WebControls.Label lblUsed;
	protected System.Web.UI.WebControls.Label lblText;

	#region Web Form Designer generated code
	override protected void OnInit(EventArgs e)
	{
		InitializeComponent();
		base.OnInit(e);
	}
		
	private void InitializeComponent()
	{    
		this.Load += new System.EventHandler(this.Page_Load);

	}
	#endregion
	
	clsDatabase clsDB = new clsDatabase();
	private void Page_Load(object sender, System.EventArgs e)
	{
		myDataReader();
		myDataSet();
		myDataTable();
		myQueryExecuteScalar();
		myExecuteNonQuery();
		myExecuteTransaction();
	}

	//*** DataReader ***//
	void myDataReader()
	{
		String strSQL;
		SqlDataReader dtReader;
		strSQL = "SELECT * FROM customer ";
		dtReader = clsDB.QueryDataReader(strSQL);
		this.myDataGrid1.DataSource = dtReader;
		this.myDataGrid1.DataBind();

		//*** Bind Rows ***//
		/*
		if(dtReader.HasRows == true)
		{
			dtReader.Read();
			this.lblCustomerID.Text = dtReader["CustomerID"].ToString();
			this.lblName.Text = dtReader["Name"].ToString();
			this.lblEmail.Text = dtReader["Email"].ToString();
			this.lblCountryCode.Text = dtReader["CountryCode"].ToString();
			this.lblBudget.Text = dtReader["Budget"].ToString();
			this.lblUsed.Text = dtReader["Used"].ToString();
		}
		*/

		clsDB.Close();
	}

	//*** DataSet ***//
	void myDataSet()
	{
		String strSQL;
		DataSet ds;
		strSQL = "SELECT * FROM customer ";
		ds = clsDB.QueryDataSet(strSQL);
		this.myDataGrid2.DataSource = ds.Tables[0].DefaultView;
		this.myDataGrid2.DataBind();

		//*** Bind Rows ***//
		if(ds.Tables[0].Rows.Count > 0)
		{
			this.lblCustomerID.Text = ds.Tables[0].Rows[0]["CustomerID"].ToString();
			this.lblName.Text = ds.Tables[0].Rows[0]["Name"].ToString();
			this.lblEmail.Text = ds.Tables[0].Rows[0]["Email"].ToString();
			this.lblCountryCode.Text = ds.Tables[0].Rows[0]["CountryCode"].ToString();
			this.lblBudget.Text = ds.Tables[0].Rows[0]["Budget"].ToString();
			this.lblUsed.Text = ds.Tables[0].Rows[0]["Used"].ToString();
		}
		clsDB.Close();
	}

	//*** DataTable ***//
	void myDataTable()
	{
		String strSQL;
		DataTable dt;
		strSQL = "SELECT * FROM customer ";
		dt = clsDB.QueryDataTable(strSQL);
		this.myDataGrid3.DataSource = dt;
		this.myDataGrid3.DataBind();

		//*** Bind Rows ***//
		if(dt.Rows.Count > 0)
		{
			this.lblCustomerID.Text = dt.Rows[0]["CustomerID"].ToString();
			this.lblName.Text = dt.Rows[0]["Name"].ToString();
			this.lblEmail.Text = dt.Rows[0]["Email"].ToString();
			this.lblCountryCode.Text = dt.Rows[0]["CountryCode"].ToString();
			this.lblBudget.Text = dt.Rows[0]["Budget"].ToString();
			this.lblUsed.Text = dt.Rows[0]["Used"].ToString();
		}
		clsDB.Close();
	}

	//*** Execute Scalar ***//
	void myQueryExecuteScalar()
	{
		String strSQL;
		strSQL = "SELECT MAX(Budget) FROM customer ";
		this.lblText.Text = clsDB.QueryExecuteScalar(strSQL).ToString();
		clsDB.Close();
	}

	//*** ExecuteNonQuery ***//
	void myExecuteNonQuery()
	{
		String strSQL1, strSQL2, strSQL3;

		//*** Insert ***//
		strSQL1 = "INSERT INTO customer (CustomerID,Name,Email,CountryCode,Budget,Used) " +
		" VALUES('C005','Weerachai Nukitram','[email protected]','TH','200000','100000')";
		if(clsDB.QueryExecuteNonQuery(strSQL1) == true)
		{
			//*** Condition Success ***//
		}
			else
		{
			//*** Condition Error ***//
		}
		clsDB.Close();

		//*** Update ***//
		strSQL2 = "UPDATE customer SET Budget = '3000000' WHERE CustomerID = 'C005' ";
		if(clsDB.QueryExecuteNonQuery(strSQL2) == true)
		{
			//*** Condition Success ***//
		}
			else
		{
			//*** Condition Error ***//
		}
		clsDB.Close();

		//*** Delete ***//
		strSQL3 = "DELETE FROM customer WHERE CustomerID = 'C005' ";
		if(clsDB.QueryExecuteNonQuery(strSQL3) == true)
		{
			//*** Condition Success ***//
		}
			else
		{
			//*** Condition Error ***//
		}
		clsDB.Close();
	}

	//*** Execute Transaction ***'
	void myExecuteTransaction()
	{
		String strSQL1, strSQL2, strSQL3;

		//*** Start Transaction ***//
		clsDB.TransStart();

		try
		{

			//*** Insert ***//
			strSQL1 = "INSERT INTO customer (CustomerID,Name,Email,CountryCode,Budget,Used) " +
			" VALUES('C005','Weerachai Nukitram','[email protected]','TH','200000','100000')";
			clsDB.TransExecute(strSQL1); //*** Execute Query 1 ***//

			//*** Update ***//
			strSQL2 = "UPDATE customer SET Budget = '3000000' WHERE CustomerID = 'C005' ";
			clsDB.TransExecute(strSQL2); //*** Execute Query 2 ***//

			//*** Delete ***//
			strSQL3 = "DELETE FROM customer WHERE CustomerID = 'C005' ";
			clsDB.TransExecute(strSQL3); //*** Execute Query 3 **//

			//*** Commit Transaction ***//
			clsDB.TransCommit();
		}
		catch (Exception)
		{
			//*** RollBack Transaction ***//
			clsDB.TransRollBack();
		}

		clsDB.Close();		
	}
}









   
Share


ช่วยกันสนับสนุนรักษาเว็บไซต์ความรู้แห่งนี้ไว้ด้วยการสนับสนุน Source Code 2.0 ของทีมงานไทยครีเอท


ลองใช้ค้นหาข้อมูล


   


Bookmark.   
       
  By : ThaiCreate.Com Team (บทความเป็นลิขสิทธิ์ของเว็บไทยครีเอทห้ามนำเผยแพร่ ณ เว็บไซต์อื่น ๆ)
  Score Rating :  
  Create/Update Date : 2008-10-30 07:45:22 / 2017-03-29 10:32:43
  Download : Download  (C#) ASP.NET SQL Server Database Class (Visual Studio .Net 2003 - .NET 1.1)
 Sponsored Links / Related

 
(C#) ASP.NET SQL Server Connect to Database
Rating :

 
(C#) ASP.NET SQL Server List Table Properties
Rating :

 
(C#) ASP.NET SQL Server List Record
Rating :

 
(C#) ASP.NET SQL Server & OleDb (System.Data.OleDb)
Rating :

 
(C#) ASP.NET SQL Server & Odbc (System.Data.Odbc)
Rating :

 
(C#) ASP.NET SQL Server Random Record
Rating :

 
(C#) ASP.NET SQL Server List Record Paging/Pagination
Rating :

 
(C#) ASP.NET SQL Server Search Record
Rating :

 
(C#) ASP.NET SQL Server Search Record Paging/Pagination
Rating :

 
(C#) ASP.NET SQL Server Add/Insert Record
Rating :

 
(C#) ASP.NET SQL Server Add-Insert Multiple Record
Rating :

 
(C#) ASP.NET SQL Server Check Already Exists Add/Insert Record
Rating :

 
(C#) ASP.NET SQL Server Transaction (BeginTransaction,Commit,Rollback)
Rating :

 
(C#) ASP.NET SQL Server Edit/Update Record
Rating :

 
(C#) ASP.NET SQL Server Delete Record
Rating :

 
(C#) ASP.NET SQL Server Multiple Checkbox Delete Record
Rating :

 
(C#) ASP.NET SQL Server and GridView, DataSource
Rating :

 
(C#) ASP.NET SQL Server Database Class
Rating :

 
(C#) ASP.NET SQL Server Database Class (Visual Studio .Net 2003 - .NET 1.1)
Rating :

 
(C#) ASP.NET SQL Server Database Class (Visual Studio 2005,2008,2010 - .NET 2.0,3.5,4.0)
Rating :

 
(C#) ASP.NET SQL Server BLOB Binary Data and Parameterized Query
Rating :


ThaiCreate.Com Forum


Comunity Forum Free Web Script
Jobs Freelance Free Uploads
Free Web Hosting Free Tools

สอน PHP ผ่าน Youtube ฟรี
สอน Android การเขียนโปรแกรม Android
สอน Windows Phone การเขียนโปรแกรม Windows Phone 7 และ 8
สอน iOS การเขียนโปรแกรม iPhone, iPad
สอน Java การเขียนโปรแกรม ภาษา Java
สอน Java GUI การเขียนโปรแกรม ภาษา Java GUI
สอน JSP การเขียนโปรแกรม ภาษา Java
สอน jQuery การเขียนโปรแกรม ภาษา jQuery
สอน .Net การเขียนโปรแกรม ภาษา .Net
Free Tutorial
สอน Google Maps Api
สอน Windows Service
สอน Entity Framework
สอน Android
สอน Java เขียน Java
Java GUI Swing
สอน JSP (Web App)
iOS (iPhone,iPad)
Windows Phone
Windows Azure
Windows Store
Laravel Framework
Yii PHP Framework
สอน jQuery
สอน jQuery กับ Ajax
สอน PHP OOP (Vdo)
Ajax Tutorials
SQL Tutorials
สอน SQL (Part 2)
JavaScript Tutorial
Javascript Tips
VBScript Tutorial
VBScript Validation
Microsoft Access
MySQL Tutorials
-- Stored Procedure
MariaDB Database
SQL Server Tutorial
SQL Server 2005
SQL Server 2008
SQL Server 2012
-- Stored Procedure
Oracle Database
-- Stored Procedure
SVN (Subversion)
แนวทางการทำ SEO
ปรับแต่งเว็บให้โหลดเร็ว


Hit Link
   







Load balance : Server 05
ThaiCreate.Com Logo
© www.ThaiCreate.Com. 2003-2024 All Rights Reserved.
ไทยครีเอทบริการ จัดทำดูแลแก้ไข Web Application ทุกรูปแบบ (PHP, .Net Application, VB.Net, C#)
[Conditions Privacy Statement] ติดต่อโฆษณา 081-987-6107 อัตราราคา คลิกที่นี่