Click here to Skip to main content
15,897,165 members
Articles / Programming Languages / C#

How to link to an Access Database in a C# Application

Rate me:
Please Sign up or sign in to vote.
3.24/5 (51 votes)
21 Sep 20043 min read 432.3K   18.3K   84  
An article explaining how to link your home database in your C# application.
//------------------------------------------------------------------------------
// <autogenerated>
//     This code was generated by a tool.
//     Runtime Version: 1.0.3705.0
//
//     Changes to this file may cause incorrect behavior and will be lost if 
//     the code is regenerated.
// </autogenerated>
//------------------------------------------------------------------------------

namespace WindowsApplication1 {
    using System;
    using System.Data;
    using System.Xml;
    using System.Runtime.Serialization;
    
    
    [Serializable()]
    [System.ComponentModel.DesignerCategoryAttribute("code")]
    [System.Diagnostics.DebuggerStepThrough()]
    [System.ComponentModel.ToolboxItem(true)]
    public class DataSet1 : DataSet {
        
        private BookDbDataTable tableBookDb;
        
        public DataSet1() {
            this.InitClass();
            System.ComponentModel.CollectionChangeEventHandler schemaChangedHandler = new System.ComponentModel.CollectionChangeEventHandler(this.SchemaChanged);
            this.Tables.CollectionChanged += schemaChangedHandler;
            this.Relations.CollectionChanged += schemaChangedHandler;
        }
        
        protected DataSet1(SerializationInfo info, StreamingContext context) {
            string strSchema = ((string)(info.GetValue("XmlSchema", typeof(string))));
            if ((strSchema != null)) {
                DataSet ds = new DataSet();
                ds.ReadXmlSchema(new XmlTextReader(new System.IO.StringReader(strSchema)));
                if ((ds.Tables["BookDb"] != null)) {
                    this.Tables.Add(new BookDbDataTable(ds.Tables["BookDb"]));
                }
                this.DataSetName = ds.DataSetName;
                this.Prefix = ds.Prefix;
                this.Namespace = ds.Namespace;
                this.Locale = ds.Locale;
                this.CaseSensitive = ds.CaseSensitive;
                this.EnforceConstraints = ds.EnforceConstraints;
                this.Merge(ds, false, System.Data.MissingSchemaAction.Add);
                this.InitVars();
            }
            else {
                this.InitClass();
            }
            this.GetSerializationData(info, context);
            System.ComponentModel.CollectionChangeEventHandler schemaChangedHandler = new System.ComponentModel.CollectionChangeEventHandler(this.SchemaChanged);
            this.Tables.CollectionChanged += schemaChangedHandler;
            this.Relations.CollectionChanged += schemaChangedHandler;
        }
        
        [System.ComponentModel.Browsable(false)]
        [System.ComponentModel.DesignerSerializationVisibilityAttribute(System.ComponentModel.DesignerSerializationVisibility.Content)]
        public BookDbDataTable BookDb {
            get {
                return this.tableBookDb;
            }
        }
        
        public override DataSet Clone() {
            DataSet1 cln = ((DataSet1)(base.Clone()));
            cln.InitVars();
            return cln;
        }
        
        protected override bool ShouldSerializeTables() {
            return false;
        }
        
        protected override bool ShouldSerializeRelations() {
            return false;
        }
        
        protected override void ReadXmlSerializable(XmlReader reader) {
            this.Reset();
            DataSet ds = new DataSet();
            ds.ReadXml(reader);
            if ((ds.Tables["BookDb"] != null)) {
                this.Tables.Add(new BookDbDataTable(ds.Tables["BookDb"]));
            }
            this.DataSetName = ds.DataSetName;
            this.Prefix = ds.Prefix;
            this.Namespace = ds.Namespace;
            this.Locale = ds.Locale;
            this.CaseSensitive = ds.CaseSensitive;
            this.EnforceConstraints = ds.EnforceConstraints;
            this.Merge(ds, false, System.Data.MissingSchemaAction.Add);
            this.InitVars();
        }
        
        protected override System.Xml.Schema.XmlSchema GetSchemaSerializable() {
            System.IO.MemoryStream stream = new System.IO.MemoryStream();
            this.WriteXmlSchema(new XmlTextWriter(stream, null));
            stream.Position = 0;
            return System.Xml.Schema.XmlSchema.Read(new XmlTextReader(stream), null);
        }
        
        internal void InitVars() {
            this.tableBookDb = ((BookDbDataTable)(this.Tables["BookDb"]));
            if ((this.tableBookDb != null)) {
                this.tableBookDb.InitVars();
            }
        }
        
        private void InitClass() {
            this.DataSetName = "DataSet1";
            this.Prefix = "";
            this.Namespace = "http://www.tempuri.org/DataSet1.xsd";
            this.Locale = new System.Globalization.CultureInfo("en-US");
            this.CaseSensitive = false;
            this.EnforceConstraints = true;
            this.tableBookDb = new BookDbDataTable();
            this.Tables.Add(this.tableBookDb);
        }
        
