📄 sqlitedatareader.cs
字号:
namespace Imps.Client.Data
{
using System;
using System.Globalization;
using System.Reflection;
public class SQLiteDataReader : IDisposable
{
private SQLiteStatement _activeStatement;
private int _activeStatementIndex;
private SQLiteCommand _command;
private CommandBehavior _commandBehavior;
internal bool _disposeCommand;
private int _fieldCount;
private SQLiteType[] _fieldTypeArray;
private int _readingState;
private int _rowsAffected;
internal SQLiteDataReader(SQLiteCommand cmd, CommandBehavior behave)
{
this._command = cmd;
this._commandBehavior = behave;
this._activeStatementIndex = -1;
this._activeStatement = null;
this._rowsAffected = -1;
this._fieldCount = -1;
if (this._command != null)
{
this.NextResult();
}
}
private void CheckClosed()
{
if (this._command == null)
{
throw new InvalidOperationException("DataReader has been closed");
}
}
private void CheckValidRow()
{
if (this._readingState != 0)
{
throw new InvalidOperationException("No current row");
}
}
public void Close()
{
if (this._command != null)
{
while (this.NextResult())
{
}
this._command.ClearDataReader();
if (((this._commandBehavior & CommandBehavior.CloseConnection) != CommandBehavior.Default) && (this._command.Connection != null))
{
this._command.Connection.Close();
}
if (this._disposeCommand)
{
this._command.Dispose();
}
}
this._command = null;
this._activeStatement = null;
this._fieldTypeArray = null;
}
public void Dispose()
{
this.Dispose(true);
}
protected void Dispose(bool disposing)
{
if (disposing)
{
this.Close();
}
}
public bool GetBoolean(int i)
{
this.VerifyType(i, DbType.Boolean);
return Convert.ToBoolean(this.GetValue(i), CultureInfo.CurrentCulture);
}
public byte GetByte(int i)
{
this.VerifyType(i, DbType.Byte);
return Convert.ToByte(this._activeStatement._sql.GetInt32(this._activeStatement, i));
}
public long GetBytes(int i, long fieldOffset, byte[] buffer, int bufferoffset, int length)
{
this.VerifyType(i, DbType.Binary);
return this._activeStatement._sql.GetBytes(this._activeStatement, i, (int) fieldOffset, buffer, bufferoffset, length);
}
public char GetChar(int i)
{
this.VerifyType(i, DbType.SByte);
return Convert.ToChar(this._activeStatement._sql.GetInt32(this._activeStatement, i));
}
public long GetChars(int i, long fieldoffset, char[] buffer, int bufferoffset, int length)
{
this.VerifyType(i, DbType.String);
return this._activeStatement._sql.GetChars(this._activeStatement, i, (int) fieldoffset, buffer, bufferoffset, length);
}
public string GetDataTypeName(int i)
{
this.CheckClosed();
SQLiteType t = this.GetSQLiteType(i);
if (t.Type == DbType.Object)
{
return SQLiteConvert.SQLiteTypeToType(t).Name;
}
return this._activeStatement._sql.ColumnType(this._activeStatement, i, out t.Affinity);
}
public DateTime GetDateTime(int i)
{
this.VerifyType(i, DbType.DateTime);
return this._activeStatement._sql.GetDateTime(this._activeStatement, i);
}
public decimal GetDecimal(int i)
{
this.VerifyType(i, DbType.Decimal);
return Convert.ToDecimal(this._activeStatement._sql.GetDouble(this._activeStatement, i));
}
public double GetDouble(int i)
{
this.VerifyType(i, DbType.Double);
return this._activeStatement._sql.GetDouble(this._activeStatement, i);
}
public Type GetFieldType(int i)
{
this.CheckClosed();
return SQLiteConvert.SQLiteTypeToType(this.GetSQLiteType(i));
}
public float GetFloat(int i)
{
this.VerifyType(i, DbType.Single);
return Convert.ToSingle(this._activeStatement._sql.GetDouble(this._activeStatement, i));
}
public Guid GetGuid(int i)
{
if (this.VerifyType(i, DbType.Guid) == TypeAffinity.Blob)
{
byte[] bDest = new byte[0x10];
this._activeStatement._sql.GetBytes(this._activeStatement, i, 0, bDest, 0, 0x10);
return new Guid(bDest);
}
return new Guid(this._activeStatement._sql.GetText(this._activeStatement, i));
}
public short GetInt16(int i)
{
this.VerifyType(i, DbType.Int16);
return Convert.ToInt16(this._activeStatement._sql.GetInt32(this._activeStatement, i));
}
public int GetInt32(int i)
{
this.VerifyType(i, DbType.Int32);
return this._activeStatement._sql.GetInt32(this._activeStatement, i);
}
public long GetInt64(int i)
{
this.VerifyType(i, DbType.Int64);
return this._activeStatement._sql.GetInt64(this._activeStatement, i);
}
public string GetName(int i)
{
this.CheckClosed();
return this._activeStatement._sql.ColumnName(this._activeStatement, i);
}
public int GetOrdinal(string name)
{
this.CheckClosed();
return this._activeStatement._sql.ColumnIndex(this._activeStatement, name);
}
private SQLiteType GetSQLiteType(int i)
{
if (this._fieldTypeArray == null)
{
this._fieldTypeArray = new SQLiteType[this.VisibleFieldCount];
}
if (this._fieldTypeArray[i].Affinity == TypeAffinity.Uninitialized)
{
this._fieldTypeArray[i].Type = SQLiteConvert.TypeNameToDbType(this._activeStatement._sql.ColumnType(this._activeStatement, i, out this._fieldTypeArray[i].Affinity));
}
return this._fieldTypeArray[i];
}
public string GetString(int i)
{
this.VerifyType(i, DbType.String);
return this._activeStatement._sql.GetText(this._activeStatement, i);
}
public object GetValue(int i)
{
this.CheckClosed();
SQLiteType typ = this.GetSQLiteType(i);
if (typ.Type != DbType.Object)
{
TypeAffinity affinity = this._activeStatement._sql.ColumnAffinity(this._activeStatement, i);
if (affinity != typ.Affinity)
{
typ.Type = DbType.Object;
typ.Affinity = affinity;
}
}
return this._activeStatement._sql.GetValue(this._activeStatement, i, ref typ);
}
public int GetValues(object[] values)
{
int fieldCount = this.FieldCount;
if (values.Length < fieldCount)
{
fieldCount = values.Length;
}
for (int i = 0; i < fieldCount; i++)
{
values[i] = this.GetValue(i);
}
return fieldCount;
}
public bool IsDBNull(int i)
{
this.CheckClosed();
return this._activeStatement._sql.IsNull(this._activeStatement, i);
}
public bool NextResult()
{
this.CheckClosed();
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -