これは、dbo.Empテーブルに挿入する最初のクラスです。
public void InsertInfo()
{
String KKStech = @"Data Source=USER-PC\SQLEXPRESS;Initial Catalog=KKSTech;Integrated Security=True";
SqlConnection conn = new SqlConnection(KKStech);
String insertstring = @"insert into Emp (EmpID, FirstName, LastName, MiddleName, Mob1, Mob2, Phone, Email1, Email2, EmpDesc)
values (@EmpID, @FirstName, @LastName, @MiddleName, @Mob1, @Mob2)";
SqlCommand cmd = new SqlCommand(insertstring, conn);
cmd.CommandText = insertstring;
cmd.CommandType = CommandType.Text;
try
{
conn.Open();
cmd.Parameters.AddWithValue("@EmpID", TextBox1.Text);
cmd.Parameters.AddWithValue("@FirstName", TextBox2.Text);
cmd.Parameters.AddWithValue("@LastName", TextBox3.Text);
cmd.Parameters.AddWithValue("@MiddleName", TextBox4.Text);
cmd.Parameters.AddWithValue("@Mob1", TextBox5.Text);
cmd.Parameters.AddWithValue("@Mob2", TextBox6.Text);
cmd.ExecuteNonQuery();
}
finally
{
conn.Close();
}
}
そしてこれは私がEmpIDがFKであるテーブルに挿入しているものです
public void Insertaddress()
{
String KKStech = @"Data Source=USER-PC\SQLEXPRESS;Initial Catalog=KKSTech;Integrated Security=True";
SqlConnection conn = new SqlConnection(KKStech);
String str = @"insert into Contact (Addressline1, Addressline2, CityID, EmpID)
values(@Addressline1, @Addressline2, @CityID, @EmpID)";
SqlCommand cmd = new SqlCommand(str, conn);
cmd.CommandText = str;
cmd.CommandType = CommandType.Text;
try
{
conn.Open();
cmd.Parameters.AddWithValue("@Addressline1", TextBox15.Text);
cmd.Parameters.AddWithValue("@Addressline2", TextBox17.Text);
cmd.Parameters.AddWithValue("@CityID", DropDownList2.SelectedValue);
cmd.Parameters.AddWithValue("@EmpID", TextBox1.Text);
cmd.ExecuteNonQuery();
}
catch (System.Data.SqlClient.SqlException ex)
{
string msg = "Insert Error:";
msg += ex.Message;
throw new Exception(msg);
}
finally
{
conn.Close();
}
}
それが私の問題でした。