Vous êtes sur la page 1sur 6

Advanced winform - 2012

Demo for transaction with SqlDataProvider

using
using
using
using
using
using
using
using
using

System;
System.Collections.Generic;
System.ComponentModel;
System.Data;
System.Drawing;
System.Linq;
System.Text;
System.Windows.Forms;
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)
{

CUSC

Page 1

Advanced winform - 2012


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 < 10)
{
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();
}
}
}

Demo BulkCopy

CUSC

Page 2

Advanced winform - 2012

using
using
using
using
using
using
using
using
using
using

System;
System.Collections.Generic;
System.ComponentModel;
System.Data;
System.Drawing;
System.Linq;
System.Text;
System.Windows.Forms;
System.Data.SqlClient;
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();

CUSC

Page 3

Advanced winform - 2012


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;
}
}
}

Demo for mail sending

CUSC

Page 4

Advanced winform - 2012

using
using
using
using
using
using
using
using
using
using

System;
System.Collections.Generic;
System.ComponentModel;
System.Data;
System.Drawing;
System.Linq;
System.Text;
System.Windows.Forms;
System.Net.Mail;
System.IO;

namespace SendMail
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
this.Text = "mail sending .....";
SmtpClient mail = new SmtpClient("smtp.gmail.com", 587);
mail.EnableSsl = true;
System.Net.NetworkCredential cert = new
System.Net.NetworkCredential("ngotuongdan04", "ngotuongdan");
mail.Credentials = cert;
mail.SendCompleted += new
SendCompletedEventHandler(mail_SendCompleted);
MailMessage message = new MailMessage("ngotuongdan04@gmail.com",
"ntdan@ctu.edu.vn");
message.Sender = new MailAddress("ngotuongdan04@gmail.com");
message.Subject = "Feedback";
message.Body = "This exam is very easily";
if (File.Exists(textBox1.Text))
message.Attachments.Add(new Attachment(textBox1.Text));
mail.SendAsync(message, this);
}
void mail_SendCompleted(object sender, AsyncCompletedEventArgs e)
{
if (e.Error == null)
MessageBox.Show("Send completed");

CUSC

Page 5

Advanced winform - 2012


else
MessageBox.Show(e.Error.ToString());
this.Text = "mail sent";
}
private void textBox1_Click(object sender, EventArgs e)
{
OpenFileDialog op = new OpenFileDialog();
op.InitialDirectory = "d:\\";
if (op.ShowDialog() == DialogResult.OK)
{
textBox1.Text = op.FileName;
}
}
}
}

CUSC

Page 6

Vous aimerez peut-être aussi