关于如何赋空值? 请教一下,怎么才可以将int类型,DataTime类型的变量赋为空值呢?要正确的阿 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 string str = "";int i = Convert.ToInt32(str);Console.WriteLine(i); 声明成可空的类型就可以了:int? a = null;DateTime dt = null; 声明成可空的类型就可以了:int? a = null;DateTime? dt = null; 声明成可空的类型就可以了:int? a = null;DateTime? dt = null; 非常感谢上面的各位兄弟,int? a = null;这真是新东东。还有个问题:@"INSERT INTO JJM_B_LOADOMETER (Dept) VALUES ( '"+deptId+"')";deptId我已经将其赋为空呢,Dept是个外键,为什么还是会因为外键出现异常呢? C#里的空值(null)不等同于数据库的空值(NULL),如果要给数据库传一个空的值,应该使用DBNull.Value,而不能使用null。object depID = DBNull.Value;@"INSERT INTO JJM_B_LOADOMETER (Dept) VALUES ( '"+deptId+"')";或if(depID==null){ @"INSERT INTO JJM_B_LOADOMETER (Dept) VALUES ( null)";}else{ @"INSERT INTO JJM_B_LOADOMETER (Dept) VALUES ( '"+deptId+"')";} 在.net 2.0中有一种叫可空类型。可空类型是 System.Nullable 结构的实例。可空类型可以表示其基础值类型正常范围内的值,再加上一个 null 值。如 int? iCount = null;class NullableExample{ static void Main() { int? num = null; if (num.HasValue == true) { System.Console.WriteLine("num = " + num.Value); } else { System.Console.WriteLine("num = Null"); } //y is set to zero int y = num.GetValueOrDefault(); // num.Value throws an InvalidOperationException if num.HasValue is false try { y = num.Value; } catch (System.InvalidOperationException e) { System.Console.WriteLine(e.Message); } }} int? iCount和int类型完全不同吧。@"INSERT INTO JJM_B_LOADOMETER (Dept) VALUES ( '"+deptId+"')";Dept在数据库中是bigint类型,所以上面的做法虽然是null但是类型不匹配,还是会产生外键冲突。 deptId不可以直接给值。建议用"INSERT INTO JJM_B_LOADOMETER (Dept) VALUES (@deptId)"然后cmd.Parameters.Add("@deptId",DBNull.Value); // cmd为SqlCommand类这样就可以了。 C#获取本地所有的应用程序(exe)列表 C# 读取配置文件 谢谢各位大侠!!!!! 在哪改一下VS的默认调试器啊,现在不能调试了,出错不能找到这个TT? 高分求购一个关于手机号码的正则表达式 treeview的拖动问题。 C#概念问题 关于数组的问题 只好把 vs.net 2003 卸掉了 winForm中怎样让MDI的子窗口不能移动? C#会存在内存碎片的问题吗? 动态生成了控件后,如何调用该控件 控制台应用程序问题
int i = Convert.ToInt32(str);
Console.WriteLine(i);
int? a = null;DateTime dt = null;
int? a = null;DateTime? dt = null;
int? a = null;DateTime? dt = null;
还有个问题:@"INSERT INTO JJM_B_LOADOMETER (Dept) VALUES ( '"+deptId+"')";
deptId我已经将其赋为空呢,Dept是个外键,为什么还是会因为外键出现异常呢?
如果要给数据库传一个空的值,应该使用DBNull.Value,而不能使用null。object depID = DBNull.Value;
@"INSERT INTO JJM_B_LOADOMETER (Dept) VALUES ( '"+deptId+"')";或if(depID==null)
{
@"INSERT INTO JJM_B_LOADOMETER (Dept) VALUES ( null)";
}
else
{
@"INSERT INTO JJM_B_LOADOMETER (Dept) VALUES ( '"+deptId+"')";
}
可空类型是 System.Nullable 结构的实例。可空类型可以表示其基础值类型正常范围内的值,再加上一个 null 值。
如 int? iCount = null;
class NullableExample
{
static void Main()
{
int? num = null;
if (num.HasValue == true)
{
System.Console.WriteLine("num = " + num.Value);
}
else
{
System.Console.WriteLine("num = Null");
} //y is set to zero
int y = num.GetValueOrDefault(); // num.Value throws an InvalidOperationException if num.HasValue is false
try
{
y = num.Value;
}
catch (System.InvalidOperationException e)
{
System.Console.WriteLine(e.Message);
}
}
}
@"INSERT INTO JJM_B_LOADOMETER (Dept) VALUES ( '"+deptId+"')";
Dept在数据库中是bigint类型,所以上面的做法虽然是null但是类型不匹配,
还是会产生外键冲突。
建议用"INSERT INTO JJM_B_LOADOMETER (Dept) VALUES (@deptId)"
然后cmd.Parameters.Add("@deptId",DBNull.Value); // cmd为SqlCommand类
这样就可以了。