using System;namespace BaResearch.Data.msSql{interface ISqlDataObject { System.Data.SqlClient.SqlConnection Connection {get; }string ConnectionString {get;set; } }}
___________________________________
using System;using System.Collections.Generic;using System.Text;using System.Data;using System.Data.SqlClient;namespace BaResearch.Data.msSql{public class SqlDataObject : ISqlDataObject {private string _ConnectionString;public SqlConnection Connection {get {return new SqlConnection(_ConnectionString); } }public string ConnectionString {get {return _ConnectionString; }set { _ConnectionString =value; } } }}
__________________________________
using System;namespace BaResearch.Data{interface IBid {string Client {get;set; }string Contact {get;set; }string Sponsor {get;set; }string Priority {get;set; }string BidStatus {get;set; } }}
_____________________________________________
using System;using System.Collections.Generic;using System.Text;using System.Data;using System.Data.SqlClient;using System.Configuration;namespace BaResearch.Data.msSql{public class Bid : SqlDataObject, IBid {#region private member & variablesprivate int _BidID;private string _DateCreated;private string _CreatedBy;private string _Client;private string _Contact;private string _Sponsor;private string _Priority;private string _BidStatus;private SqlDataAdapter sqlda =new SqlDataAdapter();private SqlCommand _command;private SqlParameter[] _parameters = {new SqlParameter("@.client",SqlDbType.NVarChar),new SqlParameter("@.contact",SqlDbType.NVarChar),new SqlParameter("@.sponsor",SqlDbType.NVarChar),new SqlParameter("@.priority",SqlDbType.NVarChar),new SqlParameter("@.bidstatus",SqlDbType.NVarChar), };#endregion #region public member variables - stored procedures//private string sp_Create = "bids_sp_insert"; //private string sp_Update_ByID = "bids_sp_updatebyid"; //private string sp_Delete_ByID = "bids_sp_deletebyid"; //private string sp_Select_All = "bids_sp_selectall"; //private string sp_Select_ByID = "bids_sp_selectbyid"; //private string sp_Select_ByValue = "bids_sp_selectbyvalue";#endregion #region constructors and destructorspublic Bid() {if (this.ConnectionString ==null) {this.ConnectionString = ConfigurationManager.ConnectionStrings["SQL.ConnectionString"].ConnectionString; }else {new Bid(this.ConnectionString); } }public Bid(string ConnectionString) {this.ConnectionString = ConnectionString; }#endregion #region methods & events/// <summary> /// Attach parameters to an SqlCommand /// </summary> /// <param name="sqlcmd">SqlCommand</param>private void attachparameters(SqlCommand sqlcmd) { sqlcmd.Parameters.Clear();foreach (SqlParameter paramin _parameters) { sqlcmd.Parameters.Add(param); } }/// <summary> /// Assign value to the parameters /// </summary> /// <param name="sqlcmd">SqlCommand</param> /// <param name="value">Client object</param>private void assignparametervalues(SqlCommand sqlcmd, Bidvalue) {this.attachparameters(sqlcmd);// todo: assign parameter values here sqlcmd.Parameters[0].Value =value.Client; sqlcmd.Parameters[1].Value =value.Contact; sqlcmd.Parameters[2].Value =value.Sponsor; sqlcmd.Parameters[3].Value =value.Priority; sqlcmd.Parameters[4].Value =value.BidStatus; }public virtual void Create(Bidvalue) { _command =new SqlCommand(); _command.Connection =this.Connection; _command.CommandText = @."INSERT INTO bids (client, contact, sponsor, priority, bidstatus, createdby) VALUES (@.client, @.contact, @.sponsor, @.priority, @.bidstatus, @.createdby);"; _command.CommandType = CommandType.Text;try { sqlda.InsertCommand = _command;this.assignparametervalues(sqlda.InsertCommand,value); sqlda.InsertCommand.Parameters.AddWithValue("@.createdby",value.CreatedBy); sqlda.InsertCommand.Parameters.Add("@.bidid", SqlDbType.Int); sqlda.InsertCommand.Parameters["@.bidid"].Direction = ParameterDirection.ReturnValue; sqlda.InsertCommand.Connection.Open(); sqlda.InsertCommand.ExecuteNonQuery(); sqlda.InsertCommand.Connection.Close(); sqlda.InsertCommand.Connection.Dispose(); }catch (Exception e) {throw new Exception(e.Message); } }public virtual int Update(Bidvalue) {int result; _command =new SqlCommand(); _command.Connection =this.Connection; _command.CommandText = @."UPDATE bids SET client=@.client, contact=@.contact, sponsor=@.sponsor, priority=@.priority, bidstatus=@.bidstatus WHERE bidid=@.bidid"; _command.CommandType = CommandType.Text;try { sqlda.UpdateCommand = _command;using (SqlCommand _cmd = sqlda.UpdateCommand) {this.assignparametervalues(_cmd,value); _cmd.Parameters.AddWithValue("@.bidid",value.BidID); _cmd.Connection.Open(); result = _cmd.ExecuteNonQuery(); _cmd.Connection.Close(); _cmd.Connection.Dispose(); } }catch (Exception e) {throw new Exception(e.Message); }return result; }public virtual void Delete(Bidvalue) { _command =new SqlCommand(); _command.Connection =this.Connection; _command.CommandText = @."DELETE bids WHERE bidid=@.bidid;"; _command.CommandType = CommandType.Text;try { sqlda.DeleteCommand = _command;using (SqlCommand _cmd = sqlda.DeleteCommand) { _cmd.Parameters.AddWithValue("@.bidid",value.BidID); _cmd.Connection.Open(); _cmd.ExecuteNonQuery(); _cmd.Connection.Close(); _cmd.Connection.Dispose(); } }catch (Exception e) {throw new Exception(e.Message); } }public virtual DataSet List(string filter) { DataSet result =new DataSet(); _command =new SqlCommand(); _command.Connection =this.Connection; _command.CommandText = @."SELECT * FROM bids_view_listall WHERE (clientname like @.value) or (contactname like @.value) or (sponsor like @.value) ORDER BY datecreated DESC"; _command.CommandType = CommandType.Text;try {if (filter !=null) { filter = filter.Replace(" ","%"); } sqlda.SelectCommand = _command; sqlda.SelectCommand.Parameters.AddWithValue("@.value","%" + filter +"%"); sqlda.Fill(result); }catch (Exception e) {throw new Exception(e.Message); }return (DataSet)result; }#endregion #region IBid Memberspublic string Client {get {return _Client; }set { _Client =value; } }public string Contact {get {return _Contact; }set { _Contact =value; } }public string Sponsor {get {return _Sponsor; }set { _Sponsor =value; } }public string Priority {get {return _Priority; }set { _Priority =value; } }public string BidStatus {get {return _BidStatus; }set { _BidStatus =value; } }#endregion #region Propertiespublic int BidID {get {return _BidID; }set { _BidID =value; } }public string DateCreated {get {return _DateCreated; }set { _DateCreated =value; } }public string CreatedBy {get {return _CreatedBy; }set { _CreatedBy =value; } }#endregion }}
_____________________________________________
Am I on a right path? I will greatly appreciate in any comments or suggestions for a design. I am creating an n-tier application and i don't know if my design is right. I dont have the proper schooling for creating this kind of applications and I am still on layer of data access and business logic.
I tested it on a console application and web app, for now its working. But, how about if a change a different type of database? I studied some sample applications downloaded online and a little bit of reading from a book.
Although, I already completed a past project with a module (dll) for a web application and already have implemented it.
I am still confused on how to implement a data library that can be used in different type of applications and services (windows, web) and can change the type of database by changing some configuration and not change the code itself.
I there someone who can show me a pattern for creating a library that can be used to any type of applications. It will be a great help for me and I will greatly appreciate it.
since no one replied, i have to search it for my self
http://www.dofactory.com/Patterns/Patterns.aspx.
I still didn't buy it, but, are these kind of books going to help me on what am i searching for?
|||Sorry did not reply you sooner but here are some good samples, and the database agnostic stuff not that simple but check the Achitecture forum for references to Petshop a Java application converted to .NET by Microsoft. And good books I prefer Martin Fowler books but he is not a beginner's writer the best beginner's book is by Craig Larman because he is complete.
http://www.microsoft.com/belux/msdn/nl/community/columns/hyatt/ntier1.mspx
http://www.microsoft.com/belux/msdn/nl/community/columns/hyatt/ntier2.mspx
http://www.microsoft.com/belux/msdn/nl/community/columns/hyatt/ntier3.mspx
http://www.dotnetbips.com/articles/displayarticle.aspx?id=515
Free Objects development resources, Post again if you still have question. Hope this helps.
http://forums.asp.net/thread/1462028.aspx
|||Thanks for your help.
|||jd2001:
Thanks for your help.
I am glad I could help
No comments:
Post a Comment