Example of ASP.net C # using a text file as a database

Source: Internet
Author: User
Tags static class

1. DataSet base class

The code is as follows Copy Code

<summary>
Data set base class
</summary>
<typeparam name= "T" ></typeparam>
Public abstract class Basetb<t>
where T:class
{
private static string _textfile = Path.Combine (configurationmanager.appsettings["Textdbdir"], typeof (T). Name + "TB.txt");
private static list<t> _data = null;

<summary>
Data containers
</summary>
Internal Static list<t> Container
{
Get
{
if (_data = null)
{
if (file.exists (_textfile))
{
_data = jsonhelper.deserialize<list<t>> (File.readalltext (_textfile));
}
Else
{
_data = new list<t> ();
}
}
return _data;
}
}

<summary>
Get Records
</summary>
<param name= "obj" ></param>
<returns></returns>
Public abstract t get (t obj);

<summary>
Property Copy
</summary>
<param name= "Obj1" ></param>
<param name= "Obj2" ></param>
public abstract void Copy (t obj1, T obj2);

<summary>
Add a record
</summary>
<param name= "obj" ></param>
<returns></returns>
public bool Add (T obj)
{
BOOL result = FALSE;

T item = Get (obj);
if (item = NULL)
{
Container.add (obj);
Save ();
result = true;
}

return result;
}

<summary>
Update records
</summary>
<param name= "obj" ></param>
<returns></returns>
public bool Update (T obj)
{
BOOL result = FALSE;

T item = Get (obj);
if (item!= NULL)
{
Copy (item, obj);
Save ();
result = true;
}

return result;
}

<summary>
Delete a record
</summary>
<param name= "obj" ></param>
<returns></returns>
public bool Remove (T obj)
{
BOOL result = FALSE;

T item = Get (obj);
if (item!= NULL)
{
Container.remove (item);
Save ();
result = true;
}

return result;
}

<summary>
Save data
</summary>
internal void Save ()
{
File.writealltext (_textfile, Jsonhelper.serialize (Container));
}
}

2. Entity and DataSet class

The code is as follows Copy Code

<summary>
Entity classes
</summary>
public class User
{
public string Usn {get; set;}
public string Pwd {get; set;}
}

<summary>
Data sets
</summary>
public class Usertb:basetb<user>
{
#region Implement the Parent class method

<summary>
Get Object
</summary>
<param name= "User" ></param>
<returns></returns>
public override user get (user user)
{
return get (user. USN);
}

<summary>
Property Copy
</summary>
<param name= "user1" ></param>
<param name= "User2" ></param>
public override void Copy (user user1, user user2)
{
User1. USN = User2. USN;
User1. PWD = User2. PWD;
}

#endregion

<summary>
Get Object
</summary>
<param name= "USN" ></param>
<returns></returns>
Public User Get (string USN)
{
User result = null;

foreach (User item in Container)
{
if (item. Usn.tolower () = = USN. ToLower ())
{
result = Item;
Break
}
}

return result;
}

<summary>
Delete a record
</summary>
<param name= "USN" ></param>
public void Remove (string usn)
{
User user = new user () {USN = USN};
Remove (user);
}
}

3. Database class

The code is as follows Copy Code

///<summary>
///database
///</summary>
public static class database
{
    <summary>
   ///initialization
   ///</summary>
    Static Database ()
    {
        if (! Directory.Exists (configurationmanager.appsettings["Textdbdir"]))
        {
            directory.createdirectory ( configurationmanager.appsettings["Textdbdir"]);
       }
   }

private static USERTB _USERTB;
<summary>
///
</summary>
public static USERTB USERTB
{
Get
{
if (_USERTB = null)
{
_USERTB = new USERTB ();
}
return _USERTB;
}
}
}

Call Example

The code is as follows Copy Code
User user = new user () {Usn = "Dnawo", Pwd = "666666"};
DATABASE.USERTB.ADD (user);
User. PWD = "999999";
Database.UserTB.Update (user);


So in the TXT file is stored by row tab-separated data storage format, so it is also convenient for it to find data oh.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.