簡體   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