无法将类型'decimal'隐式转换为'int'.存在显式转换(您是否缺少演员表?).
问题描述:
我正在调用GetSerialNo函数,但它显示一些错误,例如:
无法将类型十进制"隐式转换为"int".存在显式转换(您是否缺少演员表?).
谁能帮我解决这个问题.
这是代码:
Hi,
I am calling a GetSerialNo function But it showing some error like:
Cannot implicitly convert type ''decimal'' to ''int''. An explicit conversion exists (are you missing a cast?).
Can anyone help me to solve this issue.
Here is the code:
int slNo= GetSerailNo(keydata);
private int GetSerailNo(String keydata)
{
SqlConnection con = new SqlConnection(@"server=Servername;database=DBNAME;uid=Username;pwd=Pwd;max pool size=250;Connect Timeout=0");
con.Open();
cmd = new SqlCommand("select isnull(max(slno)+1,1) from d001docs where source_keydata=''" + keydata + "''", con);
dynamic no = cmd.ExecuteScalar();
cmd.Dispose();
con.Close();
return no;
}</pre>
Thanks in advance
答
no
是decimal
,而函数返回值是int
,因此错误(decimal
数据类型比)您可以:
- 将函数(和
slNo
变量)数据类型更改为decimal
.
no
isdecimal
while your function return value isint
, hence the error (decimal
datatype is wider thanint
) you may either:
- Change your function (and your
slNo
variable) data type todecimal
.
- 将
no
强制转换为int
:
- Explicitely cast
no
toint
:
return (int) no;
(警告:仅在确保值始终位于int
范围内时使用此技术.)
(warning: use this technique only if you are sure the values will always fit into int
range).
它就是说的意思.您的代码传递了一个期望为int的小数.我想,您需要使用Convert.ToInt()显式转换它.我认为问题是您的退货类型,为什么不只更改它呢?另外,购买一本C#书并阅读,错误消息不会比这本书更清楚.
It means what it says. Your code is passing a decimal where an int is expected. You need to explicitly cast it, with Convert.ToInt(), I guess. I assume the issue is your return type, why not just change it ? Also, buy a C# book and read it, the error messages don''t get any clearer than this one.
dynamic no = cmd.ExecuteScalar();
您需要显式地将no转换为整数,
dynamic no = cmd.ExecuteScalar();
u need to cast explicitly the no in integer,