我试图用windows窗体的值对表进行简单的插入,但即使使用调试器并一步一步地检查,也不会出现错误,插入失败。
我在服务器资源管理器中注意到,一旦启动调试器,连接就会显示并关闭。我得到的连接字符串如下:
SqlConnection con = new SqlConnection(UI.Properties.Settings.Default.PM_ConnectionString);

这是我的代码:
public void agregarLocal(String _nombre, String _telefono, int _preferencia, int _provincia, String _url, SqlConnection _con, String _descripcion = "")
{
using (_con)
{
_con.Open();
try
{
using (SqlCommand command = new SqlCommand(
"INSERT INTO Locales VALUES(@loc_nombre, @loc_telefono, @loc_descripcion, @loc_preferencia, @loc_provincia, @loc_url)", _con))
{
command.Parameters.Add(new SqlParameter("@loc_nombre", _nombre));
command.Parameters.Add(new SqlParameter("@loc_telefono", _telefono));
command.Parameters.Add(new SqlParameter("@loc_descripcion", _descripcion));
command.Parameters.Add(new SqlParameter("@loc_preferencia", _preferencia));
command.Parameters.Add(new SqlParameter("@loc_provincia", _provincia));
command.Parameters.Add(new SqlParameter("@loc_url", _url));
command.ExecuteNonQuery();
}
}
catch (SqlException ex)
{
Console.Write(ex.Message);
}
finally
{
if (_con.State == ConnectionState.Open)
_con.Close();
}
}无法插入的catch还没有如我前面所说的那样显示。有什么我可能漏掉的线索吗?
编辑:添加了建议的更改,当整个过程正在进行时,连接仍然被关闭-- PMDatabase仍然显示红色x。
此外,我还发现了一个我忽略的警告,尽管它似乎与插入数据库的过程无关--这里是:警告MSB3270:There was a mismatch between the processor architecture of the project being built "MSIL" and the processor architecture of the reference "WebKitBrowser, Version=0.5.0.0, Culture=neutral, PublicKeyToken=b967213f6d29a3be, processorArchitecture=x86", "x86". This mismatch may cause runtime failures. Please consider changing the targeted processor architecture of your project through the Configuration Manager so as to align the processor architectures between your project and references, or take a dependency on references with a processor architecture that matches the targeted processor architecture of your project.
发布于 2015-07-27 01:08:02
您需要在您的SQL参数名称中包括@符号:
command.Parameters.Add(new SqlParameter("@loc_nombre", _nombre));
command.Parameters.Add(new SqlParameter("@loc_telefono", _telefono));
command.Parameters.Add(new SqlParameter("@loc_descripcion", _descripcion));
command.Parameters.Add(new SqlParameter("@loc_preferencia", _preferencia));
command.Parameters.Add(new SqlParameter("@loc_provincia", _provincia));
command.Parameters.Add(new SqlParameter("@loc_url", _url));
command.ExecuteNonQuery();https://stackoverflow.com/questions/31643581
复制相似问题