Winforms应用程序中的数据访问问题

时间:2017-05-18 09:43:42

标签: c# winforms

代码是: 品名:

create table posts(
     post_id INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
     author_id INT NOT NULL,
     title varchar(50),
     post TEXT,
     data DATE,
     FOREIGN KEY (author_id)
     REFERENCES labels(id)
     ON DELETE RESTRICT
     )ENGINE=INNODB;

存储

    /// <summary>
    /// Method to insert an Commodity
    /// </summary>
    /// <param name="commodity">commodity's information</param>
    /// <returns>void</returns>
    public bool Add(Models.CommodityInfo commodity)
    {
        if (string.IsNullOrEmpty(commodity.Sp_itemnum) || string.IsNullOrEmpty(commodity.Sp_name))
            return false;
        else
            return dal.Insert(commodity);
    }
    ///interface is omitted
    /// <summary>
    /// Method to insert an Commodity
    /// </summary>
    /// <param name="commodity">commodity's information</param>
    /// <returns>Whether the operation is successful</returns>
    public bool Insert(CommodityInfo commodity)
    {
        StringBuilder strSQL = new StringBuilder(SQL_INSERT_COMMODITY);

        // Get each commands parameter arrays
        SqlParameter[] Parms = GetParameters();

        SqlCommand cmd = new SqlCommand();

        // Set up the parameters
        Parms[0].Value = commodity.Sp_itemnum;
        Parms[1].Value = commodity.Sp_name;
        Parms[2].Value = commodity.Sp_spec;
        Parms[3].Value = commodity.Sp_classify;
        Parms[4].Value = commodity.Sp_brand;
        Parms[5].Value = commodity.Sp_purcprice;
        Parms[6].Value = commodity.Sp_saleprice;
        Parms[7].Value = commodity.Sp_origin;
        Parms[8].Value = commodity.Sp_pack;
        Parms[9].Value = commodity.Sp_note;
        foreach (SqlParameter parm in Parms)
            cmd.Parameters.Add(parm);

        // Create the connection to the database
        using (SqlConnection conn = new SqlConnection(SqlHelper.connStr))
        {
            conn.Open();
            cmd.Connection = conn;
            cmd.CommandType = CommandType.Text;
            cmd.CommandText = strSQL.ToString();

            int r = cmd.ExecuteNonQuery();
            //Clear the parameters
            cmd.Parameters.Clear();
            return Convert.ToBoolean(r);
        }
    }

问题是: 数据库表结构: Table of commodity and storage

错误地点:

    /// <summary>
    /// Method to add a commodity
    /// </summary>
    /// <param name="spName">name of commodity of add</param>
    /// <returns>Whether the operation is successful</returns>
    public bool addCommodity(string spName)
    {
        bool r = true;

        BLL.WareHouseManager bwm = new WareHouseManager();
        BLL.CommodityManager bcm = new CommodityManager();
        Models.StorageInfo msi = new Models.StorageInfo();
        msi.Sp_id = bcm.GetCommodityIdbyName(spName);
        msi.Cc_num = 0;

        IList < int> ckIds = bwm.GetAllCkId();
        for(int i = 0; i < ckIds.Count; ++i)
        {
            msi.Ck_id = ckIds[i];
            if (!dal.Insert(msi))
                r = false;
        }
        return r;
    }
    /// <summary>
    /// Method to Insert a storageinfo
    /// </summary>
    public bool Insert(Models.StorageInfo msi)
    {
        StringBuilder strSQL = new StringBuilder(SQL_INSERT_RECORD);

        // Get each commands parameter arrays
        SqlParameter[] parms = GetUpdateParameters();

        SqlCommand cmd = new SqlCommand();

        // Set up the parameters
        parms[0].Value = msi.Ck_id;
        parms[1].Value = msi.Sp_id;
        parms[2].Value = msi.Cc_num;

        foreach (SqlParameter parm in parms)
            cmd.Parameters.Add(parm);

        // Create the connection to the database
        using (SqlConnection conn = new SqlConnection(SqlHelper.connStr))
        {
            conn.Open();
            cmd.Connection = conn;
            cmd.CommandType = CommandType.Text;
            cmd.CommandText = strSQL.ToString();

            int r = cmd.ExecuteNonQuery();
            //Clear the parameters
            cmd.Parameters.Clear();
            return Convert.ToBoolean(r);
        }
    }

VS2015给出了“外键冲突”的信息,但它在sql server Managerment studio中成功执行了

其他:我是中国人。如果你懂中文,你可以看一下 enter link description here

1 个答案:

答案 0 :(得分:0)

请检查您是否尝试在“Table_CC.sp_id列中插入一些值Table_SP.sp_id