2

新しい会社をデータベースに挿入するこの方法がありましたがList<Contacts>、会社の複数の連絡先を挿入できるようにしたいので問題があります。誰かが私を助けることができますか?

ここに画像の説明を入力

public static bool AddNewCompany(Company company,List<Contacts> contact , Location local)
{
    // get a configured DbCommand object
    DbCommand comm = GenericDataAccess.CreateCommand();

    // Set the stored procedure name 
    comm.CommandText = "AddNewCompany";

    //create new parameter @CompanyName
    DbParameter param = comm.CreateParameter();
    param.ParameterName = "@CompanyName";
    param.Value = company.CompanyName;
    param.DbType = DbType.StringFixedLength;
    comm.Parameters.Add(param);

    //create new parameter @CompanyDetail 
    param = comm.CreateParameter();
    param.ParameterName = "@CompanyDetail";
    param.Value = company.CompanyDetail;
    param.DbType = DbType.StringFixedLength;
    comm.Parameters.Add(param);

    //create new parameter @ModifiedDate
    param = comm.CreateParameter();
    param.ParameterName = "@ModifiedDate";
    param.Value = DateTime.Now;
    param.DbType = DbType.DateTime;
    comm.Parameters.Add(param);

    //Company Info
    foreach (var c in contact)
    {
        //create new parameter @LabelContactTypeID
        param = comm.CreateParameter();
        param.ParameterName = "@LabelContactTypeID";
        param.Value = c.LabelContactTypeID;
        param.DbType = DbType.StringFixedLength;
        comm.Parameters.Add(param);

        //create new parameter @ContactDetails
        param = comm.CreateParameter();
        param.ParameterName = "@ContactDetails";
        param.Value = c.ContactDetail;
        param.DbType = DbType.StringFixedLength;
        comm.Parameters.Add(param);

        //create new parameter @Status
        param = comm.CreateParameter();
        param.ParameterName = "@Status";
        param.Value = c.Status;
        param.DbType = DbType.StringFixedLength;
        comm.Parameters.Add(param);

        //create new parameter @Notes
        param = comm.CreateParameter();
        param.ParameterName = "@Notes";
        param.Value = c.Notes;
        param.DbType = DbType.StringFixedLength;
        comm.Parameters.Add(param);
    }




    //Company Info
    //create new parameter @Address
    param = comm.CreateParameter();
    param.ParameterName = "@Address";
    param.Value = local.Address;
    param.DbType = DbType.StringFixedLength;
    comm.Parameters.Add(param);

    //create new parameter @City
    param = comm.CreateParameter();
    param.ParameterName = "@City";
    param.Value = local.City;
    param.DbType = DbType.StringFixedLength;
    comm.Parameters.Add(param);

    //create new parameter @Province
    param = comm.CreateParameter();
    param.ParameterName = "@Province";
    param.Value = local.Province;
    param.DbType = DbType.StringFixedLength;
    comm.Parameters.Add(param);

    //create new parameter @PostalCode
    param = comm.CreateParameter();
    param.ParameterName = "@PostalCode";
    param.Value = local.PostalCode;
    param.DbType = DbType.StringFixedLength;
    comm.Parameters.Add(param);

    //create new parameter @Note
    param = comm.CreateParameter();
    param.ParameterName = "@Note";
    param.Value = local.Note;
    param.DbType = DbType.StringFixedLength;
    comm.Parameters.Add(param);

    //create new parameter @ModifiedDateLocation  
    param = comm.CreateParameter();
    param.ParameterName = "@ModifiedDateLocation";
    param.Value = DateTime.Now;
    param.DbType = DbType.StringFixedLength;
    comm.Parameters.Add(param);


    try
    {
        return (GenericDataAccess.ExecuteNonQuery(comm) != -1);

    }
    catch
    {
        return false;
    }
}

SQL Server ストアド プロシージャ:

ALTER PROCEDURE [dbo].[AddNewCompany]
    @CompanyName nvarchar(50),
    @CompanyDetail nvarchar(max),
    @ModifiedDate datetime,
    --ContactInfo
    @LabelContactTypeID  int,
    @ContactDetails nvarchar(MAX),
    @Status bit,
    @Notes nvarchar(MAX),
    -- Company Location 
    @Address nvarchar(max),
    @City nvarchar(50),
    @Province nvarchar(50),
    @PostalCode nvarchar(10),
    @Note nvarchar(max),
    @ModifiedDateLocation datetime
