引用來源
--
真是猛,對小的資料量 ;可以使用此法,但量大....就實在
C#
--
private Boolean dbUpdate()
{
string strSql = "select ProductsSpecID,ProductsSpec,ProductsSpecUnit,ProductsID from tbl_product_detail_master";
System.Data.DataTable dtUpdate = new System.Data.DataTable();
dtUpdate = this.dbconn(strSql);
dtUpdate.Rows.Clear();
System.Data.DataTable dtShow = new System.Data.DataTable();
//dtShow = (DataTable)this.bindSource.DataSource;
dtShow = (System.Data.DataTable)this.dgselect.DataSource;
int p1 = dtShow.Rows.Count;
// try
// {
for (int i = 0; i < dtShow.Rows.Count; i++)
{
DataRowState rowState=new DataRowState();
rowState=dtShow.Rows[i].RowState;
if (rowState==DataRowState.Added || rowState==DataRowState.Detached || rowState==DataRowState.Modified)
{
if (this.dgselect["Del", i].Value.ToString() == "1")
{
dtShow.Rows[i].Delete();
}
}
}
for (int i = 0; i < dtShow.Rows.Count; i++)
{
dtUpdate.ImportRow(dtShow.Rows[i]);
}
int num = dtUpdate.Rows.Count;
try
{
this.conn.Open();
trans = this.conn.BeginTransaction();
MySqlCommandBuilder CommandBuiler;
CommandBuiler = new MySqlCommandBuilder(this.adapter);
this.adapter.Update(dtUpdate);
trans.Commit();
this.conn.Close();
}
catch ( Exception ex)
{
MessageBox.Show(ex.Message.ToString());
trans.Rollback();
return false;
}
dtUpdate.AcceptChanges();
return true;
}
VB
--
Private Function dbUpdate() As Boolean
Dim strSql As String = "select ProductsSpecID,ProductsSpec,ProductsSpecUnit,ProductsID from tbl_product_detail_master"
Dim dtUpdate As System.Data.DataTable = New System.Data.DataTable
dtUpdate = Me.dbconn(strSql)
dtUpdate.Rows.Clear
Dim dtShow As System.Data.DataTable = New System.Data.DataTable
'dtShow = (DataTable)this.bindSource.DataSource;
dtShow = CType(Me.dgselect.DataSource,System.Data.DataTable)
Dim p1 As Integer = dtShow.Rows.Count
' try
' {
Dim i As Integer = 0
Do While (i < dtShow.Rows.Count)
Dim rowState As DataRowState = New DataRowState
rowState = dtShow.Rows(i).RowState
If ((rowState = DataRowState.Added) _
OrElse ((rowState = DataRowState.Detached) _
OrElse (rowState = DataRowState.Modified))) Then
If (Me.dgselect("Del", i).Value.ToString = "1") Then
dtShow.Rows(i).Delete
End If
End If
i = (i + 1)
Loop
Dim i As Integer = 0
Do While (i < dtShow.Rows.Count)
dtUpdate.ImportRow(dtShow.Rows(i))
i = (i + 1)
Loop
Dim num As Integer = dtUpdate.Rows.Count
Try
Me.conn.Open
trans = Me.conn.BeginTransaction
Dim CommandBuiler As MySqlCommandBuilder
CommandBuiler = New MySqlCommandBuilder(Me.adapter)
Me.adapter.Update(dtUpdate)
trans.Commit
Me.conn.Close
Catch ex As Exception
MessageBox.Show(ex.Message.ToString)
trans.Rollback
Return false
End Try
dtUpdate.AcceptChanges
Return true
End Function
沒有留言:
張貼留言