        private bool ShouldSerializeBookDb() {
            return false;
        }
        
        private void SchemaChanged(object sender, System.ComponentModel.CollectionChangeEventArgs e) {
            if ((e.Action == System.ComponentModel.CollectionChangeAction.Remove)) {
                this.InitVars();
            }
        }
        
        public delegate void BookDbRowChangeEventHandler(object sender, BookDbRowChangeEvent e);
        
        [System.Diagnostics.DebuggerStepThrough()]
        public class BookDbDataTable : DataTable, System.Collections.IEnumerable {
            
            private DataColumn columnauthor;
            
            private DataColumn columnEditionNumber;
            
            private DataColumn columnISBN;
            
            private DataColumn columnTitle;
            
            internal BookDbDataTable() : 
                    base("BookDb") {
                this.InitClass();
            }
            
            internal BookDbDataTable(DataTable table) : 
                    base(table.TableName) {
                if ((table.CaseSensitive != table.DataSet.CaseSensitive)) {
                    this.CaseSensitive = table.CaseSensitive;
                }
                if ((table.Locale.ToString() != table.DataSet.Locale.ToString())) {
                    this.Locale = table.Locale;
                }
                if ((table.Namespace != table.DataSet.Namespace)) {
                    this.Namespace = table.Namespace;
                }
                this.Prefix = table.Prefix;
                this.MinimumCapacity = table.MinimumCapacity;
                this.DisplayExpression = table.DisplayExpression;
            }
            
            [System.ComponentModel.Browsable(false)]
            public int Count {
                get {
                    return this.Rows.Count;
                }
            }
            
            internal DataColumn authorColumn {
                get {
                    return this.columnauthor;
                }
            }
            
            internal DataColumn EditionNumberColumn {
                get {
                    return this.columnEditionNumber;
                }
            }
            
            internal DataColumn ISBNColumn {
                get {
                    return this.columnISBN;
                }
            }
            
            internal DataColumn TitleColumn {
                get {
                    return this.columnTitle;
                }
            }
            
            public BookDbRow this[int index] {
                get {
                    return ((BookDbRow)(this.Rows[index]));
                }
            }
            
            public event BookDbRowChangeEventHandler BookDbRowChanged;
            
            public event BookDbRowChangeEventHandler BookDbRowChanging;
            
            public event BookDbRowChangeEventHandler BookDbRowDeleted;
            
            public event BookDbRowChangeEventHandler BookDbRowDeleting;
            
            public void AddBookDbRow(BookDbRow row) {
                this.Rows.Add(row);
            }
            
            public BookDbRow AddBookDbRow(string author, string EditionNumber, int ISBN, string Title) {
                BookDbRow rowBookDbRow = ((BookDbRow)(this.NewRow()));
                rowBookDbRow.ItemArray = new object[] {
                        author,
                        EditionNumber,
                        ISBN,
                        Title};
                this.Rows.Add(rowBookDbRow);
                return rowBookDbRow;
            }
            
            public BookDbRow FindByISBN(int ISBN) {
                return ((BookDbRow)(this.Rows.Find(new object[] {
                            ISBN})));
            }
            
            public System.Collections.IEnumerator GetEnumerator() {
                return this.Rows.GetEnumerator();
            }
            
            public override DataTable Clone() {
                BookDbDataTable cln = ((BookDbDataTable)(base.Clone()));
                cln.InitVars();
                return cln;
            }
            
            protected override DataTable CreateInstance() {
                return new BookDbDataTable();
            }
            
            internal void InitVars() {
                this.columnauthor = this.Columns["author"];
                this.columnEditionNumber = this.Columns["EditionNumber"];
                this.columnISBN = this.Columns["ISBN"];
                this.columnTitle = this.Columns["Title"];
            }
            
            private void InitClass() {
                this.columnauthor = new DataColumn("author", typeof(string), null, System.Data.MappingType.Element);
                this.Columns.Add(this.columnauthor);
                this.columnEditionNumber = new DataColumn("EditionNumber", typeof(string), null, System.Data.MappingType.Element);
                this.Columns.Add(this.columnEditionNumber);
                this.columnISBN = new DataColumn("ISBN", typeof(int), null, System.Data.MappingType.Element);
                this.Columns.Add(this.columnISBN);
                this.columnTitle = new DataColumn("Title", typeof(string), null, System.Data.MappingType.Element);
                this.Columns.Add(this.columnTitle);
                this.Constraints.Add(new UniqueConstraint("Constraint1", new DataColumn[] {
                                this.columnISBN}, true));
                this.columnISBN.AllowDBNull = false;
                this.columnISBN.Unique = true;
            }
            
            public BookDbRow NewBookDbRow() {
                return ((BookDbRow)(this.NewRow()));
            }
            