AS
BEGIN
    SET NOCOUNT ON;

    INSERT INTO [TaskManagementSystem_DB].[dbo].[Company] ([companyName],[companyDetail], [modifiedDate])
     VALUES (@CompanyName, @CompanyDetail, @ModifiedDate)

    DECLARE @CompanyID int 
    SET @CompanyID = SCOPE_IDENTITY();

    INSERT INTO [TaskManagementSystem_DB].[dbo].[Company_Contacts] ([companyID], [labelContactTypeID], [contactDetails], [status], [notes])
    VALUES (@CompanyID, @LabelContactTypeID, @ContactDetails, @Status, @Notes)

    INSERT INTO [TaskManagementSystem_DB].[dbo].[Location]([address], [city], [province], [postalCode], [note], [modifiedDate])
    VALUES (@Address, @City, @Province, @PostalCode, @Note, @ModifiedDateLocation)          

    DECLARE @LocationID  INT 
    SET @LocationID = SCOPE_IDENTITY(); 

    INSERT INTO [TaskManagementSystem_DB].[dbo].[Company_location]([companyID], [locationID])
    VALUES (@CompanyID, @LocationID)            
END
4

2 に答える 2

1

会社の部分を連絡先の上に移動します。

これをコンタクトループに入れます

GenericDataAccess.ExecuteNonQuery(comm);

障害をチェックするロジックを追加します。
続行するか、すぐに戻るかはあなた次第です。

public static bool AddNewCompany(Company company,List<Contacts> contact , Location local)
{
    // get a configured DbCommand object
    DbCommand comm = GenericDataAccess.CreateCommand();


    //Set the store Proc name 
    comm.CommandText = "AddNewCompany";



    //create new parameter @CompanyName
    DbParameter param = comm.CreateParameter();
    param.ParameterName = "@CompanyName";
    param.Value = company.CompanyName;
    param.DbType = DbType.StringFixedLength;
    comm.Parameters.Add(param);

    //create new parameter @CompanyDetail 
    param = comm.CreateParameter();
    param.ParameterName = "@CompanyDetail";
    param.Value = company.CompanyDetail;
    param.DbType = DbType.StringFixedLength;
    comm.Parameters.Add(param);

    //create new parameter @ModifiedDate
    param = comm.CreateParameter();
    param.ParameterName = "@ModifiedDate";
    param.Value = DateTime.Now;
    param.DbType = DbType.DateTime;
    comm.Parameters.Add(param);

    //Company Info
    //create new parameter @Address
    param = comm.CreateParameter();
    param.ParameterName = "@Address";
    param.Value = local.Address;
    param.DbType = DbType.StringFixedLength;
    comm.Parameters.Add(param);

    //create new parameter @City
    param = comm.CreateParameter();
    param.ParameterName = "@City";
    param.Value = local.City;
    param.DbType = DbType.StringFixedLength;
    comm.Parameters.Add(param);

    //create new parameter @Province
    param = comm.CreateParameter();
    param.ParameterName = "@Province";
    param.Value = local.Province;
    param.DbType = DbType.StringFixedLength;
    comm.Parameters.Add(param);

    //create new parameter @PostalCode
    param = comm.CreateParameter();
    param.ParameterName = "@PostalCode";
    param.Value = local.PostalCode;
    param.DbType = DbType.StringFixedLength;
    comm.Parameters.Add(param);

    //create new parameter @Note
    param = comm.CreateParameter();
    param.ParameterName = "@Note";
    param.Value = local.Note;
    param.DbType = DbType.StringFixedLength;
    comm.Parameters.Add(param);

    //create new parameter @ModifiedDateLocation  
    param = comm.CreateParameter();
    param.ParameterName = "@ModifiedDateLocation";
    param.Value = DateTime.Now;
    param.DbType = DbType.StringFixedLength;
    comm.Parameters.Add(param);

    // need to split up the SP to add the company here 
    // execute company SP here
    // and get back the companyID
    // can just clear out the parameters and reuse it
    comm.Parameters.Clear();

    //and you can just define the parameters once
    //create new parameter @LabelContactTypeID
    paramTypeID = comm.CreateParameter();
    paramTypeID.ParameterName = "@LabelContactTypeID";       
    paramTypeID.DbType = DbType.StringFixedLength;
    comm.Parameters.Add(paramTypeID);

    //Company Info
    foreach (var c in contact)
    {
        //create new parameter @LabelContactTypeID
        paramTypeID.Value = c.LabelContactTypeID;

        //create new parameter @ContactDetails
        param = comm.CreateParameter();
        param.ParameterName = "@ContactDetails";
        param.Value = c.ContactDetail;
        param.DbType = DbType.StringFixedLength;
        comm.Parameters.Add(param);

        //create new parameter @Status
        param = comm.CreateParameter();
        param.ParameterName = "@Status";
        param.Value = c.Status;
        param.DbType = DbType.StringFixedLength;
        comm.Parameters.Add(param);

        //create new parameter @Notes
        param = comm.CreateParameter();
        param.ParameterName = "@Notes";
        param.Value = c.Notes;
        param.DbType = DbType.StringFixedLength;
        comm.Parameters.Add(param);

        try
        {
             if (GenericDataAccess.ExecuteNonQuery(comm) == -1) return false;
             // this syntax is not what I am used to
             // i useally just comm.ExecuteNonQuer();
        }
        catch
        {
            return false;
        }
    }
    return true;    
}
于 2012-11-16T19:08:21.963 に答える
0

