使用SqlCommand时是否需要指定SQL数据类型?

时间:2021-04-19 10:20:08

Is there any provable reason why I should always specify the SQL data type for SqlCommand paramenters?

是否有任何可证明的理由我应该始终为SqlCommand参数指定SQL数据类型?

3 个解决方案

#1


1  

The only time I've run into a case when I had to specify a data type was when passing in DBNull. When I wasn't specifying a data type, it defaulted to a Varchar, and ended up crashing because I was trying to set an integer value to Null.

我唯一一次遇到必须指定数据类型的情况是在传入DBNull时。当我没有指定数据类型时,它默认为Varchar,并最终崩溃,因为我试图将整数值设置为Null。

#2


1  

command.AddWithValue("@Id","1"); // Id is an int

command.AddWithValue( “@ ID”, “1”); // Id是一个int

command.AddWithValue("@Id",'1'); // Id is an int

command.AddWithValue( “@ ID”, '1'); // Id是一个int

Do you guys know there is any difference between " " and ' ' when we use inside SqlCommand? I have google for a long time but I didn't see anything about this trouble.

当我们在SqlCommand中使用时,你们知道“”和“之间有什么区别吗?我有谷歌很长一段时间,但我没有看到任何关于这个麻烦。

#3


0  

and you will find that sometimes you will get very strange errors when you have not specified the sql type and size

你会发现,当你没有指定sql类型和大小时,有时你会得到非常奇怪的错误

it is safest - and more self-documenting - to always declare the correct sql type and size

最安全 - 更自我记录 - 总是声明正确的sql类型和大小

#1


1  

The only time I've run into a case when I had to specify a data type was when passing in DBNull. When I wasn't specifying a data type, it defaulted to a Varchar, and ended up crashing because I was trying to set an integer value to Null.

我唯一一次遇到必须指定数据类型的情况是在传入DBNull时。当我没有指定数据类型时,它默认为Varchar,并最终崩溃,因为我试图将整数值设置为Null。

#2


1  

command.AddWithValue("@Id","1"); // Id is an int

command.AddWithValue( “@ ID”, “1”); // Id是一个int

command.AddWithValue("@Id",'1'); // Id is an int

command.AddWithValue( “@ ID”, '1'); // Id是一个int

Do you guys know there is any difference between " " and ' ' when we use inside SqlCommand? I have google for a long time but I didn't see anything about this trouble.

当我们在SqlCommand中使用时,你们知道“”和“之间有什么区别吗?我有谷歌很长一段时间,但我没有看到任何关于这个麻烦。

#3


0  

and you will find that sometimes you will get very strange errors when you have not specified the sql type and size

你会发现,当你没有指定sql类型和大小时,有时你会得到非常奇怪的错误

it is safest - and more self-documenting - to always declare the correct sql type and size

最安全 - 更自我记录 - 总是声明正确的sql类型和大小