正常读取reader.GetInt32的方法如下

本文介绍了一个在C#中实现的NullableDataReader类,该类继承了IDataReader接口,为数据库读取操作提供了对空值的支持。通过重载各种类型转换方法,确保从数据库获取的数据能够正确地转换为.NET中的相应类型,同时处理可能的DBNull值。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

using System.Collections.Generic;

using System.Text;

 

namespace Galsun.DALProfile

{

    public interface INullableReader

    {

        // Methods

        bool GetBoolean(string name);

        byte GetByte(string name);

        char GetChar(string name);

        DateTime GetDateTime(string name);

        decimal GetDecimal(string name);

        double GetDouble(string name);

        Guid GetGuid(string name);

        short GetInt16(string name);

        int GetInt32(string name);

        long GetInt64(string name);

        float GetSingle(string name);

        string GetString(string name);

        object GetValue(string name);

        bool IsDBNull(string name);

 

    }

}


 

C# code?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

120

121

122

123

124

125

126

127

128

129

130

131

132

133

134

135

136

137

138

139

140

141

142

143

144

145

146

147

148

149

150

151

152

153

154

155

156

157

158

159

160

161

162

163

164

165

166

167

168

169

170

171

172

173

174

175

176

177

178

179

180

181

182

183

184

185

186

187

188

189

190

191

192

193

194

195

196

197

198

199

200

201

202

203

204

205

206

207

208

209

210

211

212

213

214

215

216

217

218

219

220

221

222

223

224

225

226

227

228

229

230

231

232

233

234

235

236

237

238

239

240

241

242

243

244

245

246

247

248

249

250

251

252

253

254

255

256

257

258

259

260

261

262

263

264

265

266

267

268

269

270

271

272

273

274

275

276

277

278

279

280

281

282

283

284

285

286

287

288

289

290

291

292

293

294

295

296

297

298

299

using System;

using System.Collections.Generic;

using System.Text;

using System.Data;

using System.Data.SqlClient;

 

namespace Galsun.DALProfile

{

    public sealed class NullableDataReader : IDataReader, IDisposable, IDataRecord, INullableReader

    {

        private IDataReader reader;

 

        public NullableDataReader(IDataReader dataReader)

        {

            this.reader = dataReader;

        }

 

        public void Close()

        {

            this.reader.Close();

        }

 

 

        public void Dispose()

        {

            if (this.reader != null)

            {

                this.reader.Dispose();

            }

        }

 

        public bool GetBoolean(int i)

        {

            return this.reader.GetBoolean(i);

        }

        public bool GetBoolean(string name)

        {

            bool boolean = false;

            if (!this.IsDBNull(name))

            {

                boolean = this.GetBoolean(this.reader.GetOrdinal(name));

            }

            return boolean;

        }

        public byte GetByte(int i)

        {

            return this.reader.GetByte(i);

        }

        public byte GetByte(string name)

        {

            return this.GetByte(this.reader.GetOrdinal(name));

        }

        public long GetBytes(int i, long fieldOffset, byte[] buffer, int bufferoffset, int length)

        {

            return this.reader.GetBytes(i, fieldOffset, buffer, bufferoffset, length);

        }

        public char GetChar(int i)

        {

            return this.reader.GetChar(i);

        }

        public char GetChar(string name)

        {

            return this.GetChar(this.reader.GetOrdinal(name));

        }

        public long GetChars(int i, long fieldoffset, char[] buffer, int bufferoffset, int length)

        {

            return this.reader.GetChars(i, fieldoffset, buffer, bufferoffset, length);

        }

        public IDataReader GetData(int i)

        {

            return this.reader.GetData(i);

        }

 

        public string GetDataTypeName(int i)

        {

            return this.reader.GetDataTypeName(i);

        }

 

        public string GetDataTypeName(string name)

        {

            return this.reader.GetDataTypeName(this.reader.GetOrdinal(name));

        }

        public DateTime GetDateTime(int i)

        {

            return this.reader.GetDateTime(i);

        }

        public DateTime GetDateTime(string name)

        {

            if (this.IsDBNull(name))

            {

                return DateTime.Now;

            }

            return this.reader.GetDateTime(this.reader.GetOrdinal(name));

        }

        public decimal GetDecimal(int i)

        {

            return this.reader.GetDecimal(i);

        }

        public decimal GetDecimal(string name)

