2011-10-20 30 views
0

我使用VB.Net和MySQL作爲數據庫,我是一個新手。我在使用MySQL中的外鍵時遇到了問題。在MySQL中,我創建了inq表作爲其主表和inqcontact表。這裏是我的MySQL代碼:VB.Net和MySQL

CREATE TABLE inq(
    number INT NOT NULL AUTO_INCREMENT, 
    lastname VARCHAR(20), 
    firstname VARCHAR(20), 
    middlename VARCHAR(20), 
    PRIMARY KEY(number)); 

CREATE TABLE inqcontact(
    noinqcontact INT NOT NULL AUTO_INCREMENT, 
    mobile VARCHAR(20), 
    telephone VARCHAR(20), 
    emailadd VARCHAR(20), 
    number INT, 
    PRIMARY KEY(noinqcontact), 
    FOREIGN KEY(number) REFERENCES inq(number)); 

,這裏是我的VB.Net代碼:

CommInq1 = New MySqlCommand("INSERT INTO inq VALUES (number,'" & txtLastName.Text & "','" & txtFirstName.Text & "','" & txtMiddleName.Text & "')", ConnInq) 
     ConnInq.Open() 
     CommInq1.ExecuteNonQuery() 

     CommInq2 = New MySqlCommand("INSERT INTO inqcontact VALUES (noinqcontact,'" & txtMobileNo.Text & "','" & txtTelephoneNo.Text & "','" & txtEmailAdd.Text & "',number)", ConnInq) 
     CommInq2.ExecuteNonQuery() 
     ConnInq.Close() 

     MessageBox.Show("Saved!", "") 

我VB.Net代碼返回NULL值在inqcontactnumber外鍵。我的意思是,在inq表中,number字段自動遞增,所以沒有問題。但在inqcontact表中,作爲外鍵的number字段是NULL值。你能告訴我我提供的代碼有什麼問題嗎?我想,錯誤是從我的VB.Net插入數據。

回答

2

試試這個,

CommInq1 = New MySqlCommand("INSERT INTO inq (lastname,firstname,middlename) 
       VALUES (@lastname,@firstname,@middlename)",ConnInq) 

CommInq1.Parameters.Add("@lastname",MySql.Data.MySqlClient.MySqlDbType.VarChar,20).Value=txtLastName.Text 
CommInq1.Parameters.Add("@firstname",MySql.Data.MySqlClient.MySqlDbType.VarChar,20).Value=txtFirstName.Text 
CommInq1.Parameters.Add("@middlename",MySql.Data.MySqlClient.MySqlDbType.VarChar,20).Value=txtMiddleName.Text 

ConnInq.Open() 
CommInq1.ExecuteNonQuery() 

CommInq2 = New MySqlCommand("INSERT INTO inqcontact (mobile,telephone,emailadd,number) 
       VALUES (@mobile,@telephone,@emailadd,@number)",ConnInq) 

CommInq2.Parameters.Add("@mobile",MySql.Data.MySqlClient.MySqlDbType.VarChar,20).Value=txtMobileNo.Text 
CommInq2.Parameters.Add("@telephone",MySql.Data.MySqlClient.MySqlDbType.VarChar,20).Value=txtTelephoneNo.Text 
CommInq2.Parameters.Add("@emailadde",MySql.Data.MySqlClient.MySqlDbType.VarChar,20).Value=txtEmailAdd.Text 
CommInq2.Parameters.Add("@number",MySql.Data.MySqlClient.MySqlDbType.Int32) 
     .Value=CommInq1.LastInsertedId 
      ' or use select last_insert_id() to get the last id 

CommInq2.ExecuteNonQuery() 
ConnInq.Close() 
+0

所有字段都是** NULL **代碼。我嘗試使用_MySQL Command Line Client_插入我之前使用的代碼: 'INSERT INTO INQ VALUES(number,'SomeText','SomeText','SomeText');' 並且填充這四個字段。但是這個: 'INSERT INTO inqcontact VALUES(noinqcontact,'SomeText','Sometext',SomeText',number);' 第一到第四個字段都可以,但是最後一個(**數字**),它是外鍵,不是... 所以,我的VB.Net語法沒有錯。我認爲這是我的MySQL語法。 – aer

+0

使用PDO +1。 – Johan

2

我一般使用SQL服務器,而不是我的SQL,但我相信,你應該能夠做到基本如下:

CommInq1 = New MySqlCommand("INSERT INTO inq (lastname,firstname,middlename) 
       VALUES (@lastname,@firstname,@middlename); select last_insert_id()" ,ConnInq) 
CommInq1.Parameters.Add("@lastname",MySql.Data.MySqlClient.MySqlDbType.VarChar,20).Value=txtLastName.Text 
CommInq1.Parameters.Add("@firstname",MySql.Data.MySqlClient.MySqlDbType.VarChar,20).Value=txtFirstName.Text 
CommInq1.Parameters.Add("@middlename",MySql.Data.MySqlClient.MySqlDbType.VarChar,20).Value=txtMiddleName.Text 

ConnInq.Open() 
Dim id as Integer = cint(CommInq1.ExecuteScalar()) 

然後與繼續第二個查詢使用id作爲插入項目的鍵的值。我從上面的答案中複製了代碼,因爲它更合適(防止注入和所有的安全)和習慣,但是;接下來是第二個查詢以獲取剛剛插入的id,並且還可以處理您的查詢。