繁体   English   中英

如何在不使用数据库的情况下将数据存储在数据集中?

[英]How to store data in dataset without using Database?

如何在没有数据库的情况下使用数据集在前端存储数据? 我必须在不使用后端数据库的情况下将数据存储在 c# 中。

您可以做的一件事是使用此命名空间

 using System;
 using System.Collections.Generic;
 using System.Linq;
 using System.Net.NetworkInformation;
 using System.Reflection;
 using System.Text;
 using System.Threading.Tasks;
 using static DataStoreServiceNameSpace.DataStoreServiceMain;
 namespace DataStoreService
{

public struct DataStore
{
    public string Name = "ERROR";
    public string filepath = "ERROR";
    public string path = "ERROR";

    public DataStore(string ACCESSCODE = "IMINTROUBLE")
    {
        if (ACCESSCODE == "CODE-PASS-ID:6t2uy3i2u3i2132131io31oi31u31ou3i1o3")
        {

        }
        else
        {
            throw new Exception("DataStores Must Be Declared Using DataStoreService.GetDataStore");
        }
    }
    public string GetAsync(string key)
    {


        string[] lines = File.ReadAllLines(filepath);
        string data = "";
        bool FoundData = false;
        foreach (string line in lines)
        {
            string[] split = line.Split('"');
            if (split == null)
            {
                throw new Exception("Can not find key:" + key + ". Did you forget to use DataStore.SetAsync");
            }
            if (split[0] == key)
            {
                FoundData = true;
                data = split[1];
            }
        }
        if (FoundData == false)
        {
            throw new Exception("Can not find key:" + key + ". Did you forget to use DataStore.SetAsync");
        }
        return data;
    }
    public bool SetAysnc(string key, string data)
    {
        if (key == null)
        {
            throw new ArgumentNullException();
        }
        string[] lines = File.ReadAllLines(filepath);
        bool FoundData = false;
        int position = -1;
        int i = 0;
        foreach (string line in lines)
        {
            string[] split = line.Split('"');

            if (split[0] == key)
            {
                position = i;
                FoundData = true;
            }
            i = i + 1;
        }
        if (FoundData)
        {
            lines[position] = key + '"' + data;
            File.WriteAllLines(filepath, lines);

        }
        else
        {
            string[] e = { key + '"' + data };
            File.AppendAllLines(filepath, e);
        }
        return true;
    }
}
public  class DataStoreServiceMain  
{
    

 
    

    public DataStoreServiceNameSpace.DataStore
        GetDataStore(string name)
    {
        DataStoreServiceNameSpace.DataStore d = new DataStoreServiceNameSpace.DataStore("CODE-PASS-ID:6t2uy3i2u3i2132131io31oi31u31ou3i1o3");
        d.path = GetType().Assembly.Location;
        d.filepath = Directory.GetParent(d.path).Parent.Parent.Parent.FullName + "/DataStores/" + name + ".bendata";
        d.Name = name;
        if (File.Exists(d.filepath))
        {

        }
        else
        {
            FileStream file = File.Create(d.filepath);
            file.Close();
        }
        return d;
    }
}
}

在文件的目录下建立一个文件夹,并命名为(DataStores)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM