博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
赋值文件
阅读量:4604 次
发布时间:2019-06-09

本文共 6361 字,大约阅读时间需要 21 分钟。

using System;

using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;

namespace DAL

{
public class Field
{
// Disallow Construction
private Field() { }

// By Number

static public string GetString(SqlDataReader reader, int fldnum)
{
if (reader.IsDBNull(fldnum))
return string.Empty;
return reader.GetString(fldnum).Trim();
}

static public decimal GetDecimal(SqlDataReader reader, int fldnum)

{
if (reader.IsDBNull(fldnum))
return decimal.MinValue;
return reader.GetDecimal(fldnum);
}

static public int GetInt(SqlDataReader reader, int fldnum)

{
if (reader.IsDBNull(fldnum))
//return int.MinValue;
return 0;
return reader.GetInt32(fldnum);
}

static public bool GetBoolean(SqlDataReader reader, int fldnum)

{
return (GetInt(reader, fldnum) > 0);
}

static public byte GetByte(SqlDataReader rec, int fldnum)

{
if (rec.IsDBNull(fldnum))
return 0;
return rec.GetByte(fldnum);
}

static public DateTime GetDateTime(SqlDataReader reader, int fldnum)

{
if (reader.IsDBNull(fldnum))
return DateTime.MinValue;
return reader.GetDateTime(fldnum);
}

static public double GetDouble(SqlDataReader reader, int fldnum)

{
if (reader.IsDBNull(fldnum))
return double.MinValue;
return reader.GetDouble(fldnum);
}

static public float GetFloat(SqlDataReader reader, int fldnum)

{
if (reader.IsDBNull(fldnum))
return float.MinValue;
return reader.GetFloat(fldnum);
}

static public Guid GetGuid(SqlDataReader reader, int fldnum)

{
if (reader.IsDBNull(fldnum))
return Guid.Empty;
return reader.GetGuid(fldnum);
}

static public Int32 GetInt32(SqlDataReader reader, int fldnum)

{
if (reader.IsDBNull(fldnum))
return int.MinValue;
return reader.GetInt32(fldnum);
}

static public Int16 GetInt16(SqlDataReader reader, int fldnum)

{
if (reader.IsDBNull(fldnum))
return Int16.MinValue;
return reader.GetInt16(fldnum);
}

static public Int64 GetInt64(SqlDataReader reader, int fldnum)

{
if (reader.IsDBNull(fldnum))
return Int64.MinValue;
return reader.GetInt64(fldnum);
}

static public ulong GetUlong(SqlDataReader reader, int fldnum)

{
if (reader.IsDBNull(fldnum))
return ulong.MinValue;
return ulong.Parse(reader[fldnum].ToString());
}

 

 

// By Name

static public string GetString(SqlDataReader reader, string fldname)
{
return GetString(reader, reader.GetOrdinal(fldname));
}

static public decimal GetDecimal(SqlDataReader reader, string fldname)

{
return GetDecimal(reader, reader.GetOrdinal(fldname));
}

static public int GetInt(SqlDataReader reader, string fldname)

{
return GetInt(reader, reader.GetOrdinal(fldname));
}

static public bool GetBoolean(SqlDataReader reader, string fldname)

{
return GetBoolean(reader, reader.GetOrdinal(fldname));
}

static public byte GetByte(SqlDataReader rec, string fldname)

{
return GetByte(rec, rec.GetOrdinal(fldname));
}

static public DateTime GetDateTime(SqlDataReader reader,

string fldname)
{
return GetDateTime(reader, reader.GetOrdinal(fldname));
}

static public double GetDouble(SqlDataReader reader, string fldname)

{
return GetDouble(reader, reader.GetOrdinal(fldname));
}

static public float GetFloat(SqlDataReader reader, string fldname)

{
return GetFloat(reader, reader.GetOrdinal(fldname));
}

static public Guid GetGuid(SqlDataReader reader, string fldname)

{
return GetGuid(reader, reader.GetOrdinal(fldname));
}

static public Int32 GetInt32(SqlDataReader reader, string fldname)

{
return GetInt32(reader, reader.GetOrdinal(fldname));
}

static public Int16 GetInt16(SqlDataReader reader, string fldname)

{
return GetInt16(reader, reader.GetOrdinal(fldname));
}

static public Int64 GetInt64(SqlDataReader reader, string fldname)

{
return GetInt64(reader, reader.GetOrdinal(fldname));
}

static public ulong GetUlong(SqlDataReader reader, string fldname)
{
return GetUlong(reader, reader.GetOrdinal(fldname));
}

static public DateTime GetDateTime(DataRow dr,
string fldname)
{
if (dr.IsNull(fldname))
return DateTime.MinValue;
return Convert.ToDateTime(dr[fldname]);
}

static public int GetInt(DataRow dr, string fldname)

{
if (dr.IsNull(fldname))
//return int.MinValue;
return 0;
return Convert.ToInt32(dr[fldname]);
}

static public string GetString(DataRow dr, string fldname)

{
return dr[fldname].ToString();
}

static public decimal GetDecimal(DataRow dr, string fldname)

{
if (dr.IsNull(fldname))
//return decimal.MinValue;
return 0;
return Convert.ToDecimal(dr[fldname]);
}

static public Int64 GetInt64(DataRow dr, string fldname)

{
if (dr.IsNull(fldname))
return Int64.MinValue;
return Convert.ToInt64(dr[fldname]);
}

static public double GetDouble(DataRow dr, string fldname)

{
if (dr.IsNull(fldname))
return double.MinValue;
return Convert.ToDouble(dr[fldname]);
}

 

#region

/// <summary>
/// 向数据库中插入或更新数据时,设置存储过程参数,如果数据库中该列值允许null,则插入null值
/// </summary>
/// <param name="pName">存储过程参数名</param>
/// <param name="pValue">参数值</param>
public static SqlParameter SetParam(string pName, object pValue)
{
//如果pValue为null,则直接返回 2004-12-6
if (pValue == null)
return new SqlParameter(pName, DBNull.Value);
switch (Type.GetTypeCode(pValue.GetType()))
{
case TypeCode.String:
string tempStr = (string)pValue;
if (tempStr == null || tempStr.Trim().Length == 0)
return new SqlParameter(pName, DBNull.Value);
return new SqlParameter(pName, tempStr);
case TypeCode.DateTime:
DateTime tempdt = (DateTime)pValue;
if (tempdt == DateTime.MinValue)
return new SqlParameter(pName, DBNull.Value);
return new SqlParameter(pName, tempdt);
default:
return new SqlParameter(pName, pValue);
}
}

#endregion

#region 设置存储过程输出参数

/// <summary>
/// 设置存储过程输出参数
/// </summary>
/// <param name="pName">参数名称</param>
/// <param name="otype">参数类型</param>
/// <param name="size">参数的长度</param>
/// <returns></returns>
public static SqlParameter SetOutParam(string pName, SqlDbType otype, int size)
{

SqlParameter oprm = new SqlParameter(pName, otype, size);

oprm.Direction = ParameterDirection.Output;
return oprm;
}
#endregion

#region 设置存储过程输出参数
/// <summary>
/// 设置存储过程输出参数
/// </summary>
/// <param name="pName">参数名称</param>
/// <param name="otype">参数类型</param>
/// <param name="size">参数的长度</param>
/// <returns></returns>
public static SqlParameter SetReturnParam(string pName, SqlDbType otype, int size)
{

SqlParameter oprm = new SqlParameter(pName, otype, size);

oprm.Direction = ParameterDirection.ReturnValue;
return oprm;
}
#endregion
}
}

转载于:https://www.cnblogs.com/hougelou/archive/2013/03/03/2942146.html

你可能感兴趣的文章
修改上一篇文章的node.js代码,支持默认页及支持中文
查看>>
java只能的round,ceil,floor方法的使用
查看>>
将txt文件转化为json进行操作
查看>>
我的2014-相对奢侈的生活
查看>>
Java设计模式
查看>>
mysql-This version of MySQL doesn’t yet support ‘LIMIT & IN/ALL/ANY/SOME 错误解决
查看>>
基本概念复习
查看>>
红黑树
查看>>
【数据库】
查看>>
WindowManager.LayoutParams 详解
查看>>
安卓中数据库的搭建与使用
查看>>
.NET 设计规范--.NET约定、惯用法与模式-2.框架设计基础
查看>>
sql 内联,左联,右联,全联
查看>>
C++关于字符串的处理
查看>>
Breaking parallel loops in .NET C# using the Stop method z
查看>>
[轉]redis;mongodb;memcache三者的性能比較
查看>>
让你的WPF程序在Win7下呈现Win8风格主题
查看>>
构建Docker Compose服务堆栈
查看>>
浮点数内存如何存储的
查看>>
JsonCpp 的使用
查看>>