本文整理汇总了C#中System.Data.SqlClient.SqlDataReader.GetDataTypeName方法的典型用法代码示例。如果您正苦于以下问题:C# SqlDataReader.GetDataTypeName方法的具体用法?C# SqlDataReader.GetDataTypeName怎么用?C# SqlDataReader.GetDataTypeName使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.Data.SqlClient.SqlDataReader
的用法示例。
在下文中一共展示了SqlDataReader.GetDataTypeName方法的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Property
public void Property(string key, SqlDataReader reader, int idx)
{
jsonWriter.WritePropertyName(key);
string typeName = reader.GetDataTypeName(idx);
if (reader.IsDBNull(idx))
jsonWriter.WriteNull();
else if (typeName == "decimal")
jsonWriter.WriteValue(reader.GetDecimal(idx));
else if (typeName == "datetime")
jsonWriter.WriteValue(reader.GetDateTime(idx));
else if (typeName == "varchar" || typeName == "nvarchar")
jsonWriter.WriteValue(reader.GetString(idx));
else
throw new Exception("Unknown data type " + typeName);
}
示例2: SqlDataHelper
public SqlDataHelper(SqlDataReader reader)
{
_reader=reader;
if (_cnames == null)
{
_cnames = new string[_reader.FieldCount];
_cdbtypes = new string[_reader.FieldCount];
_ctypes = new Type[_reader.FieldCount];
for (int i = 0; i < _reader.FieldCount; i++)
{
_cdbtypes[i] = _reader.GetDataTypeName(i);
_ctypes[i] = _reader.GetFieldType(i);
_cnames[i] = _reader.GetName(i);
}
}
}
示例3: GetDbDateTime
public static DateTime GetDbDateTime(SqlDataReader myReader, int myColumnNo)
{
DateTime myData = new DateTime(1900, 1, 1);
string data = "";
string datatype = "";
try
{
if (!myReader.IsDBNull(myColumnNo))
{
datatype = myReader.GetDataTypeName(myColumnNo);
if (datatype.Equals("datetime") || datatype.Equals("date"))
{
myData = myReader.GetDateTime(myColumnNo);
}
else if (datatype.Equals("varchar"))
{
data = myReader.GetString(myColumnNo);
//Debug.Print("varchar data [" + data + "]");
if (data.Length <= 0)
myData = new DateTime(1900, 1, 1);
else if (data.IndexOf("x") >= 0)
Debug.Print("varchar mask data [" + data + "]");
else
myData = Convert.ToDateTime(data);
}
}
else
myData = new DateTime(1900, 1, 1);
}
catch (Exception)
{
myData = new DateTime(1900, 1, 1);
}
return myData;
}
示例4: GetDataTypeNameTest
public void GetDataTypeNameTest ()
{
cmd.CommandText = "Select id, type_tinyint, 10,null from numeric_family where id=1";
reader = cmd.ExecuteReader ();
Assert.AreEqual ("tinyint", reader.GetDataTypeName(1), "#1");
Assert.AreEqual ("int", reader.GetDataTypeName(2), "#2");
//need check on windows
Assert.AreEqual ("int", reader.GetDataTypeName(3), "#3");
try {
reader.GetDataTypeName (10);
Assert.Fail ("#4 Exception shud be thrown");
} catch (IndexOutOfRangeException e) {
Assert.AreEqual (typeof (IndexOutOfRangeException), e.GetType (),
"#5 Incorrect Exception : " + e);
}
}
示例5: JsonFormat
/// <summary>
/// Converts an individual SqlDataReader row referenced by its column index integer into JSON
/// </summary>
/// <param name="dr">SqlDataReader - the row returned from the SQL select operation</param>
/// <param name="colno">The column index used to look up the column meta-data</param>
/// <returns>String</returns>
public static String JsonFormat(SqlDataReader dr, Int32 colno)
{
String result = String.Empty;
String sval = String.Empty;
switch (dr.GetDataTypeName(colno).ToLower())
{
case "datetime":
case "datetime2":
sval = dr.GetValue(colno).ToString().Trim();
if (sval.Length < 1)
result = String.Empty;
else {
CultureInfo ci = CultureInfo.InvariantCulture;
DateTime rdate = DateTime.Parse(sval);
//result = string.Format("\"{0}\":\"{1}\"", dr.GetName(colno), rdate.ToString("MM/dd/yyyy HH:mm:ss.FFF", ci.DateTimeFormat));
// format to JavaScript DateTime formatting
result = String.Format("\"{0}\":new Date({1})", dr.GetName(colno), ToUnixTime(rdate));
}
break;
case "varchar":
case "nvarchar":
case "uniqueidentifier":
case "smalldatetime":
case "char":
case "nchar":
case "date":
case "text":
case "ntext":
case "string":
sval = dr.GetValue(colno).ToString().Trim();
if (sval.Length < 1)
result = String.Empty;
//is this a json object column?
else if (sval.StartsWith("{"))
{
if (sval.Substring(1, sval.Length - 1).Trim().Length < 1)//ignore it if it is empty
sval = String.Empty;
else
sval = String.Format("\"{0}\":{1}", dr.GetName(colno), dr.GetValue(colno));
result = sval;
}
//is this a json array column?
else if (sval.StartsWith("["))
{
if (sval.Substring(1, sval.Length - 1).Trim().Length < 1)//ignore it if it is empty
sval = String.Empty;
else
sval = String.Format("\"{0}\":{1}", dr.GetName(colno), dr.GetValue(colno));
result = sval;
}
else
result = string.Format("\"{0}\":\"{1}\"", dr.GetName(colno), JsonEscape(sval));
break;
case "int":
case "smallint":
case "tinyint":
case "decimal":
case "money":
case "bigint":
case "float":
case "numeric":
case "number":
case "real":
case "smallmoney":
result = string.Format("\"{0}\":{1}", dr.GetName(colno), dr.GetValue(colno));
break;
case "boolean":
case "bit":
result = string.Format("\"{0}\":{1}", dr.GetName(colno), dr.GetValue(colno).Equals(true) ? "true" : "false");
break;
default:
result = String.Empty;
break;
}
return result;
}