[英]C#: How to check user console input against a column in a .csv?
Yo, ,
I am fairly new to C# and I'm making a small text based console RPG game just for practice and I was wondering if there was anyway I could make something similar to this: 我是C#的新手,我正在制作一个小型的基于文本的RPG游戏,仅供练习,我想知道是否仍然可以制作与此类似的东西:
Here's an example .csv if I didn't really explain it well: 如果我没有很好地解释它,这是一个示例.csv:
"charactername", "password123", "health", "level"
"Oswald", "498562a", "100", "4"
"Hammerfist", "98457813", "77", "6"
So just for the sake of clarity, this is what I'm thinking: 因此,为了清楚起见,这就是我的想法:
Is there a way to do this? 有没有办法做到这一点? Am I overlooking a simple solution?
我是否忽略了一个简单的解决方案? Or should I try and go about it a different way?
还是我应该尝试以其他方式去做? I'm just don't want to hard code the values into a Dictionary or List or Array or whatever.
我只是不想将值硬编码为Dictionary或List或Array或其他任何东西。 I'm not looking for security at this stage either, it's just for practice and fun.
我也不是在这个阶段寻找安全性,只是为了练习和娱乐。
Thanks in advance! 提前致谢! Let me know if you need a better understanding of what I'm trying to do and I'll try to explain the best I can.
如果您需要更好地了解我要做什么,请告诉我,我将尽力向您解释。
There are two ways to go about reading csv 阅读csv有两种方法
Just like standard text file, line by line. 就像标准文本文件一样,逐行。 Then parse your line into array of columns and get your values
然后将您的行解析为列数组并获取值
Using Microsoft.ACE.OLEDB.version 使用Microsoft.ACE.OLEDB.version
OleDbconnection connection = new OleDbConnection(); OleDbconnection连接=新的OleDbConnection(); connection.ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\\Documents\\file.csv;Extended Properties=\\"Text;HDR=Yes;FORMAT=Delimited\\"";
connection.ConnectionString =“ Provider = Microsoft.ACE.OLEDB.12.0;数据源= C:\\ Documents \\ file.csv;扩展属性= \\” Text; HDR = Yes; FORMAT = Delimited \\“”;
This way you can load DataTable
on the start of your app and query this table just like Sql database. 这样,您可以在应用程序的开头加载
DataTable
并查询该表,就像Sql数据库一样。
notice : HDR=Yes means that first row is a header row 注意:HDR = Yes表示第一行是标题行
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.