|
|
|
insert หลายๆ record ครับ C# .net พอดีผมอยากจะได้ insert ข้อมูลหลายๆ record น่ะครับ โดยเพียงใช้ array เก็บ pamary key แล้ว insert แบบใช้ for วนน่ะครับ |
|
|
|
|
|
|
|
เป็น อาเรย์ 2 มิติเก็บชื่อพารามิเตอร์และvalue และไปเรียกใช้ store ในการ insert
public bool insertMain(DataTable dtMain)
{
bool result = false;
try
{
string[,] arrParameter1 = new string[10, 2];
string[,] arrParameter2 = new string[16, 2];
foreach (DataRow drHD in dtMain.Rows)
{
arrParameter1[0, 0] = "InvoiceNo"; arrParameter1[0, 1] = drHD["InvoiceNo"].ToString();
arrParameter1[1, 0] = "TaxID"; arrParameter1[1, 1] = drHD["TaxID"].ToString();
arrParameter1[2, 0] = "Branch"; arrParameter1[2, 1] = drHD["Branch"].ToString();
arrParameter1[3, 0] = "RefNo"; arrParameter1[3, 1] = drHD["RefNo"].ToString();
arrParameter1[4, 0] = "Vessel"; arrParameter1[4, 1] = drHD["Vessel"].ToString();
arrParameter1[5, 0] = "Voyage"; arrParameter1[5, 1] = drHD["Voyage"].ToString();
arrParameter1[6, 0] = "ArrivalDate"; arrParameter1[6, 1] = drHD["ArrivalDate"].ToString();
arrParameter1[7, 0] = "LoadCountry"; arrParameter1[7, 1] = drHD["LoadCountry"].ToString();
arrParameter1[8, 0] = "CutOption"; arrParameter1[8, 1] = drHD["CutOption"].ToString();
arrParameter1[9, 0] = "UserID"; arrParameter1[9, 1] = drHD["UserID"].ToString();
}
result = ExecuteStoreHeader("sp_InsertImpCertHeader", "sp_UpdateRunnoRefNo", arrParameter1, "RN");
}
catch (Exception ex)
{
throw ex;
}
return result;
}
การinsert ส่งอาเรย์กับชื่อสโต
protected Boolean ExecuteStoreHeader(string strStoreName1, string strStoreName2, string[,] arrPara1, string prefix)
{
SqlTransaction tran_1;
bool booResult = false;
this.OpenConnection();
tran_1 = this._SQLCon.BeginTransaction(IsolationLevel.ReadCommitted);
#region + Header
SqlCommand commandHD = new SqlCommand();
commandHD.Connection = this._SQLCon;
commandHD.Transaction = tran_1;
commandHD.CommandType = CommandType.StoredProcedure;
commandHD.CommandText = strStoreName1;
if ((arrPara1 != null) || (arrPara1.Length != 0))
{
for (int i = 0; i < arrPara1.Length / 2; i++)
{
string a = arrPara1[i, 0].ToString();
string b = arrPara1[i, 1].ToString();
commandHD.Parameters.AddWithValue("@" + a, b);
}
}
commandHD.ExecuteNonQuery();//Execute Header.
#endregion
tran_1.Commit();
if (_Ex != null)
{
booResult = false;
}
else
{
booResult = true;
}
return booResult;
}
catch (Exception ex)
{
tran_1.Rollback();
throw (ex);
}
finally
{
this.CloseConnection();
}
คร่าวๆนะครับพอเป็นแนวทาง
|
|
|
|
|
Date :
2009-08-11 10:56:14 |
By :
besttooo |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Load balance : Server 05
|