Tag: sqldatatypes

如何使用SqlDataRecord处理Nullable类型

我正在解析XML(LINQ to XML),并且我在元素/属性为空的情况下使用可空类型( int?和decimal? )。 但是,在构建我的集合以传递给DB(使用TVP)时,我不知道如何处理值实际为null的情况。 我无法将null传递给SqlDataRecord SetInt32或SetDecimal,我不想设置为零….我实际上希望它为null。 告诉我int没有超载? 下面的计数是可以为空的类型( int? Count) SqlDataRecord rec = new SqlDataRecord( new SqlMetaData(“Name”, SqlDbType.VarChar, 50), new SqlMetaData(“Type”, SqlDbType.VarChar, 50), new SqlMetaData(“Count”, SqlDbType.Int)); rec.SetString(0, dm.Name); rec.SetString(1, dm.Type); rec.SetString(2, dm.Count); 任何想法如何处理这个而不传递零(保持null)?

仅在数据库中存储日期而不是时间部分C#

我有一个测试类和一个ExecutionDate属性,它只存储日期,但是当我们使用[DataType(DataType.Date)] ,它也将时间部分存储在数据库中,但我只想要日期部分。 public class Test { [Key] public int Id { get; set; } [DataType(DataType.Date)] public DateTime ExecutionDate { get; set; } } 有没有办法只使用Entity Framework在db中存储日期时间部分? 请帮我…. 我在使用[DataType(DataType.Date)]时添加了快照,它存储时间部分00:00我要删除它

如何获取DataTable中列的SqlType?

我有一个从SQL数据库获得的DataTable,如下所示: using (SqlCommand cmd = new SqlCommand(query, _sqlserverDB)) { using (SqlDataAdapter adapter = new SqlDataAdapter(cmd)) { DataSet dataSet = new DataSet(); adapter.Fill(dataSet); result = (dataSet != null && dataSet.Tables != null && dataSet.Tables.Count > 0) ? dataSet.Tables[0] : null; } } 当我尝试通过dataColumn.DataType获取每列的DataType时,我得到C#类型(Int32,Int64,String等)。 问题:如何访问本机SQL数据类型(varchar,nvarchar,bigint …)而不是C#类型? 我试过dataColumn.DataType.UnderlyingSystemType,结果是一样的。