[英]Exception cannot convert nvarchar to int
I am getting the following error, even though I have checked many times I cannot fathom why would such thing pop up when I am only searching by a string, I already checked that when filling the Local object below the properties are correct. 我收到以下错误,即使我已经多次检查我无法理解为什么当我只通过字符串搜索时会弹出这样的东西,我已经检查过,当填充Local属性下面的属性是正确的。
[TestMethod]
public void TestSearchLocalName()
{
try
{
IEnumerable lstLocales = gestor.searchLocal("Local1");
Assert.IsNotNull(lstLocales);
Console.Write("Se ha conseguido una lista de locales.");
}
catch (Exception ex)
{
Assert.Fail(ex.ToString());
}
}
Method at gestor: 在gestor的方法:
public IEnumerable<Local> searchLocal(String name)
{
try
{
return LocalRepository.Instance.getByName(name);
}
catch (DataAccessException ex)
{
throw ex;
}
catch (Exception ex)
{
throw new Exception(String.Format("Error: {0}", ex.Message));
}
}
Repository: 库:
public IEnumerable<Local> getByName(String name) {
List<Local> lista = new List<Local>();
try
{
DataTable tablaResultado = DataBaseAccess.advanceStoredProcedureRequest("pa_local_buscar_nombre", new SPP[] {
new SPP("loc_nombre", name.ToString())
});
if (tablaResultado.Rows.Count > 0)
{
foreach (DataRow fila in tablaResultado.Rows)
{
lista.Add(new Local(
int.Parse(fila.ItemArray[0].ToString()),
fila.ItemArray[1].ToString(),
fila.ItemArray[2].ToString(),
fila.ItemArray[3].ToString(),
fila.ItemArray[4].ToString(),
int.Parse(fila.ItemArray[5].ToString()),
int.Parse(fila.ItemArray[6].ToString())
));
}
}
else
{
lista.Add(new Local());
}
}
catch (Exception ex)
{
throw new Exception(String.Format("SQL Error: {0}", ex.Message));
}
return lista;
}
The stored procedure does work when I test it directly using the same name in he unit test, leading me to believe the error is not here but just in case: 当我在单元测试中使用相同的名称直接测试时,存储过程确实有效,这让我相信错误不在这里,只是以防万一:
CREATE PROCEDURE [dbo].[pa_local_buscar_nombre]
@loc_nombre as nchar(20)
AS
BEGIN
SET NOCOUNT ON;
SELECT
loc_id, loc_nombre, loc_url, loc_telefono,
loc_descripcion, loc_preferencia, loc_provincia
FROM
Locales
WHERE
loc_nombre LIKE '%' + @loc_nombre + '%'
END
Exception: 例外:
Test Name: TestSearchLocalNombre 测试名称:TestSearchLocalNombre
Test FullName: UT.UnitTestLocales.TestSearchLocalNombre 测试FullName:UT.UnitTestLocales.TestSearchLocalNombre
Test Outcome: Failed 测试结果:失败
Test Duration: 0:00:00.0568857 测试持续时间:0:00:00.0568857
Result Message: 结果消息:Assert.Fail failed. Assert.Fail失败了。 System.Exception: Error: SQL Error: Error converting data type nvarchar to int. System.Exception:错误:SQL错误:将数据类型nvarchar转换为int时出错。
at BLL.GestorLocales.searchLocal(String name) 在BLL.GestorLocales.searchLocal(String name)
EDIT: 编辑:
Advanced stored procedure: 高级存储过程:
public static DataTable advanceStoredProcedureRequest(String name, SPP[] parameters = null)
{
String storedProcedure = "dbo." + name;
SqlCommand cmd = new SqlCommand(name, getConection);
cmd.CommandType = CommandType.StoredProcedure;
if (parameters != null && parameters.Length > 0)
{
foreach (SPP p in parameters)
{
cmd.Parameters.AddWithValue("@" + p.Name, p.Value);
}
}
DataTable table = new DataTable();
SqlDataAdapter dataAdapter = new SqlDataAdapter(cmd);
dataAdapter.Fill(table);
return table;
}
EDIT2: EDIT2:
SSP: SSP:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace DAL
{
public class SPP
{
public String Name { get; set; }
public String Value { get; set; }
/**
* Stored Procedure Parameter
*/
public SPP(String name, String value)
{
this.Name = name;
this.Value = value;
}
}
}
Debugger; 调试器;
SOLVED: 解决了:
In the end it was human stupidity and careleness which led to this whole conundrum, I thank you all who helped me reach the right conclusion. 最后是人类的愚蠢和羞怯导致了这整个难题,我感谢所有帮助我做出正确结论的人。
版本控制存在重复数据库的问题,我正在使用过时的版本控制。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.