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

dtcbnfnu  于 2022-12-24  发布在  .NET
关注(0)|答案(2)|浏览(158)

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

whlutmcx

whlutmcx2#

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

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)

相关问题