【问题标题】:Cannot insert into local database无法插入本地数据库
【发布时间】:2015-07-27 15:36:13
【问题描述】:

我正在尝试使用 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();
            }


        }

正如我之前所说,无法插入捕获没有显示。关于我可能遗漏的东西的任何提示?

编辑:添加了建议的更改,连接似乎已关闭,而整个过程正在进行 PMDatabase 仍然显示红色 x。

我还发现了一个我忽略的警告,尽管它似乎与插入数据库的过程无关,但它是:warning 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.

【问题讨论】:

    标签: c# visual-studio


    【解决方案1】:

    您需要在 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();
    

    【讨论】:

    • 添加了更改,但现在它显示 System.Data.dll 中发生的“System.Data.SqlClient.SqlException”类型的第一次机会异常计数未插入。
    • 每次我运行测试或尝试使用添加功能时,与数据库的连接都会关闭。除了存储过程还有什么办法吗?在某种程度上,我认为问题可能出在连接字符串上,因为我正在使用数据集获取一些数据来填充一些组合框,并且这些组合框运行良好。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-06-16
    • 2018-10-14
    • 2015-06-11
    • 2015-12-11
    • 2017-10-23
    相关资源
    最近更新 更多