I have a database made up of 2 (more, actually, but only 2 im working with) tables.
The Material
table consists solely of the material-number and the description
- DPMatNr
- DPBezeichnung
The Eigenschaften
table is there to hold the properties of the materials.
It uses the columns:
- EigenschaftenBezeichnerID
- Wert (value)
My problem is: each entry in the Material
table needs to have multiple entries in the Eigenschaften
table.
For example:
"Material":
DPMatNr = 001,
DPBezeichnung = "Description"
"Eigenschaften":
EigenschaftenBezeichnerID = 1,
Wert = "A4"
EigenschaftenBezeichnerID = 3,
Wert = "80" and so on.
My code currently looks like this:
public static void InsertData(string connectionstring, string matnummer, string bezeichnung, string format, string grammatur, string gewicht, string eform, string kuvertierung, string altkuvert)
{
string query = @"Insert INTO dbo.Material (DPMatNr, DPBezeichnung)
VALUES (@matnummer, @bezeichnung)";
string query2 = @"Insert INTO dbo.Eigenschaften
(EigenschaftenBezeichnerID, Wert)
VALUES (@1, @format, @2, @grammatur, @3, @gewicht,
@4, @eform, @5, @kuvertierung,
@6, @altkuvert)";
using (SqlConnection cn = new SqlConnection(connectionstring))
using (SqlCommand cmd = new SqlCommand(query, cn))
{
cmd.Parameters.Add("@matnummer", SqlDbType.VarChar, 50).Value = matnummer;
cmd.Parameters.Add("@bezeichnung", SqlDbType.VarChar, 50).Value = bezeichnung;
cn.Open();
cmd.ExecuteNonQuery();
using (SqlCommand cmd2 = new SqlCommand(query2, cn))
{
cmd2.Parameters.Add("@1", SqlDbType.Int).Value = 1;
cmd2.Parameters.Add("@format", SqlDbType.VarChar, 50).Value = format;
cmd2.Parameters.Add("@2", SqlDbType.Int).Value = 2;
cmd2.Parameters.Add("@grammatur", SqlDbType.VarChar, 50).Value = grammatur;
cmd2.Parameters.Add("@3", SqlDbType.Int).Value = 3;
cmd2.Parameters.Add("@gewicht", SqlDbType.VarChar, 50).Value = gewicht;
cmd2.Parameters.Add("@4", SqlDbType.Int).Value = 4;
cmd2.Parameters.Add("@eform", SqlDbType.VarChar, 50).Value = eform;
cmd2.Parameters.Add("@5", SqlDbType.Int).Value = 5;
cmd2.Parameters.Add("@kuvertierung", SqlDbType.VarChar, 50).Value = kuvertierung;
cmd2.Parameters.Add("@6", SqlDbType.Int).Value = 6;
cmd2.Parameters.Add("@altkuvert", SqlDbType.VarChar, 50).Value = altkuvert;
cmd2.ExecuteNonQuery();
}
cn.Close();
}
}
Now I currently get an error that says:
System.Data.SqlClient.SqlException: Cannot insert duplicate key row in object 'dbo.Material' with unique index 'IX_MatNrUnique'
What am I doing wrong?