You are on page 1of 4

Create form

X l s kin click ca nt Load


private void btnCreate_Click(object sender, EventArgs e)
{
adp = new SqlDataAdapter("Select FirstName, LastName, Orders.* from
orders, employees where Employees.EmployeeID=Orders.EmployeeID", conn);
adp.UpdateCommand = UCommand;
ds = new DataSet();
adp.Fill(ds, "Orders");
dataGridView1.DataSource = ds.Tables[0];
}

X l cho s kin cho nut nhn Update


private void btnUpdate_Click(object sender, EventArgs e)
{
adp.RowUpdated += new SqlRowUpdatedEventHandler(adp_RowUpdated);
adp.Update(ds.Tables[0]);
}
void adp_RowUpdated(object sender, SqlRowUpdatedEventArgs e)
{
MessageBox.Show(e.Command.Parameters["@EmployeeID"].Value.ToString());
}

Khi cp nht ta thy trn li c d liu ca 02 bng nhng khi cp nht ta ch cho php cp nht d liu
bng Employees
-

Hiu chnh code ca s kin khi ti form nh sau:

SqlCommand UCommand = new SqlCommand();


SqlConnection conn;
SqlDataAdapter adp;
DataSet ds;
public Form1()
{
InitializeComponent();
conn = new SqlConnection();
conn.ConnectionString = "Server=ntdan\\sql2005;Integrated
Security=SSPI;Initial Catalog=northwind";
conn.Open();
UCommand.Connection = conn;
UCommand.CommandText = "Update Employees set
FirstName=@FirstName, LastName=@LastName where EmployeeID=@EmployeeID";
SqlParameter FName = new SqlParameter();
FName.ParameterName = "@FirstName";
FName.SqlDbType = SqlDbType.VarChar;
FName.SourceColumn = "FirstName";
FName.Direction = ParameterDirection.Input;
SqlParameter LName = new SqlParameter();
LName.ParameterName = "@LastName";
LName.SqlDbType = SqlDbType.VarChar;
LName.SourceColumn = "LastName";
LName.Direction = ParameterDirection.Input;
SqlParameter EmpID = new SqlParameter();
EmpID.ParameterName = "@EmployeeID";
EmpID.SqlDbType = SqlDbType.Int;
EmpID.SourceColumn = "EmployeeID";
EmpID.Direction = ParameterDirection.InputOutput;
UCommand.Parameters.Add(FName);
UCommand.Parameters.Add(LName);
UCommand.Parameters.Add(EmpID);
}

Chy form v kim tra kt qu.


Tng t hiu chnh c th cp nht thm ct birthdate bng Employees

XML read, write

private void btnWrite_Click(object sender, EventArgs e)


{
XmlTextWriter wr = new XmlTextWriter("Doc.xml",
Encoding.Unicode);
wr.Formatting = Formatting.Indented;
wr.Indentation = 3;
wr.WriteStartDocument();
wr.WriteStartElement("Root");
wr.WriteStartElement("Item");
wr.WriteStartElement("ID");
wr.WriteString("01");
wr.WriteEndElement();
wr.WriteStartElement("Name");
wr.WriteString("Apple");
wr.WriteEndElement();
wr.WriteEndElement();
wr.WriteEndElement();
wr.WriteEndDocument();
wr.Flush();
wr.Close();
}
private void btnRead_Click(object sender, EventArgs e)
{
XmlTextReader rd = new XmlTextReader("Doc.xml");
while (rd.Read())
{
if (rd.MoveToContent() == XmlNodeType.Element && rd.Name ==
"Name")
{
MessageBox.Show(rd.ReadString());
}
}
rd.Close();
}
private void btnDom_Click(object sender, EventArgs e)
{

XmlDocument doc = new XmlDocument();


doc.Load("Doc.xml");
MessageBox.Show( doc.DocumentElement.Name);
foreach (XmlNode item in doc.ChildNodes)
{
}
doc.Save("Doc.xml");
}

You might also like