我在C#中有一个double longitude =1.041970849797e-05
的数字
当我尝试将其插入decimal(9,6)
列时,收到错误消息:
将数据类型nvarchar转换为数字
时出错
如何将上述内容正确保存为小数?
C#应用程序代码:
static void Main(string[] args)
{
double lat=1.041970849797e-05;
insertRecs(lat);
}
private static void insertRecs(double latitude)
{
Int32 rowsAffected = 0;
string connectionString = GetConnectionString();
using (SqlConnection connection = new SqlConnection(connectionString))
{
connection.Open();
SqlCommand cmd = new SqlCommand("dbo.usp_temp", connection);
cmd.CommandType = CommandType.StoredProcedure;
cmd.CommandTimeout = 90;
try
{
rowsAffected = cmd.ExecuteNonQuery();
}
catch (Exception ep)
{
Console.WriteLine("Exception in Insertrecords ");
Console.WriteLine(ep.Message);
}
}
}
SQL存储过程:
create PROCEDURE [dbo].[usp_temp]
--The parameters for the stored procedure
@latitude decimal(9,6)
AS
BEGIN
insert into temptest(latitude) values(@latitude);
END
答案 0 :(得分:1)
查看您提供给我们的代码示例,在运行存储过程时,实际上并未映射latitude
变量。
你需要添加几行代码:
SqlParameter param = cmd.Parameters.Add("@latitude", SqlDbType.Decimal);
param.Direction = ParameterDirection.Input;
param.Value = latitude;
并编辑到您的代码中:
using (SqlConnection connection = new SqlConnection(connectionString))
{
connection.Open();
SqlCommand cmd = new SqlCommand("dbo.usp_temp", connection);
cmd.CommandType = CommandType.StoredProcedure;
cmd.CommandTimeout = 90;
// Map the parameters into the Stored Procedure
SqlParameter param = cmd.Parameters.Add("@latitude", SqlDbType.Decimal);
param.Direction = ParameterDirection.Input;
param.Value = latitude;
try
{
rowsAffected = cmd.ExecuteNonQuery();
}
catch (Exception ep)
{
Console.WriteLine("Exception in Insertrecords ");
Console.WriteLine(ep.Message);
}
}