- Connection = ใช้กำหนด Object Connection Database
- CommandType ใช้กำหนดชื่อชนิด Command เช่น Text,StoreProcedure,TableDirect
- CommandText ใช้อ้างอิงถึงข้อความที่เป็นภาษา SQL
- Parameters ใช้กำหนดตัวแปร
- ExecuteScalar() ให้ Command ทำงาน คืนค่าเป็นค่าเดียว
- ExecuteReader() ให้ Command ทำงาน คืนค่าเป็นแถว
- ExcuteNoneQuery() ให้ Command ทำงาน เพื่อปรับปรุงข้อมูล หรือเปลี่ยนแปลงข้อมูล คืนค่าเป็นจำนวนแถวที่มีผล สำเร็จ
- ExcuteXMlReader() ให้ Command ทำงาน ซึ่ง คืนค่าเป็น XML
____________________________________________________________________________________________
1. Method ในการรับค่า เป็นคำสั่ง SQL แล้วก้ประมวณผล โดยรับค่า เป็น String
public void QuerySQL(String isSQL)
{
this.objConn.Open();
SqlTransaction trans = this.objConn.BeginTransaction(IsolationLevel.ReadUncommitted);
this.objCmd.Connection = this.objConn;
this.objCmd.Transaction = trans;
try
{
this.objCmd.CommandText = isSQL;
this.objCmd.ExecuteNonQuery();
trans.Commit();
}
catch (InvalidOperationException XcpInvOp)
{
System.Windows.Forms.MessageBox.Show(XcpInvOp.ToString());
}
catch (Exception e)
{
trans.Rollback();
System.Windows.Forms.MessageBox.Show(e.ToString());
}
finally
{
this.objConn.Close();
}
_____________________________________________________________________________________________
2. Method ในการรับค่าเป็น Command โดยมีคำสั่ง SQL ภายใน Command
2.1 Method ที่ Return ค่า SqlConnection
SqlConnection objConn;
public SqlConnection GetCon()
{
return objConn;
}
___________________________________________________
2.2 Method รับค่าเปลี่ยนแปลงเป็น Command สำหรับ เอาค่า มาใส่ตัวแปร @ ใน SQL
ConnectDatabase tpcon = new ConnectDatabase();
private SqlCommand CommandSQL_NPT(String isSQL)
{
SqlCommand CMD = new SqlCommand(isSQL, tpcon.GetCon()); // เรียกหาตัวแปล Con
CMD.Parameters.Add(new SqlParameter("@cmdDate", ตัวแปร));
return CMD;
}
___________________________________________________
2.3 Method ในการ Query Database
private void get_Query()
{
ConnectDatabase tpcon = new ConnectDatabase();
String isSQL = "คำสั่ง SQL @comDate ";
tpcon.QuerySQL2(CommandSQL_NPT(isSQL));
}
____________________________________________________
2.4 Method รับค่าเป็น Command
public void QuerySQL2(SqlCommand cmd)
{
this.objConn.Open();
SqlTransaction trans = this.objConn.BeginTransaction(IsolationLevel.ReadUncommitted);
this.objCmd = cmd;
this.objCmd.Connection = this.objConn;
this.objCmd.Transaction = trans;
try
{
this.objCmd.ExecuteNonQuery();
trans.Commit();
}
catch (InvalidOperationException XcpInvOp)
{
System.Windows.Forms.MessageBox.Show(XcpInvOp.ToString());
}
catch (Exception e)
{
trans.Rollback();
System.Windows.Forms.MessageBox.Show(e.ToString());
}
finally
{
this.objConn.Close();
}
}
2.4 Method รับค่าเป็น Command
public void QuerySQL2(SqlCommand cmd)
{
this.objConn.Open();
SqlTransaction trans = this.objConn.BeginTransaction(IsolationLevel.ReadUncommitted);
this.objCmd = cmd;
this.objCmd.Connection = this.objConn;
this.objCmd.Transaction = trans;
try
{
this.objCmd.ExecuteNonQuery();
trans.Commit();
}
catch (InvalidOperationException XcpInvOp)
{
System.Windows.Forms.MessageBox.Show(XcpInvOp.ToString());
}
catch (Exception e)
{
trans.Rollback();
System.Windows.Forms.MessageBox.Show(e.ToString());
}
finally
{
this.objConn.Close();
}
}
ไม่มีความคิดเห็น:
แสดงความคิดเห็น