Click here to Skip to main content
15,888,461 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
I want to create a Window application without using any database, but i store the data in some other way.

How can i do that?

Please Suggest me.
Posted
Comments
_Amy 25-Aug-14 23:54pm    
What do you mean by any database? Can you explain it little more?

Convert your data(class,struct..etc) into byte[],then write the byte[] into a file.
Imaging,you have a class like this:
C#
class Customer
{
    public string FirstName { get; set; }
    public string LastName { get; set; }
}

And this is your "data":
C#
List<Customer> customerList = new List<Customer>();
customerList.Add(new Customer
{
    FirstName = "Clark ",
    LastName = "Kent"
});

Now,let's convert your data into byte[] and write the byte[] into a file.
You need a function to do this for you:
C#
void write(object obj, string path)
{
    FileStream fs = new FileStream(path, FileMode.OpenOrCreate, FileAccess.Write, FileShare.None);

    using (GZipStream zipStream = new GZipStream(fs, CompressionMode.Compress))
    {
        fs.SetLength(0);
        MemoryStream ms = new MemoryStream();
        BinaryFormatter bf = new BinaryFormatter();

        bf.Serialize(ms, obj);
        byte[] bytes = ms.ToArray();

        zipStream.Write(bytes, 0, bytes.Length);
    }
}

Call it like this:
write(customerList, path);

You know what is the "path" here.
This is the way of using your data in your file,just call the "read" function:
List<Customer> customerList = read(path);

The "read" function just like this:
C#
List<Customer> read(string path)
{
    using (FileStream fs = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.Read))
    {
        GZipStream zipStream = new GZipStream(fs, CompressionMode.Decompress, true);
        BinaryFormatter bf = new BinaryFormatter();
        return bf.Deserialize(zipStream) as List<Customer>;
    }
}
 
Share this answer
 
v2
Comments
Akaash Kumar 25-Aug-14 5:05am    
Can you give me some more explanation?
vikinghunter 25-Aug-14 22:05pm    
I had updated my solution.Hope this can help you.
Thanks7872 25-Aug-14 5:26am    
Data base doesn't mean SQL Server or MySql or Oracle only. "File" is also a type of Database.
vikinghunter 25-Aug-14 22:19pm    
That's right,file is also a type of database.Everything,stored something with it,can called database.
you can use many things like excel sheet, files t store your data
 
Share this answer
 
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900