查询和数据库的Vb.net错误

Dim datetimepicker1 As String = Format(System.DateTime.Now,“yyyy-MM-dd HH:mm:ss”)

    Try
        Dim cmd As MySqlCommand = New MySqlCommand
        With cmd
            .CommandText = "INSERT INTO tbl_product (`prod_name`,`prod_desc`, `cat_id`, `uom_id`,`uom_num`, `dept_id`, `brand_id`, `size_id`, `type_id`, `remarks`, `date`) values (@prod_name,@prod_desc,@cat_id,@uom_id,@uom_num,@dept_id,@brand_id,@size_id,@type_id,@remarks,@date)"
            .Connection = SQLConnection
            .CommandType = CommandType.Text
            .Parameters.AddWithValue("@prod_name", TextBox1.Text)
            .Parameters.AddWithValue("@prod_desc", TextBox2.Text)
            .Parameters.AddWithValue("@cat_id", ComboBox1.Text)
            .Parameters.AddWithValue("@uom_id", ComboBox2.Text)
            .Parameters.AddWithValue("@uom_num", TextBox3.Text)
            .Parameters.AddWithValue("@dept_id", ComboBox3.Text)
            .Parameters.AddWithValue("@brand_id", ComboBox4.Text)
            .Parameters.AddWithValue("@size_id", ComboBox5.Text)
            .Parameters.AddWithValue("@type_id", ComboBox6.Text)
            .Parameters.AddWithValue("@remarks", RichTextBox1.Text)
            .Parameters.AddWithValue("@date", datetimepicker1)
            .ExecuteNonQuery()

        End With

            MsgBox(" SIze Successfully added")
        Catch ex As Exception
            MsgBox(ex.Message.ToString)



        End Try

无法添加或更新子行外键约束失败,这是我的错误

这是我的表格结构* *********** * 8


-

- 表tbl_brand表结构

CREATE TABLE IF NOT EXISTS tbl_brandbrand_id int(11)NOT NULL AUTO_INCREMENT, brand_name varchar(200)NOT NULL, brand_desc varchar(200)DEFAULT NULL,PRIMARY KEY( brand_id ))ENGINE = InnoDB DEFAULT CHARSET = latin1 AUTO_INCREMENT = 4;

-

- 转储表tbl_brand数据

INSERT INTO tbl_brandbrand_idbrand_namebrand_desc )VALUES(1,'Nike','Nike Air'),(2,'Crocs','A级'),(3,'SafeGuard','SafeGuard');


-

- 表tbl_category表结构

CREATE TABLE IF NOT EXISTS tbl_categorycat_id int(11)NOT NULL AUTO_INCREMENT, cat_name varchar(200)NOT NULL, cat_desc varchar(200)NOT NULL,PRIMARY KEY( cat_id ))ENGINE = InnoDB DEFAULT CHARSET = latin1 AUTO_INCREMENT = 3;

-

- 转储表tbl_category数据

INSERT INTO tbl_categorycat_idcat_namecat_desc )VALUES(1,'Bath Soap','Bath Soap'),(2,'Detergent','Detergent');


-

- 表tbl_dept表结构

CREATE TABLE IF NOT EXISTS tbl_deptdept_id int(11)NOT NULL AUTO_INCREMENT, dept_name varchar(200)NOT NULL, dept_desc varchar(200)NOT NULL,PRIMARY KEY( dept_id ))ENGINE = InnoDB DEFAULT CHARSET = latin1 AUTO_INCREMENT = 3;

-

- 转储表tbl_dept数据

INSERT INTO tbl_deptdept_iddept_namedept_desc )VALUES(1,'Shoes','Shoes'),(2,'Soap','Soap');


-

- 表tbl_product表结构

CREATE TABLE IF NOT EXISTS tbl_productprod_id int(11)NOT NULL AUTO_INCREMENT, prod_name varchar(200)NOT NULL, prod_desc varchar(200)DEFAULT NULL, cat_id int(11)NOT NULL, dept_id int(11)NOT NULL, brand_id int (11)NOT NULL, type_id int(11)NOT NULL, uom_id int(11)NOT NULL, size_id int(11)NOT NULL, date datetime NOT NULL, remarks varchar(200)DEFAULT NULL, uom_num int(60)DEFAULT NULL ,PRIMARY KEY( prod_id ),KEY tbl_product_ibfk_9type_id ),KEY tbl_product_ibfk_10uom_id ),KEY tbl_product_ibfk_11size_id ),KEY tbl_product_ibfk_12dept_id ),KEY tbl_product_ibfk_13cat_id ),KEY tbl_product_ibfk_14brand_id )); ENGINE = InnoDB DEFAULT CHARSET = latin1 AUTO_INCREMENT = 3;


-

- 表tbl_size表结构

CREATE TABLE IF NOT EXISTS tbl_sizesize_id int(11)NOT NULL AUTO_INCREMENT, size_name varchar(100)NOT NULL,PRIMARY KEY( size_id ))ENGINE = InnoDB DEFAULT CHARSET = latin1 AUTO_INCREMENT = 4;

