Friday, July 6, 2012

Demo bulkcopy với ADO.NET

Trong phần này tôi demo khả năng sao chép nhanh dữ liệu hàng loạt với ADO.NET



[sourcecode language="CSharp" wraplines="false"]
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.Security.Principal;

namespace BulkCopy
{
public partial class Copy : Form
{
SqlConnection conn;
SqlConnection DesConn;
SqlBulkCopy bulk;
public Copy()
{
InitializeComponent();
conn = new SqlConnection("Server=.;database=Northwind;uid=sa;pwd=sa;");
DesConn = new SqlConnection("Server=.;database=Northwind;uid=sa;pwd=sa;");

conn.Open();
DesConn.Open();

bulk = new SqlBulkCopy(DesConn);
}

private void button1_Click(object sender, EventArgs e)
{
listBox1.Items.Insert(0, "BulkCopy started " + DateTime.Now.ToString("HH - MM - ss"));
// Perform an initial count on the destination table.
SqlCommand command = new SqlCommand("SELECT * FROM Items", conn);

SqlDataReader reader = command.ExecuteReader();

bulk.DestinationTableName = "NewItems";
bulk.WriteToServer(reader);

reader.Close();

listBox1.Items.Insert(0, "BulkCopy end " + DateTime.Now.ToString("HH - MM - ss"));
}

private void button2_Click(object sender, EventArgs e)
{
SqlCommand comm = new SqlCommand("Delete NewItems", DesConn);
comm.ExecuteNonQuery();
}

private void button3_Click(object sender, EventArgs e)
{
listBox1.Items.Insert(0, "Copy started " + DateTime.Now.ToString("HH - MM - ss"));

// Perform an initial count on the destination table.
SqlCommand command = new SqlCommand("SELECT * FROM Items", conn);

SqlDataReader reader = command.ExecuteReader();

SqlCommand comm = new SqlCommand("", DesConn);
while (reader.Read())
{
comm.CommandText = "Insert into NewItems values(" + reader.GetValue(0).ToString() + ",'" + reader.GetValue(1).ToString() + "')";
comm.ExecuteNonQuery();
}

reader.Close();

listBox1.Items.Insert(0, "Copy end " + DateTime.Now.ToString("HH - MM - ss"));
}

private void Copy_Load(object sender, EventArgs e)
{
WindowsIdentity user = System.Security.Principal.WindowsIdentity.GetCurrent();

this.Text = user.Name;

toolStripStatusLabel1.Text = user.Name;
}
}
}
[/sourcecode]

Transaction with ADO.NET - Quản lý phiên làm việc với ADO.NET

Transaction là một trong những thuật ngữ rất phổ biến đố với lập trình viên. Với SQLServer hay bấc kỳ một hệ quản trị csdl nào đều phải đảm bảo tính ACID, trong trường hợp thực thi hàng loạt các thao tác thay đổi đến csdl thì sẽ có những tình huống cần ràng buộc hoạt là tất cả hoàn thành hoạt là không tác vụ nào hoàn thành. Trong nội dung của bài này tôi giới thiệu với các bạn các thức cài đặc Transacton trên nền ADO.NET.

[caption id="attachment_668" align="aligncenter" width="300"]Transaction with ADO.NET Transaction with ADO.NET[/caption]

Tham khảo sources nguồn tại đây:

[sourcecode language="CSharp" wraplines="false"]
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace Transaction
{
public partial class Form1 : Form
{
SqlConnection conn;
public Form1()
{
InitializeComponent();

conn = new SqlConnection("Server=.;database=Northwind;uid=sa;pwd=sa;");
}

private void button1_Click(object sender, EventArgs e)
{
if (conn.State == ConnectionState.Closed)
conn.Open();

listBox1.Items.Insert(0, "Connected");
}

private void button2_Click(object sender, EventArgs e)
{
listBox1.Items.Insert(0, "Transaction started");
SqlTransaction tran = conn.BeginTransaction("AddItems");

SqlCommand comm = new SqlCommand("", conn);
try
{
comm.Transaction = tran;

string strSql = "";

for (int i = 0; i < 10000; i++)
{
strSql += "Insert into Items values(" + i + ",'Name" + i + "')";
}

comm.CommandText = strSql;

int count = comm.ExecuteNonQuery();

if (count < 10000)
{
comm.Transaction.Rollback("AddItems");
listBox1.Items.Insert(0, "Transaction rollback");
}
else
{
comm.Transaction.Commit();
listBox1.Items.Insert(0, "Transaction commit");
}
}
catch
{
comm.Transaction.Rollback("AddItems");
listBox1.Items.Insert(0, "Exception raise -> Transaction rollback");
}
}

private void button3_Click(object sender, EventArgs e)
{
SqlCommand comm = new SqlCommand("Delete items", conn);
comm.ExecuteNonQuery();
}
}
}
[/sourcecode]

Translate