            protected override DataRow NewRowFromBuilder(DataRowBuilder builder) {
                return new BookDbRow(builder);
            }
            
            protected override System.Type GetRowType() {
                return typeof(BookDbRow);
            }
            
            protected override void OnRowChanged(DataRowChangeEventArgs e) {
                base.OnRowChanged(e);
                if ((this.BookDbRowChanged != null)) {
                    this.BookDbRowChanged(this, new BookDbRowChangeEvent(((BookDbRow)(e.Row)), e.Action));
                }
            }
            
            protected override void OnRowChanging(DataRowChangeEventArgs e) {
                base.OnRowChanging(e);
                if ((this.BookDbRowChanging != null)) {
                    this.BookDbRowChanging(this, new BookDbRowChangeEvent(((BookDbRow)(e.Row)), e.Action));
                }
            }
            
            protected override void OnRowDeleted(DataRowChangeEventArgs e) {
                base.OnRowDeleted(e);
                if ((this.BookDbRowDeleted != null)) {
                    this.BookDbRowDeleted(this, new BookDbRowChangeEvent(((BookDbRow)(e.Row)), e.Action));
                }
            }
            
            protected override void OnRowDeleting(DataRowChangeEventArgs e) {
                base.OnRowDeleting(e);
                if ((this.BookDbRowDeleting != null)) {
                    this.BookDbRowDeleting(this, new BookDbRowChangeEvent(((BookDbRow)(e.Row)), e.Action));
                }
            }
            
            public void RemoveBookDbRow(BookDbRow row) {
                this.Rows.Remove(row);
            }
        }
        
        [System.Diagnostics.DebuggerStepThrough()]
        public class BookDbRow : DataRow {
            
            private BookDbDataTable tableBookDb;
            
            internal BookDbRow(DataRowBuilder rb) : 
                    base(rb) {
                this.tableBookDb = ((BookDbDataTable)(this.Table));
            }
            
            public string author {
                get {
                    try {
                        return ((string)(this[this.tableBookDb.authorColumn]));
                    }
                    catch (InvalidCastException e) {
                        throw new StrongTypingException("Cannot get value because it is DBNull.", e);
                    }
                }
                set {
                    this[this.tableBookDb.authorColumn] = value;
                }
            }
            
            public string EditionNumber {
                get {
                    try {
                        return ((string)(this[this.tableBookDb.EditionNumberColumn]));
                    }
                    catch (InvalidCastException e) {
                        throw new StrongTypingException("Cannot get value because it is DBNull.", e);
                    }
                }
                set {
                    this[this.tableBookDb.EditionNumberColumn] = value;
                }
            }
            
            public int ISBN {
                get {
                    return ((int)(this[this.tableBookDb.ISBNColumn]));
                }
                set {
                    this[this.tableBookDb.ISBNColumn] = value;
                }
            }
            
            public string Title {
                get {
                    try {
                        return ((string)(this[this.tableBookDb.TitleColumn]));
                    }
                    catch (InvalidCastException e) {
                        throw new StrongTypingException("Cannot get value because it is DBNull.", e);
                    }
                }
                set {
                    this[this.tableBookDb.TitleColumn] = value;
                }
            }
            
            public bool IsauthorNull() {
                return this.IsNull(this.tableBookDb.authorColumn);
            }
            
            public void SetauthorNull() {
                this[this.tableBookDb.authorColumn] = System.Convert.DBNull;
            }
            
            public bool IsEditionNumberNull() {
                return this.IsNull(this.tableBookDb.EditionNumberColumn);
            }
            
            public void SetEditionNumberNull() {
                this[this.tableBookDb.EditionNumberColumn] = System.Convert.DBNull;
            }
            
            public bool IsTitleNull() {
                return this.IsNull(this.tableBookDb.TitleColumn);
            }
            
            public void SetTitleNull() {
                this[this.tableBookDb.TitleColumn] = System.Convert.DBNull;
            }
        }
        
        [System.Diagnostics.DebuggerStepThrough()]
        public class BookDbRowChangeEvent : EventArgs {
            
            private BookDbRow eventRow;
            
            private DataRowAction eventAction;
            
            public BookDbRowChangeEvent(BookDbRow row, DataRowAction action) {
                this.eventRow = row;
                this.eventAction = action;
            }
            
            public BookDbRow Row {
                get {
                    return this.eventRow;
                }
            }
            
            public DataRowAction Action {
                get {
                    return this.eventAction;
                }
            }
        }
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Web Developer
United Kingdom United Kingdom
Ahmed J. Kattan Bachelor degree from Jordan University of Science and Technology computer science department, Master Degree from University of Essex and PhD student at University of Essex ”United Kingdom”, I have written several applications, designed multiple algorithms and several publications. My favorite languages are C++ and C#.



see www.ahmedkattan.com to view Ahmed Kattan's online CV.

Comments and Discussions