-

- 为表tbl_size转储数据

INSERT INTO tbl_sizesize_idsize_name )VALUES(1,'Small(S)'),(2,'Medium(M)'),(3,'Large(L)');


-

- 表tbl_type表结构

CREATE TABLE IF NOT EXISTS tbl_typetype_id int(11)NOT NULL AUTO_INCREMENT, type_name varchar(200)NOT NULL,PRIMARY KEY( type_id ))ENGINE = InnoDB DEFAULT CHARSET = latin1 AUTO_INCREMENT = 9;

-

- 为表tbl_type转储数据

INSERT INTO tbl_typetype_idtype_name )VALUES(1,'BaskertBall Shoes'),(2,'Jersey Shorts'),(3,'跑鞋'),(8,'Bath Soap');


-

- 表tbl_uom表结构

CREATE TABLE IF NOT EXISTS tbl_uomuom_id int(11)NOT NULL AUTO_INCREMENT, uom_name varchar(200)NOT NULL,PRIMARY KEY( uom_id ))ENGINE = InnoDB DEFAULT CHARSET = latin1 AUTO_INCREMENT = 8;

-

- 为表tbl_uom转储数据

INSERT INTO tbl_uomuom_iduom_name )VALUES(1,'kilogram(kg)'),(2,'Gram(g)'),(3,'Milligram(Mg)'),(4''Liter(L) '),(5,'Milliliters(ml)'),(6''Pieces(pcs)'),(7,'foot(ft)');


-

- 表tbl_user表结构

CREATE TABLE IF NOT EXISTS tbl_useruser_id int(11)NOT NULL AUTO_INCREMENT, user_code varchar(200)DEFAULT NULL, user_password varchar(200)DEFAULT NULL, user_name varchar(200)DEFAULT NULL, user_level int(1)DEFAULT NULL, datetime datetime NOT NULL, com_code varchar(11)NOT NULL,PRIMARY KEY( user_id ),UNIQUE KEY user_codeuser_code ))ENGINE = InnoDB DEFAULT CHARSET = latin1 AUTO_INCREMENT = 10;

-

- 为表tbl_user转储数据

INSERT INTO tbl_useruser_iduser_codeuser_passworduser_nameuser_leveldatetimecom_code )VALUES(1,'1024','1024','Vincent Dematera',3,'2012-01-10 18:18:33', '001'),(7,'14','14','Megan Bueno',1,'2012-10-30 21:56:14','002'),(8,'13','13 ','Anonymous',1,'2012-10-20 21:51:00','002'),(9,'9','9','boom',1,'0000-00-00 00 :00:00','003');

-

- 转储表的约束

-

- 表tbl_product约束

ALTER TABLE tbl_product

ADD CONSTRAINT tbl_product_ibfk_9外键( type_id )参考tbl_typetype_id ),添加约束tbl_product_ibfk_10外键( uom_id )参考tbl_uomuom_id ),添加约束tbl_product_ibfk_11外键( size_id )参考tbl_sizesize_id ),添加约束tbl_product_ibfk_12外键( dept_id )参考tbl_deptdept_id ),ADD CONSTRAINT tbl_product_ibfk_13 FOREIGN KEY( cat_id )REFERENCES tbl_categorycat_id ),ADD CONSTRAINT tbl_product_ibfk_14 FOREIGN KEY( brand_id )REFERENCES tbl_brandbrand_id ),ADD CONSTRAINT tbl_product_ibfk_2 FOREIGN KEY( type_id )REFERENCES tbl_typetype_id ),ADD CONSTRAINT tbl_product_ibfk_3外键( uom_id )参考tbl_uomuom_id ),添加约束tbl_product_ibfk_4外键( size_id )参考tbl_sizesize_id ),添加约束tbl_product_ibfk_5外键( dept_id )参考tbl_deptdept_id ),添加约束tbl_product_ibfk_6外键( cat_id )参考tbl_categorycat_id cat_id ),ADD CONSTRAINT tbl_product_ibfk_7 FOREIGN KEY( brand_id )REFERENCES tbl_brandbrand_id ); SET FOREIGN_KEY_CHECKS = 1;


您的桌面产品有大量的外键约束。 例如,您输入表格产品的任何“size_id”都与表格大小中的“size_id”相对应。

您的vb.net程序告诉您的是,您输入到表产品中的某个id值不存在于其对应的表中。 我最好的猜测是你的id值之一(即size_iduom_id等)是空的,空白的或零。

您有多种选择 - 您可以:

  • 如果希望使用SET foreign_key_checks = 0;则关闭表的外键SET foreign_key_checks = 0;
  • 你可以完全删除外键
  • 您可以修复代码,以确保在尝试插入值之前您仍然遵守外键
  • 链接地址: http://www.djcxy.com/p/59689.html

    上一篇: Vb.net error for query and database

    下一篇: Add Foreign Key to existing table