gpt4 book ai didi

c# - 如何实现 IDataReader?

转载 作者:行者123 更新时间:2023-11-30 19:49:13 26 4
gpt4 key购买 nike

我有一个看起来像这样的 XML:

<resultset>
<datarow>
<datacol>Row 1 - Col 1</datacol>
<datacol>Row 1 - Col 2</datacol>
<datacol>Row 1 - Col 3</datacol>
...
</datarow>
...
</resultset>
...

我的问题是,如何使用此 XML 实现 IDataReader 接口(interface)?我迷路了……

我开发了这个:

public sealed class SybaseDataReader : IDataReader
{
private DataSet _dataSet = new DataSet();

#region IDataReader methods implementation
// ...
}

我的路还好吗?

感谢建设性和解释清楚的帖子。

最佳答案

我写了一个简单的文件阅读器实现。您可以轻松地调整它以读取 xml 文件:

public class MyFileDataReader : IDataReader
{
protected StreamReader Stream { get; set; }
protected object[] Values;
protected bool Eof { get; set; }
protected string CurrentRecord { get; set; }
protected int CurrentIndex { get; set; }

public MyFileDataReader(string fileName)
{
Stream = new StreamReader(fileName);
Values = new object[this.FieldCount];
}

请记住,IDataReader 有几个您不需要实现的方法,具体取决于您的场景。但可能有一些方法实现是您无法避免的:

 public void Close()
{
Array.Clear(Values, 0, Values.Length);
Stream.Close();
Stream.Dispose();
}

public int Depth
{
get { return 0; }
}

public DataTable GetSchemaTable()
{
// avoid to implement several methods if your scenario do not demand it
throw new NotImplementedException();
}

public bool IsClosed
{
get { return Eof; }
}

public bool NextResult()
{
return false;
}

public bool Read()
{
CurrentRecord = Stream.ReadLine();
Eof = CurrentRecord == null;

if (!Eof)
{
Fill(Values);
CurrentIndex++;
}

return !Eof;
}

private void Fill(object[] values)
{
//To simplify the implementation, lets assume here that the table have just 3
//columns: the primary key, and 2 string columns. And the file is fixed column formatted
//and have 2 columns: the first with width 12 and the second with width 40. Said that, we can do as follows

values[0] = null;
values[1] = CurrentRecord.Substring(0, 12).Trim();
values[2] = CurrentRecord.Substring(12, 40).Trim();

// by default, the first position of the array hold the value that will be
// inserted at the first column of the table, and so on
// lets assume here that the primary key is auto-generated
// if the file is xml we could parse the nodes instead of Substring operations
}

public int RecordsAffected
{
get { return -1; }
}

要实现 IDataReader,还必须实现 IDisposable 和 IDataRecord 接口(interface)。

IDisposable 很容易,但 IDataRecord 可能会很痛苦。同样,在这种情况下,有一些我们无法避免的方法实现:

 public int FieldCount
{
get { return 3;//assuming the table has 3 columns }
}

public IDataReader GetData(int i)
{
if (i == 0)
return this;

return null;
}

public string GetDataTypeName(int i)
{
return "String";
}

public string GetName(int i)
{
return Values[i].ToString();
}

public string GetString(int i)
{
return Values[i].ToString();
}

public object GetValue(int i)
{
return Values[i];
}

public int GetValues(object[] values)
{
Fill(values);

Array.Copy(values, Values, this.FieldCount);

return this.FieldCount;
}

public object this[int i]
{
get { return Values[i]; }
}

希望对您有所帮助。

关于c# - 如何实现 IDataReader?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4354533/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com