我正在尝试进行一些数据转换.不幸的是,大部分数据都是字符串,它应该是int或double等等......
所以我得到的是:
double? amount = Convert.ToDouble(strAmount);
Run Code Online (Sandbox Code Playgroud)
这种方法的问题是如果strAmount是空的,如果它是空的我希望它等于null,所以当我将它添加到数据库时,该列将为null.所以我最后写了这个:
double? amount = null;
if(strAmount.Trim().Length>0)
{
amount = Convert.ToDouble(strAmount);
}
Run Code Online (Sandbox Code Playgroud)
现在这个工作正常,但我现在有五行代码而不是一行代码.这使得事情变得更难以阅读,特别是当我有大量的列要转换时.
我以为我会使用字符串类和泛型的扩展来传入类型,这是因为它可能是double,或int或long.所以我尝试了这个:
public static class GenericExtension
{
public static Nullable<T> ConvertToNullable<T>(this string s, T type) where T: struct
{
if (s.Trim().Length > 0)
{
return (Nullable<T>)s;
}
return null;
}
}
Run Code Online (Sandbox Code Playgroud)
但我收到错误:无法将类型'string'转换为'T?'
有没有解决的办法?我不太熟悉使用泛型创建方法.
我在两个不同的数据库中有两个表.在table1中(在database1中)有一个名为column1的列,它是一个主键.现在在table2(在database2中)有一个名为column2的列,我想将它添加为外键.
我试图添加它,它给了我以下错误:
消息1763,级别16,状态0,行1
不支持跨数据库外键引用.外键Database2.table2.消息1750,级别16,状态0,行1
无法创建约束.查看以前的错误.
我是如何做到这一点的,因为表位于不同的数据库中.
我想编写一个程序来实现c#中的放大和缩小任务.我不想调整pictureBox的大小,我想调整图片的大小.但我不知道我要做什么.你能帮我吗?
我正在尝试使用readText函数:
import std.stdio;
import std.file;
string xmlName = r"D:\files\123.xml";
File file;
void main()
{
writeln("Edit source/app.d to start your project.");
file = File(xmlName, "r");
string file_text = file.readText;
}
Run Code Online (Sandbox Code Playgroud)
我收到一个错误:
Error: template std.file.readText cannot deduce function from argument types !()(File), candidates are:
C:\D\dmd2\windows\bin\..\..\src\phobos\std\file.d(499,3): std.file.readText(S = string, R)(auto ref R name) if (isSomeString!S && (isInputRange!R && !isInfinite!R && isSomeChar!(ElementType!R) || is(StringTypeOf!R)))
Run Code Online (Sandbox Code Playgroud)
我究竟做错了什么?
c# ×2
.net ×1
d ×1
foreign-keys ×1
generics ×1
image ×1
picturebox ×1
resize ×1
sql ×1
sql-server ×1
t-sql ×1