我不确定自己到底在做什么错,请问有人可以纠正吗?我需要使用C#确定从SQL Server数据库中检索到的列的类型。
说,我有这个:
SqlConnection cn = new SqlConnection("Sql Connection String"); SqlCommand cmd = new SqlCommand("SELECT * FROM [TableName]", cn); SqlDataReader rdr = cmd.ExecuteReader(); while (rdr.Read()) { for (int c = 0; c < rdr.VisibleFieldCount; c++) { System.Type type = rdr.GetFieldType(c); //So can I do this? (Pseudo-code) //switch(type) //{ //case string: //case int: //case DateTime: //etc. //} } }
您可以执行以下操作:
/* ... code .... */ System.Type type = rdr.GetFieldType(c); switch (Type.GetTypeCode(type)) { case TypeCode.DateTime: break; case TypeCode.String: break; default: break; } /* ... code .... */