ストアド プロシージャは、会社の連絡先を 1 つだけ挿入しています。別の連絡先 (または複数の連絡先) を追加する場合は、データベースに最後に挿入された会社の CompanyID を使用して新しい連絡先を挿入する別の手順を記述します。

テーブルがわからず、結果を複製できないため、コードを書くことはできませんが、このコードから別のプロシージャを作成する必要があります

INSERT INTO [TaskManagementSystem_DB].[dbo].[Company_Contacts]
           ([companyID] 

           ,[labelContactTypeID]
           ,[contactDetails]
           ,[status]
           ,[notes])
     VALUES
           (@CompanyID 
           ,@LabelContactTypeID
           ,@ContactDetails
           ,@Status
           ,@Notes)

最初の部分では、リストから 1 つの連絡先を追加するだけです (リストのインデックス 0 にある最初の連絡先)。

挿入されたばかりの会社の CompanyID を読み取り、リスト内の他の連絡先ごとに、この ID を使用して新しいプロシージャを呼び出します。同じ companyID (1 対多の参照 ID) を使用して会社を参照する新しい連絡先を追加します。

編集次のようにする必要があります。

public static bool AddNewCompany(Company company,List<Contacts> contact , Location local)
{
    // get a configured DbCommand object
    DbCommand comm = GenericDataAccess.CreateCommand();
//Set the store Proc name 
comm.CommandText = "AddNewCompany";


//create new parameter @CompanyName
DbParameter param = comm.CreateParameter();
param.ParameterName = "@CompanyName";
param.Value = company.CompanyName;
param.DbType = DbType.StringFixedLength;
comm.Parameters.Add(param);

//create new parameter @CompanyDetail 
param = comm.CreateParameter();
param.ParameterName = "@CompanyDetail";
param.Value = company.CompanyDetail;
param.DbType = DbType.StringFixedLength;
comm.Parameters.Add(param);

//create new parameter @ModifiedDate
param = comm.CreateParameter();
param.ParameterName = "@ModifiedDate";
param.Value = DateTime.Now;
param.DbType = DbType.DateTime;
comm.Parameters.Add(param);

    //Company Info
//add only one contact
    /create new parameter @LabelContactTypeID
        param = comm.CreateParameter();
        param.ParameterName = "@LabelContactTypeID";
        param.Value = contact[0].LabelContactTypeID;
        param.DbType = DbType.StringFixedLength;
        comm.Parameters.Add(param);

    //create new parameter @ContactDetails
    param = comm.CreateParameter();
    param.ParameterName = "@ContactDetails";
    param.Value = contact[0].ContactDetail;
    param.DbType = DbType.StringFixedLength;
    comm.Parameters.Add(param);

    //create new parameter @Status
    param = comm.CreateParameter();
    param.ParameterName = "@Status";
    param.Value = contact[0].Status;
    param.DbType = DbType.StringFixedLength;
    comm.Parameters.Add(param);

    //create new parameter @Notes
    param = comm.CreateParameter();
    param.ParameterName = "@Notes";
    param.Value = contact[0].Notes;
    param.DbType = DbType.StringFixedLength;
    comm.Parameters.Add(param);


//Company Info
//create new parameter @Address
param = comm.CreateParameter();
param.ParameterName = "@Address";
param.Value = local.Address;
param.DbType = DbType.StringFixedLength;
comm.Parameters.Add(param);

//create new parameter @City
param = comm.CreateParameter();
param.ParameterName = "@City";
param.Value = local.City;
param.DbType = DbType.StringFixedLength;
comm.Parameters.Add(param);

//create new parameter @Province
param = comm.CreateParameter();
param.ParameterName = "@Province";
param.Value = local.Province;
param.DbType = DbType.StringFixedLength;
comm.Parameters.Add(param);

//create new parameter @PostalCode
param = comm.CreateParameter();
param.ParameterName = "@PostalCode";
param.Value = local.PostalCode;
param.DbType = DbType.StringFixedLength;
comm.Parameters.Add(param);

//create new parameter @Note
param = comm.CreateParameter();
param.ParameterName = "@Note";
param.Value = local.Note;
param.DbType = DbType.StringFixedLength;
comm.Parameters.Add(param);

//create new parameter @ModifiedDateLocation  
param = comm.CreateParameter();
param.ParameterName = "@ModifiedDateLocation";
param.Value = DateTime.Now;
param.DbType = DbType.StringFixedLength;
comm.Parameters.Add(param);


try
{
    return (GenericDataAccess.ExecuteNonQuery(comm) != -1);

}
catch
{
    return false;
}

}

ここに貼り付けた手順の一部を使用して新しい連絡先を追加する新しいメソッドを作成し、他のすべての連絡先を挿入します。このメソッドに companyID を渡します。

于 2012-11-16T19:04:52.860 に答える