        {

            decimal @decimal = 0M;

            if (!this.IsDBNull(name))

            {

                @decimal this.reader.GetDecimal(this.reader.GetOrdinal(name));

            }

            return @decimal;

        }

        public double GetDouble(int i)

        {

            return this.reader.GetDouble(i);

        }

        public double GetDouble(string name)

        {

            double num = 0.0;

            if (!this.IsDBNull(name))

            {

                num = this.reader.GetDouble(this.reader.GetOrdinal(name));

            }

            return num;

        }

 

        public Type GetFieldType(int i)

        {

            return this.reader.GetFieldType(i);

        }

        public Type GetFieldType(string name)

        {

            return this.reader.GetFieldType(this.reader.GetOrdinal(name));

        }

        public float GetFloat(int i)

        {

            return this.reader.GetFloat(i);

        }

        public Guid GetGuid(int i)

        {

            return this.reader.GetGuid(i);

        }

        public Guid GetGuid(string name)

        {

            return this.reader.GetGuid(this.reader.GetOrdinal(name));

        }

        public short GetInt16(int i)

        {

            return this.reader.GetInt16(i);

        }

        public short GetInt16(string name)

        {

            if (this.IsDBNull(name))

            {

                return 0;

            }

            return this.reader.GetInt16(this.reader.GetOrdinal(name));

        }

        public int GetInt32(int i)

        {

            return this.reader.GetInt32(i);

        }

 

        public int GetInt32(string name)

        {

            if (this.IsDBNull(name))

            {

                return 0;

            }

            return this.reader.GetInt32(this.reader.GetOrdinal(name));

        }

        public long GetInt64(int i)

        {

            return this.reader.GetInt64(i);

        }

        public long GetInt64(string name)

        {

            if (this.IsDBNull(name))

            {

                return 0L;

            }

            return this.reader.GetInt64(this.reader.GetOrdinal(name));

        }

        public string GetName(int i)

        {

            return this.reader.GetName(i);

        }

        public DateTime? GetNullableDateTime(string name)

        {

            if (this.IsDBNull(name))

            {

                return null;

            }

            return new DateTime?(this.reader.GetDateTime(this.reader.GetOrdinal(name)));

        }

        public int GetOrdinal(string name)

        {

            return this.reader.GetOrdinal(name);

        }

        public DataTable GetSchemaTable()

        {

            return this.reader.GetSchemaTable();

        }

        public float GetSingle(string name)

        {

            float @float = 0f;

            if (!this.IsDBNull(name))

            {

                @float this.reader.GetFloat(this.reader.GetOrdinal(name));

            }

            return @float;

        }

        public string GetString(int i)

        {

            return this.reader.GetString(i);

        }

 

        public string GetString(string name)

        {

            string str = string.Empty;

            if (!this.IsDBNull(name))

            {

                str = this.reader.GetString(this.reader.GetOrdinal(name));

            }

            return str;

        }

        public object GetValue(int i)

        {

            return this.reader.GetValue(i);

        }

        public object GetValue(string name)

        {

            return this.reader.GetValue(this.reader.GetOrdinal(name));

        }

        public int GetValues(object[] values)

        {

            return this.reader.GetValues(values);

        }

 

        public bool IsDBNull(int i)

        {

            return this.reader.IsDBNull(i);

        }

 

        public bool IsDBNull(string name)

        {

            return this.reader.IsDBNull(this.reader.GetOrdinal(name));

        }

 

        public bool NextResult()

        {

            return this.reader.NextResult();

        }

 

        public bool Read()

        {

            return this.reader.Read();

        }

        public int Depth

        {

            get

            {

                return this.reader.Depth;

            }

        }

        public int FieldCount

        {

            get

            {

                return this.reader.FieldCount;

            }

        }

        public bool IsClosed

        {

            get

            {

                return this.reader.IsClosed;

            }

        }

        public object this[int i]

        {

            get

            {

                return this.reader[i];

            }

        }

        public object this[string name]

        {

            get

            {

                return this.reader[name];

            }

        }

 

        public int RecordsAffected

        {

            get

            {

                return this.reader.RecordsAffected;

            }

        }

  

    }

}

 

 using (NullableDataReader reader = GetinfoListByReader(table,RowNum, orderFid, Conditions))

            {

                if (reader.Read())

                {

                    foreach (PropertyInfo proper in type.GetProperties())

                    {

                        if (!reader.IsDBNull(proper.Name))

                            proper.SetValue(obj, reader.GetValue(proper.Name), null);

                    }

                }

            }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值