You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

System.InvalidOperationException异常排查:C#数据插入代码报错求助

异常原因排查
  • SqlCommand构造参数错误:SQL语句字符串错误地将,con包含在内,导致SqlCommand仅初始化了SQL语句,未关联数据库连接对象con。执行ExecuteNonQuery()时,命令无有效连接,触发System.InvalidOperationException。
  • 参数赋值逻辑错误:@dateofentry参数使用txt_dat.Text = string.Empty赋值,这是清空文本框的操作,会传入空字符串到数据库,既不符合业务逻辑,也可能违反字段非空约束。
  • 异常捕获范围不足:代码仅捕获SqlException,未处理InvalidOperationException,导致异常未被拦截直接抛出。
  • 数据库连接未安全释放:未用using语句管理SqlCommand和SqlConnection,若发生异常,con.Close()可能无法执行,造成连接泄漏。
解决方法

针对上述问题逐一修正:

  1. 修正SqlCommand构造:将连接对象con作为第二个参数传入,而非包含在SQL字符串中

    SqlCommand cmd = new SqlCommand("INSERT INTO DataEntry Values(@vehicleid,@vehiclename,@exteriorcolor,@interiorcolor,@model,@vin_number,@plate_number,@location,@option_codes,@dateofentry,@note)", con);
    
  2. 修正dateofentry参数赋值:传入文本框实际值,而非清空操作

    cmd.Parameters.AddWithValue("@dateofentry", txt_dat.Text);
    

    如果dateofentry是日期类型,建议转换为DateTime后传入,比如DateTime.Parse(txt_dat.Text),同时需处理格式错误

  3. 扩大异常捕获范围:添加InvalidOperationException的捕获逻辑

    catch (SqlException ex)
    {
        MessageBox.Show(ex.Message);
    }
    catch (InvalidOperationException ex)
    {
        MessageBox.Show(ex.Message);
    }
    
  4. 用using管理资源:确保数据库连接和命令对象自动释放,避免泄漏

    using (SqlCommand cmd = new SqlCommand("INSERT INTO DataEntry Values(...)", con))
    {
        // 参数赋值、执行逻辑
    }
    
修正后的完整代码
private void button_Click_1(object sender, RoutedEventArgs e)
{
    try 
    {
        if (isValid())
        {
            using (SqlCommand cmd = new SqlCommand("INSERT INTO DataEntry Values(@vehicleid,@vehiclename,@exteriorcolor,@interiorcolor,@model,@vin_number,@plate_number,@location,@option_codes,@dateofentry,@note)", con))
            {
                cmd.CommandType = CommandType.Text;

                cmd.Parameters.AddWithValue("@vehicleid", txt_vehicleid.Text);
                cmd.Parameters.AddWithValue("@vehiclename", txt_vehiclename.Text);
                cmd.Parameters.AddWithValue("@exteriorcolor", txt_exteriorcolor.Text);
                cmd.Parameters.AddWithValue("@interiorcolor", txt_interiorcolor.Text);
                cmd.Parameters.AddWithValue("@model", txt_model.Text);
                cmd.Parameters.AddWithValue("@vin_number", txt_vinnumber.Text);
                cmd.Parameters.AddWithValue("@plate_number", txt_platenumber.Text);
                cmd.Parameters.AddWithValue("@location", txt_location.Text);
                cmd.Parameters.AddWithValue("@option_codes", txt_optioncode.Text);
                cmd.Parameters.AddWithValue("@dateofentry", txt_dat.Text);
                cmd.Parameters.AddWithValue("@note", txt_note.Text);

                con.Open();
                cmd.ExecuteNonQuery();
                con.Close();

                LoadGrid();
                MessageBox.Show("Successfully registered", "Saved", MessageBoxButton.OK, MessageBoxImage.Information);
                cleardata();
            }
        }
    }
    catch (SqlException ex)
    {
        MessageBox.Show(ex.Message);
    }
    catch (InvalidOperationException ex)
    {
        MessageBox.Show(ex.Message);
    }
}

内容的提问来源于stack exchange,提问作者AD Amazing

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.15 08:50:36