Advertisement
ASP_Volume3 String Manipulation #52637

Database Connection Class

This code serves as a starting point for database connections in C#. Adding greater functionality should not be too difficult. This example contains functionality for data navigation, database connection, recordset connection, Bookmarking, AbsolutePosition and RecordCount counters, and of course: Returning data from the table.

AI

สรุปโดย AI: This codebase represents a historical implementation of the logic described in the metadata. Our preservation engine analyzes the structure to provide context for modern developers.

ซอร์สโค้ด
original-source
using System;
using System.Windows.Forms; //Used for message boxes during testing
namespace Nova
{
	public class DBConnect
	{
		#region Declares
		private static ADODB.Connection cn = new ADODB.ConnectionClass();
		private ADODB.Recordset rs = new ADODB.RecordsetClass();
		private long RecordCount = 0;
		private long AbsolutePosition = 0;
		private long BMark = 0;
		#endregion
		#region Property get & let...
		public long RSCount
		{
			get{return RecordCount;}
		}
		public long AbsPosition
		{
			get{return AbsolutePosition;}
		}
		public bool EOF
		{
			get{return rs.EOF;}
		}
		public bool BOF
		{
			get{return rs.BOF;}
		}
		public long Bookmark
		{
			get{return BMark;}
			set{BMark = value;}
		}
		#endregion
		#region Methods/Functions/Procedures
		public DBConnect()
		{}//End DBConnect()
		public static void Connect(string DBName)
		{
			//Connect to the specified database. This database will
			//need to be stored in the same directory as the .exe file.
			//During design time this directory will most likely be in
			//the bin folder. So don't be confused if it doesn't find it.
			//You can also specify a longer string of subfolders for
			//DBName
			try
			{
				string DBProvider = "Provider=Microsoft.Jet.OLEDB.4.0;";
				string DBSource = "Data Source=" + Application.StartupPath.ToString() + DBName + ";";
				string ConStr = DBProvider + DBSource;
				cn.Open(ConStr, "", "", 0);
			}
			catch(Exception e)
			{
				MessageBox.Show(e.ToString());
			}
		}//End Connect()
		
		public void RSConnect(string TableName)
		{
			try
			{
				//Open a connection to the table
				rs.Open(TableName, cn, ADODB.CursorTypeEnum.adOpenDynamic,ADODB.LockTypeEnum.adLockOptimistic, 0);
				//Set AbsPosition and RecordCount
				if(rs.EOF == true && rs.BOF == true)
				{
					RecordCount = 0;
					AbsolutePosition = 0;
				}
				else
				{
					rs.MoveFirst();
					//Loop through records to count them
					do
					{
						RecordCount++;
						rs.MoveNext();
					} while(rs.EOF == false);
					rs.MoveFirst();
				}
			}
			catch
			{
				MessageBox.Show("Cannot connect to table specified.");
			}
		}//End RSConnect()
		public string Value(int FieldNum)
		{
			//Return a value from the database
			return(rs.Fields[FieldNum].Value.ToString());
		}//End Value()
		public void Navigate(string Direction)
		{
			//This switch statement handles the data navigation
			switch(Direction.ToUpper())
			{
				case "FIRST":
					rs.MoveFirst();
					AbsolutePosition = 1;
					break;
				case "PREVIOUS":
				case "PREV":
					if(rs.BOF == false)
					{
						rs.MovePrevious();
						AbsolutePosition--;
					}
					break;
				case "NEXT":
					if(rs.EOF == false)
					{
						rs.MoveNext();
						AbsolutePosition++;
					}
					break;
				case "LAST":
					rs.MoveLast();
					AbsolutePosition = RecordCount;
					break;
				default:
					MessageBox.Show(Direction + " is not a valid argument.");
					break;
			}//End Switch
		}//End MoveNext()
		public void GotoBookmark()
		{
			//Move to the first record
			rs.MoveFirst();
			AbsolutePosition = 1;
			//Loop through the records until the bookmark is reached
			for(long i = 1; i < BMark; i++)
			{
				rs.MoveNext();
				AbsolutePosition++;
			}
		}//End GotoBookmark
		#endregion
	}
}
Upload
ความคิดเห็นดั้งเดิม (3)
กู้คืนจาก Wayback Machine