简体   繁体   English

我如何更改文本文件中的数据类型以读取字符串而不是整数(C ++ / CLI,OleDb)?

[英]How do I need to change the datatype in a textfile in order to read a String and not a Integer (C++/CLI, OleDb)?

My goal is it to read from a text file. 我的目标是从文本文件中读取。 This text file contains different columns and rows for each value. 此文本文件包含每个值的不同列和行。 I can read the file as long as I don't change the datatype that windows set on its own. 只要我不更改windows自己设置的数据类型,我就可以读取该文件。 But I do not want the "plz" and "nr" column to be numbers (integers) but a text (String) value because a plz could contain values like "01979" and the nr could contain something like "4a". 但我不希望“plz”和“nr”列为数字(整数)而是文本(String)值,因为plz可能包含类似“01979”的值,而nr可能包含类似“4a”的值。 As a number the starting zero would be lost and this way something like a postcard would never reach its intended destination. 作为一个数字,起始零将丢失,这样的方式像明信片永远不会到达其预定的目的地。

This way I need to change the datatype in a "schema.ini" file. 这样我需要更改“schema.ini”文件中的数据类型。 But it doesn't work. 但它不起作用。 I think I make some mistakes and do not follow the tutorial the way I need to do: "Schema.ini File" 我想我犯了一些错误,不按照我需要的方式按照教程: “Schema.ini文件”

Everytime I tried to read a String I got an Exception because it still want to read an Int32-values that I would need to convert into a string. 每当我尝试读取一个String时,我都会得到一个Exception,因为它仍然想要读取我需要转换为字符串的Int32值。

I did name the file "kunde.txt" 我把文件命名为“kunde.txt”

knr|nachname|vorname|plz|ort|strasse|nr
1|Müller|Johan|12345|Muster|Musterstr|1
2|Kummer|Freude|23456|Feeling|Gefühlswelt|4a

Col 0 = knr, 1 = nachname, 2 = vorname, 3 = plz, 4 = ort, 5 = strasse, 6 = nr Col 0 = knr,1 = nachname,2 = vorname,3 = plz,4 = ort,5 = strasse,6 = nr

con->ConnectionString =
    "Provider=Microsoft.JET.OLEDB.4.0;" +
    "Data Source=D:/C++/Quellen;" +
    "Extended Properties=text";
// ....
meineKunden->CommandText =
    "SELECT knr, nachname, vorname, plz, ort, strasse, nr " +
    "FROM kunde.txt ";
// ....
String ^ str;
while(reader->Read()){
    str += Convert::ToString(reader->GetInt32(0));
    str += " ";
    str += reader->GetString(3);
    str += " ";
    str += reader->GetString(6);
    str += "\r\n";
}
this->txb_Insert->Text = str;

My schema.ini 我的schema.ini

[kunde.txt]
ColNameHeader=True  
Format=Delimited(|) 
3=plz Char Width 5
6=nr Char Width 10

I did try with "Col3" instead of "3". 我确实试过“Col3”而不是“3”。 I did use "Text" rather tan "Char", and I did even attempted it without Width. 我确实使用了“Text”而不是“Char”,我甚至尝试过没有Width。 But everytime I got the same failure message. 但每次我得到同样的失败信息。 Even if i use 4 or 7 ... since I am not sure how it will be counted in the ini / txt file. 即使我使用4或7 ......因为我不确定它将如何计算在ini / txt文件中。

Exception: 例外:

System.InvalidCastException: Die angegebene Umwandlung ist ungültig.
bei System.Data.OleDb.ColumnBinding.ValueString()
bei System.Data.OleDb.OleDbDataReader.GetString(Int32 ordinal)

The exception is calls already by str += reader->GetString(3) If I am correct column 3 contains plz, this way. 例外是str + = reader-> GetString(3)调用如果我是正确的,第3列包含plz,这样。

Could someone please say what I do understand wrong with the schema.ini file? 有人可以说一下我对schema.ini文件的错误吗? Since I could read the file without mistakes as long as I doesn't try to specific change the datatype in some columns the problems need to be with the ini file. 因为我可以在没有错误的情况下读取文件,只要我不尝试在某些列中特定地更改数据类型,就需要使用ini文件。 At least I think so. 至少我是这么认为的。

EDIT: I did change my ini-file to: 编辑:我确实将我的ini文件更改为:

[kunde.txt]
ColNameHeader=True  
Format=Delimited(|) 
Col1="knr" Integer
Col2="nachname" Text
Col3="vorname" Text
Col4="plz" Text
Col5="ort" Text
Col6="strasse" Text
Col7="nr" Text

Now it works for "plz" but the exception is called in the last row, when I call the "nr". 现在它适用于“plz”,但是当我调用“nr”时,在最后一行调用异常。 WTF? WTF?

You have to maintain that the way you read data from the file suits the way it's written in . 您必须保持从文件中读取数据的方式符合其编写方式。

So if the file in not written by you and you have to use it,try to know if it has a specific structure (for ex: fixed length record delimited fields , fixed length record fixed length fields.. etc) and use a way that suits this structure to read it. 因此,如果文件不是由您编写而您必须使用它,请尝试知道它是否具有特定结构(例如:固定长度记录分隔字段,固定长度记录固定长度字段等等)并使用一种方式适合这种结构来阅读它。

Also try to know how fields of records are written in details because the way you read is the same as you write . 还要尝试了解记录字段的详细信息,因为您阅读的方式与您编写的相同。

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

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