ADO.NET: ExecuteScalar() 连接问题

ADO.NET: ExecuteScalar() Connection problum

这是我的 C# 代码

protected bool SaveItems(ObservableCollection<Model> Items, Int32 UserId)
    {
        if (sqlcon.State == ConnectionState.Closed)
            sqlcon.Open();
        foreach (Model Rate in Items)
        {
            SqlCommand sccmd = new SqlCommand("ItemsAdd", sqlcon);
            sccmd.CommandType = CommandType.StoredProcedure;
            sccmd.Parameters.Add("@ItemsId", SqlDbType.Int).Value = Rate.Id;
            sccmd.Parameters.Add("@RMId", SqlDbType.Int).Value = Rate.HeadId;
            sccmd.Parameters.Add("@Date", SqlDbType.DateTime).Value = Rate.Date;
            sccmd.Parameters.Add("@Price", SqlDbType.Decimal).Value = Rate.Price;
            sccmd.Parameters.Add("@UserId", SqlDbType.Int).Value = UserId;
            if (Rate.Id == 0)
            {
                Rate.Id = Convert.ToInt32(sccmd.ExecuteScalar());
            }
            else
                sccmd.ExecuteScalar();
        }
        sqlcon.Close();
        return true;
    }

重复点击添加按钮,出现此错误

Additional information: ExecuteScalar requires an open and available Connection. The connection's current state is closed.

是什么导致了这个错误,我该如何解决?

像这样尝试:

protected bool SaveItems(ObservableCollection<Model> Items, Int32 UserId)
{
    if (sqlcon.State == ConnectionState.Closed)
{
        sqlcon.Open();
    foreach (Model Rate in Items)
    {
        SqlCommand sccmd = new SqlCommand("ItemsAdd", sqlcon);
        sccmd.CommandType = CommandType.StoredProcedure;
        sccmd.Parameters.Add("@ItemsId", SqlDbType.Int).Value = Rate.Id;
        sccmd.Parameters.Add("@RMId", SqlDbType.Int).Value = Rate.HeadId;
        sccmd.Parameters.Add("@Date", SqlDbType.DateTime).Value = Rate.Date;
        sccmd.Parameters.Add("@Price", SqlDbType.Decimal).Value = Rate.Price;
        sccmd.Parameters.Add("@UserId", SqlDbType.Int).Value = UserId;
        if (Rate.Id == 0)
        {
            Rate.Id = Convert.ToInt32(sccmd.ExecuteScalar());
        }
        else
            sccmd.ExecuteScalar();
    }
    sqlcon.Close();
    }
    return true;
}