저장이나 수정시 for문 안에서 루프 돌면서 insert 하는경우 종종 있죠??
그럴경우 xml형태로 데이터를 전송하여 sp안에서 처리하게 하는건 어떨까요?
머 많은분들이 알고 계시겠지만 그래도~ ㅋㅋㅋ
우선 클릭 이벤트~
// 다중 입력 처리
private void Button1_Click(object sender, System.EventArgs e)
{
string sXML = ""; // xml 저장소
bool bResult = false; // 결과값
DataSet ds = new DataSet();
ds.Tables.Add(MakeTable()); // 테이블 생성
if(ds.Tables[0].Rows.Count > 0)
{
sXML = ds.GetXml();
bResult = AddMemo(sXML);
if(bResult)
{
// 처리 성공
}
else
{
// 처리 실패
}
}
else
{
// 테이블 생성 실패
}
}
DataTable 형식으로 자료를 만든다. xml로 쓰기 위함
private DataTable MakeTable()
{
char cSplit = ';';
string[] sReceiveID = null;
DataTable dt = new DataTable("MemoAdd");
dt.Columns.Add("SendID");
dt.Columns.Add("ReceiveID");
dt.Columns.Add("Memo");
dt.Columns.Add("DelState");
string sSendID = AuthUser.CurrentUserID;
string sMemo = txtMemo.Text.Trim().Replace("\r\n", "<br>");
string sDelState = txtDelState.Value.Trim();
// 수신자 아이디
if(txtReceiveID.Text.IndexOf(cSplit) > 0)
{
sReceiveID = txtReceiveID.Text.Split(cSplit);
}
else
{
sReceiveID = new string[1];
sReceiveID[0] = txtReceiveID.Text.Trim();
}
DataRow dr = null;
for(int i=0;i<sReceiveID.Length;i++)
{
dr = dt.NewRow();
dr["SendID"] = sSendID;
dr["ReceiveID"] = sReceiveID[i];
dr["Memo"] = sMemo;
dr["DelState"] = sDelState;
dt.Rows.Add(dr);
}
return dt;
}
sp 호출
public bool AddMemo(string sXML)
{
string sQuery = "USP_SM_MemoAdd_I"; // sp명
bool bResult = false; // 결과값
SqlCommand sqlCmd = null;
SqlParameterCollection sqlParamColl = null;
SqlParameter[] sqlParams = null;
try
{
sqlCmd = new SqlCommand();
sqlParamColl = sqlCmd.Parameters;
sqlParamColl.Add("@VarXML", sXML);
sqlParams = new SqlParameter[sqlParamColl.Count];
sqlParamColl.CopyTo(sqlParams, 0);
sqlParamColl.Clear();
SqlHelper.ExecuteNonQuery(CONNSTR_UTIL, CommandType.StoredProcedure, sQuery, sqlParams); // 실행
bResult = true;
}
catch(Exception ex)
{
throw ex;
}
finally
{
if (sqlCmd != null) sqlCmd.Dispose();
}
return bResult;
}
sp 내용
Create PROC USP_SM_MemoAdd_I
@VarXML NTEXT
AS
DECLARE @SendID varchar(50)
DECLARE @ReceiveID varchar(50)
DECLARE @Memo Varchar(7000)
DECLARE @DelState char(2)
DECLARE @HDoc INT
DECLARE @RootName VARCHAR(100)
SET NOCOUNT ON
BEGIN TRANSACTION
EXEC SP_XML_PREPAREDOCUMENT @HDoc OUTPUT, @VarXML
SET @RootName ='/' + (SELECT TOP 1 LocalName FROM OPENXML(@HDoc, '/') WHERE ID = 0) + '/'
SET @RootName = @RootName + (SELECT TOP 1 LocalName FROM OPENXML(@HDoc, '/') WHERE ParentID = 0)
DECLARE XML_CURSOR CURSOR FOR
SELECT SendID, ReceiveID, Memo, DelState
FROM OPENXML(@HDoc, @RootName, 1)
WITH (
SendID varchar(50) 'SendID',
ReceiveID varchar(50) 'ReceiveID',
Memo Varchar(7000) 'Memo',
DelState char(2) 'DelState'
)
OPEN XML_CURSOR
FETCH NEXT FROM XML_CURSOR INTO @SendID, @ReceiveID, @Memo, @DelState
WHILE @@FETCH_STATUS = 0
BEGIN
IF ( EXISTS(SELECT 1 FROM dbo.TB_SM_BOSUser where UserID = @SendID) and EXISTS(SELECT 1 FROM dbo.TB_SM_BOSUser where UserID = @ReceiveID) )
BEGIN
INSERT INTO
dbo.TB_SM_Memo (SendID, ReceiveID, Memo, DelState)
VALUES
(
@SendID,
@ReceiveID,
@Memo,
@DelState
)
END
IF @@ERROR <> 0
BEGIN
ROLLBACK TRANSACTION
CLOSE XML_CURSOR
DEALLOCATE XML_CURSOR
EXECUTE SP_XML_REMOVEDOCUMENT @HDoc -- OpenXML을 사용하고 나서, Document를 닫고, Handle을 제거한다.
SET NOCOUNT OFF
RAISERROR 13000 '……'
RETURN
END
FETCH NEXT FROM XML_CURSOR INTO @SendID, @ReceiveID, @Memo, @DelState
END
CLOSE XML_CURSOR
DEALLOCATE XML_CURSOR
EXECUTE SP_XML_REMOVEDOCUMENT @HDoc -- OpenXML을 사용하고 나서, Document를 닫고, Handle을 제거한다.
COMMIT TRANSACTION
SET NOCOUNT OFF
RETURN
GO