新しい会社をデータベースに挿入するこの方法がありましたが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