插入外键

时间:2014-11-07 21:52:04

标签: sql sql-server insert key

您能否帮助我将Addresses表中的外键添加到Company表,谢谢。

myCommand2.Parametres.AddWithValue(@AddressId", ????????);"

protected void RegisterUser_CreatedUser(object sender, EventArgs e)
{
    MembershipUser newUser = Membership.GetUser(RegisterUser.UserName);
    Guid newUserId = (Guid)newUser.ProviderUserKey;
    ...

    string connectionString = ConfigurationManager.ConnectionStrings["ApplicationServices"].ConnectionString;
    string insertSql1 = "INSERT INTO Addresses(Street, Number, City, Postcode) VALUES (@Street, @Number, @City, @Postcode)";

    using (SqlConnection myConnection1 = new SqlConnection(connectionString))
    {
        myConnection1.Open();
        SqlCommand myCommand1 = new SqlCommand(insertSql1, myConnection1);
        myCommand1.Parameters.AddWithValue("@Street", Street);
        myCommand1.Parameters.AddWithValue("@Number", Number);
        myCommand1.Parameters.AddWithValue("@City", City);
        myCommand1.Parameters.AddWithValue("@Postcode", Postcode);
        myCommand1.ExecuteNonQuery();
        myConnection1.Close();
    }

    string insertSql2 = "INSERT INTO Company (AddressId, UserId, CompanyName, IC, DIC, Web) VALUES (@AddressId, @UserId, @CompanyName, @IC, @DIC, @Web)";

    using (SqlConnection myConnection2 = new SqlConnection(connectionString))
    {
        myConnection2.Open();
        SqlCommand myCommand2 = new SqlCommand(insertSql2, myConnection2);
        myCommand2.Parametres.AddWithValue(@AddressId", ????????);
        myCommand2.Parameters.AddWithValue("@UserId", newUserId);
        myCommand2.Parameters.AddWithValue("@CompanyName", CompanyName);
        myCommand2.Parameters.AddWithValue("@IC", Ic);
        myCommand2.Parameters.AddWithValue("@DIC", Dic);
        myCommand2.Parameters.AddWithValue("@Web", Web);
        myCommand2.ExecuteNonQuery();
        myConnection2.Close();
    }
}

0 个答案:

没有答案