lab asp
<P><B>
<P></B></P></P>
<P><B>
</B></P>
<P>
<B>
<P>XÂY D*NG THU VI*N </P>
<P>DATA ACCESS </P></B>
<P> </P>
<P> </P>
<P> </P>
<P> </P>
<P> </P>
<P> </P>
<P> </P>
<P> </P>
<P> </P>
<P> </P>
<P> </P><B>
</B>
<P>Ð* Ng*c Cu*ng - ITDLU</P>
<P>Email: [email protected] </P>
<P>M</P>*c l*<B>c</B>
<P> </P>
<P>1 </P>
<P>Xây d*ng t*ng Data </P>
<P>Xây d*ng t*ng Data ........................................................................................................ 2</P>
<P> </P>
<P>Xây d*ng thu vi*n Data Access ...................................................................................... 3</P>
<P> </P>
<P>Vi*t code cho l*p Post (Post.cs) .................................................................................. 5</P>
<P> </P>
<P>Vi*t code cho l*p DataProvider (DataProvider.cs) ...................................................... 6</P>
<P> </P>
<P>Vi*t code cho l*p SqlDataProvider (SqlDataProvider.cs) ............................................ 7</P>
<P> </P>
<P>B* sung code vào l*p DataProvider ............................................................................ 9</P>
<P> </P>
<P>Test ............................................................................................................................... 10</P>
<P> </P>
<P>Luy*n t*p ...................................................................................................................... 15</P>
<P> </P>
<P>Làm ti*p các th* t*c còn l*i ........................................................................................... 16</P>
<P> </P>
<P>Th* t*c và các hàm b* sung Post_Count .................................................................. 17</P>
<P> </P>
<P>Th* t*c và các hàm b* sung Post_All ........................................................................ 17</P>
<P> </P>
<P>Th* t*c và hàm b* sung Post_Single ........................................................................ 18</P>
<P> </P>
<P>Th* t*c và hàm b* sung Post_Find ........................................................................... 19</P>
<P> </P>
<P>S* d*ng l*p SqlHelper d* t*i uu l*p SqlDataProvider ................................................. 20</P>
<P> </P>
<P>S* d*ng l*p CBO d* t* d*ng chuy*n d* li*u t* DataReader sang d*i tu*ng ............. 24</P>
<P> </P>
<P>Cách s* d*ng l*p CBO ............................................................................................. 25</P>
<P> </P>
<P>Xây d*ng thu vi*n Core * m*c t*ng quát ..................................................................... 27</P>
<P> </P>
<P>B* sung code cho l*p DataProvider .......................................................................... 29</P>
<P> </P>
<P>B* sung code cho l*p SqlDataProvider ..................................................................... 29</P>
<P> </P>
<P>B* sung ti*p code cho l*p DataProvider ................................................................... 31</P>
<P> </P>
<P>Xây d*ng thu vi*n DataAccess s* d*ng thu vi*n Core.dll ........................................... 32</P>
<P> </P>
<P> </P>
<P> </P>
<P> </P>
<P>[email protected] </P>
<B>
<P>Xây d*ng t*ng Data </P></B>
<P>2 </P>
<P>Xây d*ng t*ng Data </P>
<P>T*o co s* d* li*u d*t tên là <B>Sample</B>. Sau dó t*o m*t b*ng nhu hình và thi*t l*p giá tr*</P>
<P>t* tang cho c*t PostID </P>
<P>Quy t*c d*t tên th* t*c (Stored Procedure - SP) nhu sau: <B>X_Y</B> </P>
<P>Trong dó: </P>
<P>X: Tên c*a b*ng</P>
<P>Y: Ch*c nang mà th* t*c th*c hi*n </P>
<P>Ví d*:</P><B>
<P>Tên th* t*c Mô t* </P></B>
<P>Post_All L*y t*t c* record</P>
<P>Post_Single L*y <B>m*t</B> record theo PostID</P>
<P>Post_Find L*y nhi*u record theo di*u ki*n nào dó</P>
<P>Post_Add Thêm m*t record vào b*ng Post</P>
<P>Post_Update C*p nh*t m*t record theo PostID</P>
<P>Post_Delete Xóa m*t record theo PostID </P>
<P>[email protected] </P>
<P>3 </P>
<P>Xây d*ng thu vi*n Data Access </P>
<P>Post_Count Ð*m t*t c* record ho*c d*m theo di*u ki*n nào dó</P>
<P>Post_Paging L*y các record theo trang (phân trang)</P>
<P>... ... </P>
<P>T*o th* t*c Post_Add nhu sau: chú ý <B>tên</B> và <B>d* li*u</B> c*a các tham s*</P>
<P>CREATE</P> PROCEDURE
<P> [Post_Add]</P><B>
<P>@PostID </P></B>int output,
<P>-- output: tuong tu nhu truyen tham chieu trong OOP </P>
<P>@Title </P>nvarchar(50
<P>),</P>
<P>@Body </P>nvarchar(4000)
<P>,</P>
<P>@Publish </P><B>
<P>datetime</P></B>
<P>AS</P>
<P>INSERT</P> INTO
<P> [Post]</P>
<P>(</P>
<P>[Title]</P>
<P>,</P>
<P>[Body]</P>
<P>,</P>
<P>[Publish]</P>
<P>)</P>
<P>VALUES</P>
<P>( </P>
<P>@Title</P>
<P>,</P>
<P>@Body</P>
<P>,</P>
<P>@Publish</P>
<P>)</P>
<P>-- Lay gia tri tu tang cua record vua moi them vao</P>
<P>SET</P> @PostID = @@IDENTITY
<P> </P><B>
<P>Xây d*ng thu vi*n Data Access </P></B>
<P>1. T*o m*t thu m*c * Desktop d*t tên là <B>Sample</P></B>
<P>2. M* Microsoft Visual Studio (VS), t*o m*t project thu vi*n (Class Library Project) </P>
<P>và d*t tên là <B>DataAccess</P></B>
<P>[email protected] </P>
<P>4 </P>
<P>Xây d*ng thu vi*n Data Access </P><B>
</B>
<P>3. T*o 3 class: </P><B>
<P>Tên Class Mô t* </P></B>
<P>DataProvider L*p tr*u tu*ng ch*a các phuong th*c abstract</P>
<P>SqlDataProvider L*p k* th*a t* l*p DataProvider, dùng cho SQL Server</P>
<P>Post L*p ánh x* t* b*ng Post </P>
<P> </P>
<P> </P>
<P> </P>
<B>
</B>
<P>[email protected] </P>
<P>B*ng ánh x* ki*u d* li*u SQL Server sang ki*u d* li*u trong C#</P><B>
<P>DBType </P></B>
<P>*</P>
<P><B> </P></B>
<P>5 </P>
<P>Xây d*ng thu vi*n Data Access </P><B>
<P>C# Type</P></B>
<P>...char String</P>
<P>...text String</P>
<P>bit Bool</P>
<P>datetime DateTime</P>
<P>smalldatetime DateTime</P>
<P>smallint Int16</P>
<P>int Int32 (int)</P>
<P>bigint Int64 (long)</P>
<P>float float</P>
<P>decimal Decimal</P>
<P>money Double</P>
<P>real Double</P>
<P>... ... </P><B>
<P>Vi*t code cho l*p Post (Post.cs) </P>
<P>Tên c*t DBType </P>
<P>C# Property C# Type</P></B></P>
<P>
<P>
<P>PostID int PostID int ho*c Int32</P>
<P>Title Nvarchar(50) Title String</P>
<P>Body Nvarchar(max) Body String</P>
<P>Publish Datetime (</P><B>du*c phép null</B>) Publish
<P><B>DateTime? </P></B></P></P>
<P>
<P>Phiên b*n .NET 2.0 (VS 2005) code nhu sau </P>
<P>using</P>
<P> System;</P>
<P>namespace</P>
<P> DataAccess</P>
<P>{</P>
public class
<P>Post</P>
<P>{</P>
private int
<P> _PostID;</P>
public int
<P> PostID</P>
<P>{</P>
get { return
<P> _PostID; }</P>
set { _PostID = value
<P>; }</P>
<P>}</P>
private string
<P> _Title;</P>
public string
<P> Title</P>
<P>{</P>
get { return
<P> _Title; }</P>
set { _Title = value
<P>; }</P>
<P>}</P>
private string
<P> _Body; </P>
<P>*</P>
<P><B> </P></B>
<P>[email protected] </P>
public string
<P> Body</P>
<P>{</P>
get { return
<P> _Body; }</P>
set { _Body = value
<P>; }</P>
<P>}</P>
private DateTime
<P>? _Publish;</P>
public DateTime
<P>? Publish</P>
<P>{</P>
get { return
<P> _Publish; }</P>
set { _Publish = value
<P>; }</P>
<P>}</P>
public
<P> Post()</P>
<P>{</P>
<P>}</P>
<P>}</P>
<P>}</P>
<P><B> </P></B>
<P>6 </P>
<P>Xây d*ng thu vi*n Data Access </P>
<P>Các phiên b*n t* .NET 3.5 v* sau (t* VS 2008 v* sau) có th* code rút g*n nhu sau</P>
<P>ho*c theo cách code c*a phiên b*n .NET 2.0 </P>
<P>using</P>
<P> System;</P>
<P>namespace</P>
<P> DataAccess</P>
<P>{</P>
public class
<P>Post</P>
<P>{</P>
public int PostID { get; set
<P>; }</P>
public string Title { get; set
<P>; }</P>
public string Body { get; set
<P>; }</P>
public DateTime? Publish { get; set
<P>; }</P>
public
<P> Post()</P>
<P>{ </P>
<P>}</P>
<P>}</P>
<P>} </P><B>
<P>Vi*t code cho l*p DataProvider (DataProvider.cs) </P></B>
<P>using</P>
<P> System;</P>
<P>using</P>
<P> System.Data;</P>
<P>namespace</P>
<P> DataAccess</P>
<P>{</P>
public abstract class DataProvider
<P> </P>
<P>{</P>
public abstract int PostAdd(Post
<P> post);</P>
<P>}</P>
<P>}<B> </P></B>
<P>[email protected] </P>
<B>
<P>Vi*t code cho l*p SqlDataProvider (SqlDataProvider.cs) </P></B>
<P>7 </P>
<P>Xây d*ng thu vi*n Data Access </P>
<P>Trong l*p SqlDataProvider có s* d*ng l*p <B>ConfigurationManager</B> d* l*y chu*i k*t n*i</P>
<P>t* <B>web config/app config</B>. Mu*n s* d*ng du*c l*p này thì ph*i thêm m*t thu vi*n</P>
<P>System.Configuration vào project hi*n t*i. Cách thêm nhu sau:</P>
<P><B> </P></B>
<P>B*m chu*t ph*i vào m*c References (bên c*a s* Solution) ch*n Add Reference<B> </P></B>
<P>Ch*n th* .NET trong c*a s* v*a m*i hi*n ra, sau dó ch*n dòng </P><B>
<P>System.Configuration</B> và b*m OK </P>
<P>[email protected] </P>
<P>using</P>
<P> System;</P>
<P>using</P>
<P> System.Data.SqlClient;</P>
<P>using</P>
<P> System.Configuration;</P>
<P>using</P>
<P> System.Data;</P>
<P>namespace</P>
<P> DataAccess</P>
<P>{</P>
public class SqlDataProvider :
<P>DataProvider</P>
<P>{ </P>
private string
<P> _ConnectionString;</P>
public SqlDataProvider(string
<P> connectionStringName)</P>
<P>{ </P>
<P>// Lay chuoi ket noi tu web config / app config</P>
<P>_ConnectionString =</P>
<P>ConfigurationManager</P>
<P>.ConnectionStrings[connectionStringName]</P>
<P>.ConnectionString;</P>
<P>}</P>
<P>// Ham tao mot ket noi den CSDL</P>
protected SqlConnection
<P> GetSqlConnection()</P>
<P>{</P>
<P>try</P>
<P>{</P>
return new SqlConnection
<P>(_ConnectionString);</P>
<P>}</P>
<P>catch</P>
<P>{</P>
throw new Exception("SqlConnection"
<P>);</P>
<P>}</P>
<P>}</P>
<P> </P>
public override int PostAdd(Post
<P> post)</P>
<P>{ </P>
<P>// 1. Tao doi tuong SqlConnection</P>
using (SqlConnection
<P> cnn = GetSqlConnection())</P>
<P>{ </P>
<P>// 2. Tao doi tuong SqlCommand</P>
SqlCommand
<P> cmd = cnn.CreateCommand(); </P>
<P>// 2.1 Dat loai command la SP va ten thu tuc</P>
<P>// Thuc hien truy van tu SP </P>
<P>8 </P>
<P>Xây d*ng thu vi*n Data Access </P>
<P>cmd.CommandType = </P>CommandType
<P>.StoredProcedure;</P>
<P>// Truyen ten cua SP</P>
<P>cmd.CommandText = </P>"Post_Add"
<P>; </P>
<P>// 2.2 Truyen ten, kieu du lieu va gia tri tham so</P>
<P>// Tuong ung voi phan PostID output trong SP </P>
// Do cot PostID tu tang nen khong can truyen gia tri
<P> </P>
<P>cmd.Parameters.Add(</P>"@PostID", SqlDbType
<P>.Int) </P>
<P>.Direction = </P>ParameterDirection
<P>.Output;</P>
<P>cmd.Parameters.Add(</P>"@Title", SqlDbType
<P>.NVarChar, 50) </P>
<P>.Value = post.Title; </P>
<P>[email protected] </P>
<P>9 </P>
<P>Xây d*ng thu vi*n Data Access </P>
<P>cmd.Parameters.Add(</P>"@Body", SqlDbType
<P>.NVarChar, 4000)</P>
<P>.Value = post.Body;</P>
<P>// chi co kieu du lieu nullable </P>
<P>// (them dau ? sau kieu du lieu)</P>
<P>// moi phai kiem tra HasValue</P>
<P> </P>
if
<P> (post.Publish.HasValue)</P>
<P>cmd.Parameters.Add(</P>"@Publish", SqlDbType
<P>.DateTime)</P>
<P>.Value = post.Publish.Value;</P>
<P>else</P>
<P>cmd.Parameters.Add(</P>"@Publish", SqlDbType
<P>.DateTime)</P>
<P>.Value = </P>DBNull
<P>.Value;</P>
<P>// 3. Mo ket noi</P>
<P>cnn.Open();</P>
<P>// Thuc hien them mot record voi cac gia tri </P>
<P>// duoc truyen thong qua cacs Parameter</P>
<P>// Ket qua cua ham ExecuteNonQuery la </P>
<P>// so record duoc them vao CSDL</P>
<P>// > 0: them vao thanh cong</P>
<P>// = 0: khong co hang nao duoc them ~ that bai </P>
<P>// 4. Goi ham ExecuteNonQuery cua doi tuong SqlCommand</P>
int
<P> rs = cmd.ExecuteNonQuery(); </P>
if
<P> (rs > 0)</P>
<P>// 5. Lay gia tri id tu tang cua record vua them vao</P>
return (int)cmd.Parameters["@PostID"
<P>].Value; </P>
return
<P> 0;</P>
<P>}</P>
<P>}</P>
<P>}</P>
<P>} </P>
<B>
<P>B* sung code vào l*p DataProvider </P></B>
<P>private</P> static DataProvider _Instance = null
<P>;</P>
<P>public</P> static DataProvider
<P> Instance</P>
<P>{</P>
<P>get</P>
<P>{</P>
if (_Instance == null
<P>)</P>
<P>_Instance = </P>new SqlDataProvider("ConnectionString"
<P>);</P>
return
<P> _Instance;</P>
<P>}</P>
<P>}</P>
<P> </P>
<P>B*m <B>Ctrl + Shift + B</B> ho*c ch*n menu <B>Build > Build Solution</B> d* build project </P>
<P>[email protected] </P>
<B>
<P>Test </P></B>
<P>1. Thêm project ki*u Console (Console Application) trong thu m*c Sample và d*t</P>
<P>tên là <B>Test</P></B>
<P>[email protected] </P>
<P>10 </P>
<P>Test </P>
<P>2. Set Startup Project: b*m chu*t ph*i vào Project Test và ch*n <B>Set as StartUp</P>
<P>Project</B> </P>
<P>3. Thêm file App.Config project Console </P>
<P>[email protected] </P>
<P>11 </P>
<P>Test </P>
<P>4. Thêm thu vi*n DataAccess vào project Console</P></P>
<P>
<P>
<P>[email protected] </P>
<P>12 </P>
<P>Test </P>
</P></P>
<P>
</P>
<P>
<P>
<P>
<P>5. M* t*p t*p tin App.config và thêm vào do*n mã sau </P>
<P><?</P>xml version="1.0" encoding="utf-8"
<P> ?></P>
<P><</P>configuration
<P>></P>
<P><</P>connectionStrings
<P>></P>
<P><</P>add name="<B>ConnectionString</B>"
<P> </P>
connectionString="
<P>server=.\sqlexpress;database=sample;integrated</P>
<P>security=true</P>"
<P>/></P>
<P></</P>connectionStrings
<P>> </P>
<P>[email protected] </P>
<P>13 </P>
<P>Test </P>
<P></</P>configuration
<P>></P>
<B>
<P>Name</B>: tuong *ng v*i chu*i </P><B>ConnectionString </B>
<P>trong thu*c tính <B>Instance</B> c*a</P>
<P>l*p</P>
<P>DataProvider </P>
<P>public</P> static DataProvider
<P> Instance</P>
<P>{</P>
<P>get</P>
<P>{</P>
if (_Instance == null
<P>)</P>
<P>_Instance = </P>new SqlDataProvider("ConnectionString"
<P>);</P>
return
<P> _Instance;</P>
<P>}</P>
<P>}</P>
<P> </P><B>
<P>ConnectionString</B></P>: chu*i k*t n*i (có th* khác nhau tùy t*ng máy, d*u
<P><B>"ch*m"</P></B></P></P>
<P>trong <B>.\sqlexpress</B> tuong duong v*i ch* <B>(local)\sqlexpress</B>)</P>
<P>6. Thêm do*n code sau vào hàm Main trong l*p Program.cs </P></P>
<P>
<P>
<P>static</P> void Main(string
<P>[] args)</P>
<P>{</P>
<P>// Tao mot doi tuong de them vao CSDL</P>
Post p = new Post
<P>();</P>
<P>p.Title = </P>"A Title"
<P>;</P>
<P>p.Body = </P>"Lorem Ip sum"
<P>;</P>
<P>// Them du lieu</P>
int rs = DataProvider
<P>.Instance.PostAdd(p);</P>
if
<P> (rs > 0)</P>
Console.WriteLine("New post id is "
<P> + rs);</P>
<P>else</P>
Console.WriteLine("Insert failed!"
<P>);</P>
Console
<P>.Read(); </P>
<P>} </P>
<P>7. Ch*y và xem k*t qu* </P>
<P>8. Tuong t* thêm m*t record khác v*i thu*c tính Publish d*t b*ng DateTime.Now.</P>
<P>Sau dó dùng SQL Server d* ki*m d* li*u v*a du*c thêm vào. </P>
<P>[email protected] </P>
<P>14 </P>
<P>Test </P>
<B>
<P>Luy*n t*p </P></B>
<P>1. Tuong t* nhu trên t*o th* t*c Post_Update và b* sung thêm phuong th*c</P>
<P>PostUpdate vào thu vi*n DataAccess d* g*i th* t*c trên. </P>
<P>2. T*o th* t*c Post_Delete và phuong th*c PostDelete </P><B>
<P>Hu*ng d*n:</P></B>
<P>T*o th* t*c Post_Update </P>
<P>CREATE</P> PROCEDURE
<P> [Post_Update]</P>
<P>@PostID </P>int
<P>,</P>
<P>@Title </P>nvarchar(50
<P>),</P>
<P>@Body </P>nvarchar(4000
<P>),</P>
<P>@Publish </P>
<P>datetime</P>
<P>AS</P>
<P>UPDATE</P> [Post]
<P>SET</P>
<P>[Title] </P>= @Title
<P>,</P>
<P>[Body] </P>= @Body
<P>,</P>
<P>[Publish] </P>=
<P> @Publish</P>
<P>WHERE</P> [PostID] = @PostID
<P> </P>
<P>B* sung thêm phuo</P>ng th
<P>*c vào l*p DataProvider </P>
<P>public</P> abstract int PostUpdate(Post
<P> post);</P>
<P>B* sung code vào l*p SqlDataProvider</P>
<P>Ð* ti*t ki*m th*i gian b*n có th* rê chu*t vào ch* DataProvider trong l*p </P>
<P>SqlDataProvider và b*m vào nút mui tên xu*ng > ch*n <B>Implement abstract class </B>nhu</P>
<P>hình sau d* VS t* d*ng sinh 1 ph*n mã </P>
<P>Sau khi b*m vào dó thì VS s* sinh ra do*n mã nhu sau </P>
<P>[email protected] </P>
<P>15 </P>
<P>Luy*n t*p </P>
<P>public</P> override int PostUpdate(Post
<P> post)</P>
<P>{</P>
throw new NotImplementedException
<P>();</P>
<P>}</P>
<P> </P>
<P>16 </P>
<P>Làm ti*p các th* t*c còn l*i </P>
<P>Xóa dòng </P>throw new NotImplementedException().<B>Copy & Paste</B> t* hàm <B>
<P>PostAdd</B></P>, sau dó s*a l*i nh*ng ph*n c*n thi*t d* phù h*p v*i SP Post_Update. Ph*n</P></P>
<P>in d*m là ph*n du*c thay d*i </P>
<P><B>
</B></P>
<P>
<P>
<P>public</P> override int PostUpdate(Post
<P> post)</P>
<P>{</P>
using (SqlConnection
<P> cnn = GetSqlConnection())</P>
<P>{</P>
SqlCommand
<P> cmd = cnn.CreateCommand();</P>
<P>cmd.CommandType = </P>CommandType
<P>.StoredProcedure;</P>
<P>cmd.CommandText = </P>"Post_Update"
<P>;</P>
<P>cmd.Parameters.Add(</P>"@PostID", SqlDbType
<P>.Int) </P>
<P>.Value = post.PostID;</P>
<P>cmd.Parameters.Add(</P>"@Title", SqlDbType
<P>.NVarChar, 50) </P>
<P>.Value = post.Title;</P>
<P>cmd.Parameters.Add(</P>"@Body", SqlDbType
<P>.NVarChar, 4000) </P>
<P>.Value = post.Body;</P>
if
<P> (post.Publish.HasValue)</P>
<P>cmd.Parameters.Add(</P>"@Publish", SqlDbType
<P>.DateTime)</P>
<P>.Value = post.Publish.Value;</P>
<P>else</P>
<P>cmd.Parameters.Add(</P>"@Publish", SqlDbType
<P>.DateTime)</P>
<P>.Value = </P>DBNull
<P>.Value;</P>
<P>cnn.Open();</P>
return
<P> cmd.ExecuteNonQuery();</P>
<P>}</P>
<P>}</P>
<P>Ki*m tra k*t qu*</P>
<P>T*o th* t*c Post_Delete và làm tuong t*. <B>Chú ý</B>: th* t*c này ch* có 1 tham s* </P>
<P>CREATE</P> PROCEDURE
<P> [Post_Delete]</P>
<P>@PostID </P>
<P>int </P>
<P>AS</P>
<P>DELETE</P>
<P> [Post]</P>
<P>WHERE</P> [PostID] = @PostID
<P> </P><B>
<P>Làm ti*p các th* t*c còn l*i </P></B>
<P>Post_Count Ð*m t*t c* bài post</P>
<P>Post_All L*y t*t c* bài post (s*p x*p theo m*t PostID gi*m d*n)</P>
<P>Post_Single Tìm bài post theo PostID</P>
<P>Post_Find Tìm ki*m các bài post theo Title </P>
<P>[email protected] </P>
<B>
<P>Hu*ng d*n: </P></B>
<P>Th</P>* t*c và các hàm b*<B>
<P> sung Post_Count </P></B><I>
<P>SP Post_Count </P></I>
<P>CREATE</P> PROCEDURE
<P> [Post_Count]</P>
<P>AS</P>
<P>SELECT</P> COUNT(PostID) FROM [Post]
<P><B> </P></B><I>
<P>DataProvider.cs </P></I>
<P>public</P> abstract int
<P> PostCount(); </P><I>
<P>SqlDataProvider.cs </P></I>
<P>public</P> override int
<P> PostCount()</P>
<P>{</P>
using (SqlConnection
<P> cnn = GetSqlConnection())</P>
<P>{</P>
SqlCommand
<P> cmd = cnn.CreateCommand();</P>
<P>cmd.CommandType = </P>CommandType
<P>.StoredProcedure;</P>
<P>cmd.CommandText = </P>"Post_Count"
<P>;</P>
<P>cnn.Open();</P>
<P><B>object rs = cmd.ExecuteScalar();</P></B></P></P>
<P>return Convert.ToInt32(rs);</P>
<P>}</P>
<P>} </P>
<P><I></I></P>
<P><B></P>
</B>
<P>
<P>17 </P>
<P>Làm ti*p các th* t*c còn l*i </P>
<P>Khi truy v*n SP Post_Count thì b*ng k*t qu* tr* v* nhu sau, ch* có 1 hàng và 1 c*t. Ð*</P>
<P>l*y du*c ô d* li*u dó 1 cách nhanh chóng ngoài cách dùng DataReader thì b*n dùng</P>
<P>phuong th*c <B>ExcecuteScalar</B>, sau dó ép v* ki*u mà b*n mu*n. </P><B>
<P>Th* t*c và các hàm b* sung Post_All </P></B><I>
<P>SP Post_All </P></I>
<P>CREATE</P> PROCEDURE
<P> [Post_All]</P>
<P>AS</P>
<P>SET</P> NOCOUNT ON
<P> -- sv tu tim hieu tac dung cua lenh nay</P>
<P>SELECT</P> * FROM [Post] ORDER BY [PostID]
<P> DESC </P>
<I>
</I>
<P>[email protected] </P>
<I>
<P>DataProvider.cs </P></I>
<P>public</P> abstract List<Post
<P>> PostAll(); </P><I>
<P>SqlDataProvider.cs </P></I>
<P>18 </P>
<P>Làm ti*p các th* t*c còn l*i </P>
<P>public</P> override List<Post
<P>> PostAll()</P>
<P>{</P>
using (SqlConnection
<P> cnn = GetSqlConnection())</P>
<P>{</P>
SqlCommand
<P> cmd = cnn.CreateCommand();</P>
<P>cmd.CommandType = </P>CommandType
<P>.StoredProcedure;</P>
<P>cmd.CommandText = </P>"Post_All"
<P>;</P>
<P>cnn.Open();</P>
<P>// Tao doi tuong SqlDataReader de doc du lieu tuan tu tu csdl</P>
using (SqlDataReader
<P> reader =</P>
<P>cmd.ExecuteReader(</P>CommandBehavior
<P>.CloseConnection))</P>
<P>{</P>
<P>// Tao mot danh sach de chua du lieu doc duoc</P>
List<Post> list = new List<Post
<P>>();</P>
<P>// Do kq tra ve la mot ds nhieu record nen phai dung</P>
<P>// vong lap while de doc tung reader</P>
<P>// reader.Read() => doc mot record tu csdl</P>
while
<P> (reader.Read())</P>
<P>{</P>
<P>// Trich du lieu tu 1 record va day vao doi tuong Post</P>
Post p = new Post
<P>();</P>
<P>// reader[ten cot] => doc gia tri cua mot o^ du lieu </P>
<P>// Kieu du lieu tra ve la object => ep ve kieu du lieu tuong ung voi </P>
<P>// tung property cua doi tuong</P>
<P>p.PostID = </P>Convert.ToInt32(reader["PostID"
<P>]);</P>
<P>p.Title = reader[</P>"Title"
<P>].ToString();</P>
<P>p.Body = reader[</P>"Body"
<P>].ToString();</P>
<P>// Do cot Publish duoc phep null nen kiem tra truoc khi ep kieu du lieu</P>
<P>// DBNull.Value: gia tri NULL trong database</P>
if (reader["Publish"] != DBNull
<P>.Value)</P>
<P>p.Publish = </P>DateTime.Parse(reader["Publish"
<P>].ToString());</P>
<P>list.Add(p);</P>
<P>}</P>
return
<P> list;</P>
<P>}</P>
<P>}</P>
<P>}</P>
<B>
<P>Th* t*c và hàm b* sung Post_Single </P></B><I>
<P>SP Post_Single </P></I>
<P>CREATE</P> PROCEDURE [dbo].
<P>[Post_Single]</P>
<P>@PostID </P>
<P>int</P>
<P>AS </P>
<P>[email protected] </P>
<P>SET</P> NOCOUNT
<P>ON</P>
<P>SELECT</P> * FROM [Post] WHERE [PostID]=
<P>@PostID</P>
<I>
<P>DataProvider.cs </P></I>
<P>public</P> abstract Post PostSingle(int
<P> postId);</P><I>
<P>SqlDataProvider.cs </P></I>
<P>public</P> override Post PostSingle(int
<P> postId)</P>
<P>{</P>
using (SqlConnection
<P> cnn = GetSqlConnection())</P>
<P>{</P>
SqlCommand
<P> cmd = cnn.CreateCommand();</P>
<P>cmd.CommandType = </P>CommandType
<P>.StoredProcedure;</P>
<P>cmd.CommandText = </P>"Post_Single"
<P>;</P>
<P>cmd.Parameters.Add(</P>"@PostID", SqlDbType
<P>.Int).Value = postId;</P>
<P>cnn.Open();</P>
using (SqlDataReader
<P> reader =</P>
<P>cmd.ExecuteReader(</P>CommandBehavior
<P>.CloseConnection))</P>
<P>{ </P>
<P>19 </P>
<P>Làm ti*p các th* t*c còn l*i </P>
<P>// ket qua chi la 1 record nen ko dung vong </P>
<P>while</P>
<P>reader.Read();</P>
Post p = new Post
<P>();</P>
<P>p.PostID = </P>Convert.ToInt32(reader["PostID"
<P>]);</P>
<P>p.Title = reader[</P>"Title"
<P>].ToString();</P>
<P>p.Body = reader[</P>"Body"
<P>].ToString();</P>
if (reader["Publish"] != DBNull
<P>.Value)</P>
<P>p.Publish = </P>DateTime.Parse(reader["Publish"
<P>].ToString());</P>
return
<P> p;</P>
<P>}</P>
<P>}</P>
<P>}</P>
<B>
<P>Th* t*c và hàm b* sung Post_Find </P></B><I>
<P>SP Post_Find </P></I>
<P>CREATE</P> PROCEDURE [dbo].
<P>[Post_Find]</P>
<P>@Title </P>nvarchar(50
<P>)</P>
<P>AS</P>
<P>SET</P> NOCOUNT
<P>ON</P>
<P>SELECT</P> * FROM [Post] WHERE [Title] LIKE N'%'+@Title+'%'
<P><I> </P>
<P>DataProvider.cs </P></I>
<P>public</P> abstract List<Post> PostFind(string
<P> title);</P>
<I>
<P>SqlDataProvider.cs </P></I>
<P>[email protected] </P>
<P>20 </P>
<P>S* d*ng l*p SqlHelper d* t*i uu l*p SqlDataProvider </P>
<P>public</P> override List<Post> PostFind(string
<P> title)</P>
<P>{ </P>
<P>// sinh vien tu lam</P>
<P>} </P><B>
<P>Nh*n xét:<I> </P></B></I>
<P>Các phuong th*c trong l*p SqlDataProvider s* du*c *ng d*ng g*i r*t nhi*u l*n. Ð*</P>
<P>tránh vi*c t*o di t*o l*i các d*i tu*ng SqlParamter, thì chúng ta s* cache (luu vào b*</P>
<P>nh*) d* sau này có th* dùng l*i. </P>
<P>Cách làm tham kh*o trong cu*n ebook <B>ScaleNet</B> trang 543 m*c <B>Cache Stored</P>
<P>Procedure SqlParameter Objects</B> ho*c s* d*ng l*p <B>SqlHelper</B> </P><B>
<P>S* d*ng l*p SqlHelper d* t*i uu l*p SqlDataProvider </P></B>
<P>Copy t*p tin <B>Files/ DataTools/SQLHelper.cs</B> vào project DataAccess.</P>
<P>T*o 1 class SqlDataProviderV2 cho k* th*a t* l*p DataProvider và b* sung ph*n thân </P>
<P>cho các phuong th*c du*c k* th*a. Chèn thêm namespace</P><B>
<P>Microsoft.ApplicationBlocks.Data</B></P> d* s* d*ng l*p SqlHelper
<P> </P>
<P>Ð*i v*i phuong th*c PostUpdate, ta thêm nhu sau: </P>
<P>public</P> override int PostUpdate(Post
<P> post)</P>
<P>{</P>
return SqlHelper.ExecuteNonQuery(_ConnectionString, "Post_Update"
<P>, </P>
<P>post.PostID, post.Title, post.Body, post.Publish);</P>
<P>}</P>
<P> </P><B>
<P>Gi*i thích:</P></B>
<P>Phuong th*c PostUpdate * trên dùng d* g*i SP Post_Update. Ý nghia l*n lu*t c*a các </P>
<P>tham s* truy*n vào nhu sau</P>
<P>_ConnectionString: </P>
<P>chu*i k*t n*i</P>
<P>"Post_Update"</P>:
<P>tên c*a SP mu*n g*i</P>
<P>post.PostID, post.Title, post.Body, post.Publish: </P>
<P>* cách làm tru*c thì dây </P>
<P>chính là giá tr* c*a các tham s* mà b*n mu*n truy*n vào 4 tham s* theo th* t* là:</P>
<P>@PostID, @Title, @Body, @Publish </P>
<P>SqlCommand</P>
<P> cmd = cnn.CreateCommand();</P>
<P>cmd.CommandType = </P>CommandType
<P>.StoredProcedure;</P>
<P><B>cmd.CommandText = </P></B>"Post_Update"
<P>;</P>
<P>cmd.Parameters.Add(</P>"@PostID", SqlDbType<B>
<P>.Int).Value = post.PostID; </P></B>
<P>[email protected] </P>
<P>21 </P>
<P>S* d*ng l*p SqlHelper d* t*i uu l*p SqlDataProvider </P>
<B>
<P>cmd.Parameters.Add("@Title", SqlDbType.NVarChar, 50).Value = post.Title;</P>
<P>cmd.Parameters.Add("@Body", SqlDbType.NVarChar, 4000).Value = post.Body;</P></B></P>
<P>
<P>
if
<P> (post.Publish.HasValue)</P>
<P><B>
<P>cmd.Parameters.Add("@Publish", SqlDbType.DateTime)</P>
<P>.Value = post.Publish.Value;</P></B></P>
<P>else</P></P></P>
<P>
<P><B>
<P>cmd.Parameters.Add("@Publish", SqlDbType.DateTime)</P>
<P>.Value = DBNull.Valu</B></P>e;
<P> </P>
<P>Khi s* d*ng SqlHelper thì b*n không c*n ph*i vi*t chi ti*t nhu trên mà ch* c*n truy*n</P>
<P>giá tr* dúng v*i th* t* các tham s* trong SP, có nghia là </P>
<P>Giá tr* post.PostID </P>*
<P> giá tr* c*a tham s* @PostID</P>
<P>post.Title </P>*
<P> giá tr* c*a tham s* @Title</P>
<P>.... </P>
<P>Tuong t* nhu cách làm trên, ph*n code b* sung cho phuong th*c PostDelete nhu sau </P>
<P>public</P> override int PostDelete(Post
<P> post)</P>
<P>{ </P>
<P>} </P>
<P>return</P> SqlHelper
<P>.ExecuteNonQuery(_ConnectionString, </P>
<P>"Post_Delete"</P>, post.PostID);
<P> </P>
<P>Do 2 th* t*c Update và Delete không có tham s* nào là ouput nên cách vi*t trên không</P>
<P>có v*n d* gì. B*n ti*p t*c b* sung code cho phuong th*c PostAdd thì m*t s* c* nh*</P>
<P>x*y ra là b*n mu*n l*y giá tr* output t* SP. Cách làm nhu sau:</P>
<P>// Gan tu dong cac gia tri cho cac doi tuong SqlParameter</P>
<P> </P>
<P>private</P> void AssignParameterValues(SqlParameter[] commandParameters, object
<P>[]</P>
<P>parameterValues)</P>
<P>{</P>
if ((commandParameters == null) || (parameterValues == null
<P>))</P>
return
<P>;</P>
if
<P> (commandParameters.Length != parameterValues.Length)</P>
throw new ArgumentException(
<P>"Parameter count does not match Parameter Value</P>
<P>count."</P>
<P>);</P>
for (int
<P> i = 0, j = commandParameters.Length; i < j; i++)</P>
<P>commandParameters[i].Value = parameterValues[i];</P>
<P>}</P>
<P>public</P> override int PostAdd(Post
<P> post)</P>
<P>{</P>
string spName = "Post_Add"
<P>;</P>
<P>// Lay cac tham so da luu trong bo nho</P>
<P> </P>
SqlParameter
<P>[] parameters =</P>
<P>SqlHelperParameterCache</P>
<P>.GetSpParameterSet(_ConnectionString, spName);</P>
<P>// Gan cac gia tri cho cac tham so do theo dung thu tu trong SP</P>
<P> </P>
<P>[email protected] </P>
<P>22 </P>
<P>S* d*ng l*p SqlHelper d* t*i uu l*p SqlDataProvider </P>
<P>AssignParameterValues(parameters,</P>
new object
<P>[] { post.PostID, post.Title, post.Body, post.Publish });</P>
// Lay cac tham so da luu trong bo nho
<P> </P>
int rs =
<P><B>SqlHelper.ExecuteNonQuery(_ConnectionString,</P>
<P>CommandType.StoredProcedure, spName, parameters);</P></B></P></P>
<P>
<P>
if
<P> (rs > 0)</P>
<P>{</P>
<P>// Lay gia tri cua tham so @PostID</P></P></P>
<P><B>
</B></P>
<P><B>
</B></P>
<P>
<P>
foreach (SqlParameter p in
<P> parameters)</P>
<P>{</P>
if (String.Compare(p.ParameterName, "@PostID", true
<P>) == 0)</P>
return (int
<P>)p.Value;</P>
<P>}</P>
<P>}</P>
return
<P> rs;</P>
<P>}</P>
<P> </P>
<P>Ph*n code cho các phuong th*c còn l*i</P>
<P>public</P> override Post PostSingle(int
<P> postId)</P>
<P>{</P>
using (SqlDataReader reader = SqlHelper
<P>.ExecuteReader(_ConnectionString,</P>
<P>"Post_Single"</P>
<P>, postId))</P>
<P>{</P>
<P>reader.Read();</P><B>
</B>Post p = new Post
<P>();</P>
<P>p.PostID = </P>Convert.ToInt32(reader["PostID"
<P>]);</P>
<P>p.Title = reader[</P>"Title"
<P>].ToString();</P>
<P>p.Body = reader[</P>"Body"
<P>].ToString();</P>
if (reader["Publish"] != DBNull
<P>.Value)</P>
<P>p.Publish = </P>DateTime.Parse(reader["Publish"<B>
<P>].ToString());</P></B>
return
<P> p;</P>
<P>}</P>
<P>}</P>
<P> </P>
<P>public</P> override List<Post> PostFind(string
<P> title)</P>
<P>{</P>
using (SqlDataReader reader = SqlHelper
<P>.ExecuteReader(_ConnectionString,</P>
<P>"Post_Find"</P>
<P>, title))</P>
<P>{</P>
List<Post> list = new List<Post
<P>>();</P>
while
<P> (reader.Read())</P>
<P>{</P><B>
</B>Post p = new Post
<P>();</P>
<P>p.PostID = </P>Convert.ToInt32(reader["PostID"
<P>]);</P>
<P>p.Title = reader[</P>"Title"
<P>].ToString();</P>
<P>p.Body = reader[</P>"Body"
<P>].ToString();</P>
if (reader["Publish"] != DBNull
<P>.Value)</P>
<P>p.Publish = </P>DateTime.Parse(reader["Publish"<B>
<P>].ToString());</P></B>
<P>list.Add(p);</P>
<P>} </P>
<P>[email protected] </P>
return
<P> list;</P>
<P>}</P>
<P>}</P>
<P> </P>
<P>23 </P>
<P>S* d*ng l*p SqlHelper d* t*i uu l*p SqlDataProvider </P>
<P>public</P> override List<Post
<P>> PostAll()</P>
<P>{</P>
using (SqlDataReader reader = SqlHelper
<P>.ExecuteReader(_ConnectionString,</P>
<P>"Post_All"</P>
<P>))</P>
<P>{</P>
List<Post> list = new List<Post
<P>>();</P>
while
<P> (reader.Read())</P>
<P>{</P>
<P><B>Post p = new Post();</P>
<P>p.PostID = Convert.ToInt32(reader["PostID"]);</P>
<P>p.Title = reader["Title"].ToString();</P>
<P>p.Body = reader["Body"].ToString();</P>
<P>if (reader["Publish"] != DBNull.Value)</P>
<P>p.Publish = DateTime.Parse(reader["Publish"].ToString());</P></B></P></P>
<P>
<P>
<P>list.Add(p);</P>
<P>}</P>
return
<P> list;</P>
<P>}</P>
<P>}</P></P></P>
<P><B>
</B></P>
<P>
<P>public</P> override int
<P> PostCount()</P>
<P>{</P>
object rs = SqlHelper.ExecuteScalar(_ConnectionString, "Post_Count"
<P>);</P>
return Convert
<P>.ToInt32(rs);</P>
<P>} </P>
<P>S*a l*i code trong l*p DataProvider nhu sau, sau dó ch*y, ki*m tra k*t qu* và các b*n</P>
<P>s* th*y nó v*n ch*y bình thu*ng</P>
<P>public</P> static DataProvider
<P> Instance</P>
<P>{</P>
<P>get</P>
<P>{</P>
if (_Instance == null
<P>)</P><B>
<P>_Instance = </P></B>new SqlDataProviderV2("ConnectionString"<B>
<P>);</P></B>
return
<P> _Instance;</P>
<P>}</P>
<P>} </P>
<B>
<P>Nh*n xét: </P></B></P>
<P>
<P>
<P>Ð*i v*i 3 phuong th*c Single, Find và All ph*n code tuong d*i gi*ng nhau, ch* khác *</P>
<P>ph*n </P>SqlHelper
<P>.ExecuteReader (truy*n tên th* t*c và các giá tr* cho các tham s* c*a</P>
<P>th* t*c).</P>
<P>Ph*n code tô d*m chính là ph*n trích d* li*u t* DataReader và chuy*n vào d*i tu*ng. </P></P></P>
<P>
</P>
<P>
<P>[email protected] </P>
<P>24 </P>
<P>S* d*ng l*p CBO d* t* d*ng chuy*n d* li*u t* DataReader sang d*i tu*ng </P>
<B>
<P>S* d*ng l*p CBO d* t* d*ng chuy*n d* li*u t* DataReader sang d*i tu*ng </P></B>
<P>Chèn thêm thu vi*n System.Web vào project DataAccess </P>
<P>Copy 3 t*p tin CBO.cs, Null.cs và DataCache.cs trong thu m*c <B>Files/DataTools</B> vào</P>
<P>project DataAccess</P>
<P>S*a l*i code c*a 3 phuong th*c Single, All, Find trong l*p SqlDataProviderV2 nhu sau</P>
<P>(chèn thêm namespace <B>Core</B> d* s* d*ng l*p CBO)</P>
<P>public</P> override Post PostSingle(int
<P> postId)</P>
<P>{</P>
return CBO.FillObject<Post>(SqlHelper
<P>.ExecuteReader(_ConnectionString,</P>
<P>"Post_Single"</P>
<P>, postId));</P>
<P>}</P>
<P>public</P> override List<Post> PostFind(string
<P> title)</P>
<P>{ </P>
return CBO.FillCollection<Post>(SqlHelper
<P>.ExecuteReader(_ConnectionString,</P>
<P>"Post_Find"</P>
<P>, title));</P>
<P>}</P>
<P>public</P> override List<Post
<P>> PostAll()</P>
<P>{</P>
return CBO.FillCollection<Post>(SqlHelper
<P>.ExecuteReader(_ConnectionString,</P>
<P>"Post_All"</P>
<P>));</P>
<P>}</P>
<P> </P>
<P>[email protected] </P>
<P>25 </P>
<P>S* d*ng l*p CBO d* t* d*ng chuy*n d* li*u t* DataReader sang d*i tu*ng </P><B>
<P>Cách s* d*ng l*p CBO </P></B>
<P>public</P> override Post PostSingle(int
<P> postId)</P>
<P>{</P>
return CBO.FillObject<Post>(SqlHelper
<P>.ExecuteReader(_ConnectionString,</P>
<P>"Post_Single"</P>
<P>, postId));</P>
<P>} </P>
<P>Phuong th*c <B>FillObject<ki*u d* li*u d*i tu*ng>( DataReader )</B>: l*y d* li*u t*</P>
<P>DataReader và tr* v* m*t d*i tu*ng </P>
<P>public</P> override List<Post
<P>> PostAll()</P>
<P>{</P>
return CBO.FillCollection<Post>(SqlHelper
<P>.ExecuteReader(_ConnectionString,</P>
<P>"Post_All"</P>
<P>));</P>
<P>}</P>
<P> </P>
<P>Phuong th*c <B>FillCollection< ki*u d* li*u d*i tu*ng>( DataReader )</B>: l*y d* li*u t*</P>
<P>DataReader và tr* v* m*t danh sách ki*u List</P>
<P>Toàn b* code c*a l*p SqlDataProviderV2 du*c vi*t nhu sau:</P>
<P>using</P>
<P> System;</P>
<P>using</P>
<P> System.Collections.Generic;</P>
<P>using</P>
<P> System.Configuration;</P>
<P>using</P> Microsoft.ApplicationBlocks.Data;
<P>// namespace cua lop SqlHelper</P>
<P>using</P>
<P> System.Data;</P>
<P>using</P>
<P> System.Data.SqlClient;</P>
<P>using</P> Core;
<P>// namespace cua lop CBO</P>
<P>namespace</P>
<P> DataAccess</P>
<P>{</P>
public class SqlDataProviderV2 :
<P>DataProvider</P>
<P>{</P>
private string
<P> _ConnectionString;</P>
public SqlDataProviderV2(string
<P> connectionStringName)</P>
<P>{</P>
<P>_ConnectionString =</P>
<P>ConfigurationManager</P>
<P>.ConnectionStrings[connectionStringName].ConnectionString;</P>
<P>}</P>
private void AssignParameterValues(SqlParameter[] commandParameters, object
<P>[]</P>
<P>parameterValues)</P>
<P>{</P>
if ((commandParameters == null) || (parameterValues == null
<P>))</P>
return
<P>;</P>
if
<P> (commandParameters.Length != parameterValues.Length)</P>
throw new ArgumentException(
<P>"Parameter count does not match Parameter</P>
<P>Value count."</P>
<P>);</P>
<P>[email protected] </P>
<P>26 </P>
<P>S* d*ng l*p CBO d* t* d*ng chuy*n d* li*u t* DataReader sang d*i tu*ng </P>
for (int
<P> i = 0, j = commandParameters.Length; i < j; i++)</P>
<P>commandParameters[i].Value = parameterValues[i];</P>
<P>}</P>
public override Post PostSingle(int
<P> postId)</P>
<P>{</P>
return CBO.FillObject<Post>(SqlHelper
<P>.ExecuteReader(_ConnectionString,</P>
<P>"Post_Single"</P>
<P>, postId));</P>
<P>}</P>
public override List<Post> PostFind(string
<P> title)</P>
<P>{</P>
<P>return</P>
<P>CBO</P>.FillCollection<Post>(SqlHelper.ExecuteReader(_ConnectionString, "Post_Find"
<P>,</P>
<P>title));</P>
<P>}</P>
public override List<Post
<P>> PostAll()</P>
<P>{</P>
<P>return</P>
<P>CBO</P>.FillCollection<Post>(SqlHelper.ExecuteReader(_ConnectionString, "Post_All"
<P>));</P>
<P>}</P>
public override int PostAdd(Post
<P> post)</P>
<P>{</P>
string spName = "Post_Add"
<P>;</P>
<P>// Lay cac tham so tu bo nho</P>
SqlParameter
<P>[] parameters =</P>
<P>SqlHelperParameterCache</P>
<P>.GetSpParameterSet(_ConnectionString, spName);</P>
<P>AssignParameterValues(parameters,</P>
new object
<P>[] { post.PostID, post.Title, post.Body, post.Publish });</P>
int rs = SqlHelper
<P>.ExecuteNonQuery(_ConnectionString,</P>
<P>CommandType</P>
<P>.StoredProcedure, spName, parameters);</P>
if
<P> (rs > 0)</P>
<P>{</P>
foreach (SqlParameter p in
<P> parameters)</P>
<P>{</P>
if (String.Compare(p.ParameterName, "@PostID", true
<P>) == 0)</P>
return (int
<P>)p.Value;</P>
<P>}</P>
<P>}</P>
return
<P> rs;</P>
<P>}</P>
public override int PostUpdate(Post
<P> post)</P>
<P>{</P>
return SqlHelper.ExecuteNonQuery(_ConnectionString, "Post_Update"
<P>,</P>
<P>post.PostID, post.Title, post.Body, post.Publish);</P>
<P>}</P>
public override int PostDelete(Post
<P> post)</P>
<P>{</P>
return SqlHelper.ExecuteNonQuery(_ConnectionString, "Post_Delete"
<P>,</P>
<P>post.PostID); </P>
<P>[email protected] </P>
<P>}</P>
<P>}</P>
<P>} </P><B>
<P>Nh*n xét: </P></B>
<P>27 </P>
<P>Xây d*ng thu vi*n Core * m*c t*ng quát </P>
<P>Các phuong th*c Update, Delete tuong d*i gi*ng nhau ch* khác ph*n spName</P>
<P>và các giá tr* tham s* </P>
<P>Các phuong th*c Find, All, Single tuong d*i gi*ng nhau ch* khác ph*n spName</P>
<P>và các giá tr* tham s*</P>
<B>
<P>Ð*t v*n d*:</P></B>
<P>Gi* s* b*n có thêm m*t b*ng n*a trong CSDL, thì các thao tác d*c/ghi cung tuong t*</P>
<P>nhu các trên. B*n ch* c*n "cóp dán" và s*a l*i nh*ng ph*n sai khác. Nhung b*n ph*i</P>
<P>làm gì khi b*n mu*n xây d*ng m*t *ng d*ng khác v*i CSDL hoàn toàn khác. B*n s*</P>
<P>ph*i vi*t l*i ho*c copy và s*a l*i m*t s* ph*n trong project DataAccess d* phù h*p v*i</P>
<P>project m*i</P>*
<P> m*t th*i gian. Do dó d* ti*t ki*m th*i gian xây d*ng *ng d*ng, b*n nên</P>
<P>xây d*ng m*t thu vi*n bao g*m các l*p th*c hi*n thao tác d*c, ghi d* li*u * m*c t*ng</P>
<P>quát (t*m d*t là Core) có th* dùng cho nhi*u project khác nhau. </P><B>
<P>Xây d*ng thu vi*n Core * m*c t*ng quát </P></B>
<P>1. T*o thu m*c * Desktop và d*t tên là LibCore</P>
<P>2. M* Microsoft Visual Studio (VS), t*o m*t project thu vi*n (Class Library Project) </P>
<P>và d*t tên là <B>Core</B> </P>
<P>[email protected] </P>
<B>
</B>
<P>28 </P>
<P>Xây d*ng thu vi*n Core * m*c t*ng quát </P>
<P>3. B*m chu*t ph*i References ch*n Add References, sau dó vào th* .NET d* thêm</P>
<P>2 thu vi*n sau </P>
<P>System.Configuration</P>
<P>System.Web </P>
<P>4. Xóa Class1 và copy 4 file sau vào project Core</P>
<P>SqlHelper.cs</P>
<P>CBO.cs</P>
<P>Null.cs</P>
<P>DataCache.cs </P><B>
</B>
<P>5. Thêm 2 class là DataProvider và SqlDataProvider vào project</P>
<P>6. Solution c*a b*n s* có 2 thu vi*n và các t*p tin nhu hình sau: </P><B>
</B>
<P>[email protected] </P>
<B>
</B>
<B>
<P>B* sung code cho l*p DataProvider </P></B>
<P>29 </P>
<P>Xây d*ng thu vi*n Core * m*c t*ng quát </P>
<P>using</P>
<P> System;</P>
<P>using</P>
<P> System.Data;</P>
<P>namespace</P>
<P> DataAccess</P>
<P>{</P>
public abstract class
<P>DataProvider</P>
<P>{</P>
public abstract object ExecuteNonQueryWithOutput(string outputParam,
<P>string</P>
<P>spName, </P>params object
<P>[] parameterValues);</P>
public abstract int ExecuteNonQuery(string spName, params object
<P>[]</P>
<P>parameterValues);</P>
public abstract DataSet ExecuteDataset(string spName, params object
<P>[]</P>
<P>parameterValues);</P>
public abstract IDataReader ExecuteReader(string spName, params object
<P>[]</P>
<P>parameterValues);</P>
public abstract object ExecuteScalar(string spName, params object
<P>[]</P>
<P>parameterValues);</P>
<P>}</P>
<P>} </P><B>
<P>B* sung code cho l*p SqlDataProvider </P></B>
<P>using</P>
<P> System;</P>
<P>using</P>
<P> System.Configuration;</P>
<P>using</P>
<P> Microsoft.ApplicationBlocks.Data;</P>
<P>using</P>
<P> System.Data;<B> </P></B>
<P>[email protected] </P>
<P>30 </P>
<P>Xây d*ng thu vi*n Core * m*c t*ng quát </P>
<P>using</P>
<P> System.Data.SqlClient;</P>
<P>namespace</P>
<P> DataAccess</P>
<P>{</P>
public class SqlDataProvider :
<P>DataProvider</P>
<P>{</P>
private string
<P> connectionString;</P>
public SqlDataProvider(string
<P> connectionStringName)</P>
<P>{</P>
this
<P>.connectionString =</P>
<P>ConfigurationManager</P>
<P>.ConnectionStrings[connectionStringName].ConnectionString;</P>
<P>}</P>
public override int ExecuteNonQuery(string spName, params object
<P>[]</P>
<P>parameterValues)</P>
<P>{</P>
return SqlHelper
<P>.ExecuteNonQuery(connectionString, spName,</P>
<P>parameterValues);</P>
<P>}</P>
public override object ExecuteNonQueryWithOutput(string outputParam,
<P>string</P>
<P>spName, </P>params object
<P>[] parameterValues)</P>
<P>{</P>
SqlParameter
<P>[] parameters =</P>
<P>SqlHelperParameterCache</P>
<P>.GetSpParameterSet(connectionString, spName);</P>
<P>AssignParameterValues(parameters, parameterValues);</P>
int rs = SqlHelper
<P>.ExecuteNonQuery(connectionString,</P>
<P>CommandType</P>
<P>.StoredProcedure, spName, parameters);</P>
if (rs > 0 && !string
<P>.IsNullOrEmpty(outputParam))</P>
<P>{</P>
foreach (var item in
<P> parameters)</P>
if (String.Compare(item.ParameterName, outputParam, true
<P>) == 0)</P>
return
<P> item.Value;</P>
<P>}</P>
return null
<P>;</P>
<P>}</P>
public override IDataReader ExecuteReader(string spName, params object
<P>[]</P>
<P>parameterValues)</P>
<P>{</P>
return SqlHelper
<P>.ExecuteReader(connectionString, spName,</P>
<P>parameterValues);</P>
<P>}</P>
public override DataSet ExecuteDataset(string spName, params object
<P>[]</P>
<P>parameterValues)</P>
<P>{</P>
return SqlHelper
<P>.ExecuteDataset(connectionString, spName,</P>
<P>parameterValues);</P>
<P>}</P>
public override object ExecuteScalar(string spName, params object
<P>[]</P>
<P>parameterValues) </P>
<P>[email protected] </P>
<P>31 </P>
<P>Xây d*ng thu vi*n Core * m*c t*ng quát </P>
<P>{</P>
return SqlHelper
<P>.ExecuteScalar(connectionString, spName,</P>
<P>parameterValues);</P>
<P>}</P>
private void AssignParameterValues(SqlParameter[] commandParameters, object
<P>[]</P>
<P>parameterValues)</P>
<P>{</P>
if ((commandParameters == null) || (parameterValues == null
<P>))</P>
return
<P>;</P>
if
<P> (commandParameters.Length != parameterValues.Length)</P>
throw new ArgumentException(
<P>"Parameter count does not match Parameter</P>
<P>Value count."</P>
<P>);</P>
for (int
<P> i = 0, j = commandParameters.Length; i < j; i++)</P>
<P>commandParameters[i].Value = parameterValues[i];</P>
<P>}</P>
<P>}</P>
<P>} </P><B>
<P>B* sung ti*p code cho l*p DataProvider </P></B>
<P>private</P> static DataProvider instance = null
<P>;</P>
<P>public</P> static DataProvider
<P> Instance</P>
<P>{</P>
<P>get</P>
<P>{</P>
if (instance == null
<P>)</P>
<P>instance = </P>new SqlDataProvider("ConnectionString"
<P>);</P>
return
<P> instance;</P>
<P>}</P>
<P>} </P>
<P>Toàn b* code c*a l*p DataProvider nhu sau </P>
<P>using</P>
<P> System;</P>
<P>using</P>
<P> System.Data;</P>
<P>namespace</P>
<P> DataAccess</P>
<P>{</P>
public abstract class
<P>DataProvider</P>
<P>{</P>
private static DataProvider instance = null
<P>;</P>
public static DataProvider
<P> Instance</P>
<P>{</P>
<P>get</P>
<P>{</P>
if (instance == null
<P>)</P>
<P>instance = </P>new SqlDataProvider("ConnectionString"
<P>);</P>
return
<P> instance;</P>
<P>}</P>
<P>}</P>
<P>[email protected] </P>
<P>32 </P>
<P>Xây d*ng thu vi*n DataAccess s* d*ng thu vi*n Core.dll </P>
public abstract object ExecuteNonQueryWithOutput(string outputParam,
<P>string</P>
<P>spName, </P>params object
<P>[] parameterValues);</P>
public abstract int ExecuteNonQuery(string spName, params object
<P>[]</P>
<P>parameterValues);</P>
public abstract DataSet ExecuteDataset(string spName, params object
<P>[]</P>
<P>parameterValues);</P>
public abstract IDataReader ExecuteReader(string spName, params object
<P>[]</P>
<P>parameterValues);</P>
public abstract object ExecuteScalar(string spName, params object
<P>[]</P>
<P>parameterValues);</P>
<P>}</P>
<P>} </P>
<P>B*m Ctrl + Shilft + B d* build project, sau dó vào thu m*c bin d* l*y file thu vi*n</P>
<P>Core.dll </P><B>
<P>Xây d*ng thu vi*n DataAccess s* d*ng thu vi*n Core.dll </P></B>
<P>1. Vào menu File > Add > New Project vào t*o m*t project thu vi*n (Class Library</P>
<P>Project) và d*t tên là <B>DataAccess</B> </P><B>
</B>
<P>[email protected] </P>
<P>33 </P>
<P>Xây d*ng thu vi*n DataAccess s* d*ng thu vi*n Core.dll </P>
<P>2. Xóa Class1 và thêm thu vi*n Core vào project DataAccess.</P>
<P>3. T*o l*p Post nhu sau </P>
<P>using</P>
<P> System;</P>
<P>using</P>
<P> System.Collections.Generic;</P>
<P>using</P>
<P> Core;</P>
<P>namespace</P>
<P> DataAccess</P>
<P>{</P>
public class
<P>Post</P>
<P>{</P>
public int PostID { get; set
<P>; }</P>
public string Title { get; set
<P>; }</P>
public string Body { get; set
<P>; }</P>
public DateTime? Publish { get; set
<P>; }</P>
public
<P> Post() { }</P>
public static List<Post
<P>> All()</P>
<P>{</P>
return CBO.FillCollection<Post
<P>>( </P>
<P>DataProvider</P>.Instance.ExecuteReader("Post_All"
<P>));</P>
<P>}</P>
public static List<Post> Find(string
<P> title)</P>
<P>{</P>
return CBO.FillCollection<Post
<P>>( </P>
<P>DataProvider</P>.Instance.ExecuteReader("Post_Find"
<P>, title));</P>
<P>}</P>
public static Post Single(string
<P> postId)</P>
<P>{</P>
<P>try</P>
<P>{</P>
return CBO.FillObject<Post
<P>>( </P>
<P>DataProvider</P>.Instance.ExecuteReader("Post_Single"
<P>,</P>
Convert
<P>.ToInt32(postId)));</P>
<P>}</P>
catch (Exception
<P>)</P>
<P>{</P>
return null
<P>;</P>
<P>}</P>
<P>}</P>
public static int
<P> Count()</P>
<P>{</P>
object rs = DataProvider.Instance.ExecuteScalar("Post_Count"
<P>);</P>
return Convert
<P>.ToInt32(rs);</P>
<P>}</P>
public static int Add(Post
<P> data)</P>
<P>{ </P>
<P>[email protected] </P>
<P>34 </P>
<P>Xây d*ng thu vi*n DataAccess s* d*ng thu vi*n Core.dll </P>
object
<P> rs =</P>
<P>DataProvider</P>.Instance.ExecuteNonQueryWithOutput("@PostID", "Post_Add"
<P>,</P>
<P>data.PostID, data.Title, data.Body, data.Publish);</P>
int identity = rs != null ? Convert
<P>.ToInt32(rs) : 0;</P>
return
<P> identity;</P>
<P>}</P>
public static bool Update(Post
<P> data)</P>
<P>{</P>
int rs = DataProvider.Instance.ExecuteNonQuery("Post_Update"
<P>,</P>
<P>data.PostID, data.Title, data.Body, data.Publish);</P>
return
<P> rs > 0;</P>
<P>}</P>
public static bool Delete(string
<P> postId)</P>
<P>{</P>
<P>try</P>
<P>{</P>
int rs = DataProvider.Instance.ExecuteNonQuery("Post_Delete"
<P>,</P>
Convert
<P>.ToInt32(postId));</P>
return
<P> rs > 0;</P>
<P>}</P>
catch (Exception
<P>)</P>
<P>{</P>
return false
<P>;</P>
<P>}</P>
<P>}</P>
<P>}</P>
<P>}</P>
<P> </P>
<P>4. T*o ti*p project Console và test nhu vi d* trên </P>
<P>[email protected] </P>
<B>
<P>Luy*n t*p </P></B>
<P>1. T*o b*ng nhu sau, thi*t l*p c*t Id có giá tr* id t* tang, d*t tên là Movie </P>
<P>2. Thêm d* li*u cho b*ng <B>Movie</B> </P>
<P>M* file <B>SrciptSQL/Movie_Data.sql</B>, ch*n database ch*a b*ng Movie<B> </B>sau dó b*m</P><B>
<P>Execute</B> </P>
<P>Database ch*a b*ng Movie </P>
<P>3. T*o thu vi*n Data Access nhu ph*n tru*c d* g*i các th* t*c sau (t*o ti*p l*p </P>
<P>Movie và b* sung các phuong th*c vào project * trên). Sau m*i phuong th*c</P>
<P>ph*i test k*t qu* trong project Consolde </P>
<P>[email protected] </P>
<P>35 </P>
<P>Luy*n t*p </P>
<P>Movie_All</P>
<P>Movie _Single</P>
<P>Movie _Find</P>
<P>Movie _Add</P>
<P>Movie _Update</P>
<P>Movie _Delete</P>
<P>Movie _Count</P>
<P><B>Movie_Paging </P></B>
<P>Hu*ng d*n làm th* t*c Movie_Paging </P>
<P>1. B*m vào <B>New Query</B>, gõ l*n lu*t các câu truy v*n sau và xem k*t qu*</P>
<B>
<P>Câu 1</B>: thêm m*t c*t s* th* t* vào b*ng k*t qu* ( hàm </P>ROW_NUMBER()
<P>ch* s* </P>
<P>d*ng * các b*n SQL Server 2005 tr* lên) </P>
<P>SELECT</P> ROW_NUMBER() OVER (ORDER BY Id) AS Row,
<P> Title</P>
<P>FROM</P>
<P> Movie </P>
<B>
<P>Câu 2</B></P>: l*y các k*t qu* t* hàng th* 6 *
<P> 10 </P>
<P>SELECT</P> Row,
<P> Title</P>
<P>FROM</P>
<P>(</P>
SELECT ROW_NUMBER() OVER (ORDER BY Id) AS Row,
<P> Title</P>
FROM
<P> Movie</P>
<P>)</P> AS
<P> MoviesWithRowNumbers </P>
<P>[email protected] </P>
<P>36 </P>
<P>Luy*n t*p </P>
<P>WHERE</P> Row >= 6 AND Row <=
<P> 10 </P>
<B>
<P>Câu 3</B>: t*o m*t b*ng t*m (<B>@Movies</B>) có kèm theo c*t th* t* d* luu d* li*u t*m</P>
<P>vào dó, sau dó l*y các hàng t* 6 </P>* 10 trong b*ng t*m *
<P> cách làm này g*i là phân</P>
<P>trang (Paging) </P>
<P>S* d*ng SQL 2005 tr* di </P>
<P>-- declare a new TABLE variable</P>
<P>DECLARE</P> @Movies
<P>TABLE</P>
<P>(</P>
<P>RowNumber </P>INT
<P>,</P>
<P>Id </P>INT
<P>,</P>
<P>Title </P>NVARCHAR(100
<P>)</P>
<P>)</P>
<P>-- populate the table variable with the complete list of products</P>
<P>INSERT</P> INTO
<P> @Movies</P>
<P>SELECT</P> ROW_NUMBER() OVER (ORDER BY Movie.Id) AS Row
<P>,</P>
<P>Id</P>,
<P> Title</P>
<P>FROM</P>
<P> Movie</P>
<P>-- extract the requested page of products</P>
<P>SELECT</P> * FROM
<P> @Movies</P>
<P>WHERE</P> RowNumber >= 6 AND RowNumber <=
<P> 10 </P>
<P>S* d*ng SQL Server 2000 </P>
<P>DECLARE</P> @Movies
<P>TABLE</P>
<P>(</P>
<P>RowNumber </P>INT IDENTITY (1,1
<P>),</P>
<P>Id </P>INT
<P>,</P>
<P>Title </P>NVARCHAR(100
<P>)</P>
<P>)</P>
<P>-- populate the table variable with the complete list of products</P>
<P>INSERT</P> INTO
<P> @Movies</P>
<P>SELECT</P> Id,
<P> Title</P>
<P>FROM</P>
<P> Movie</P>
<P>-- extract the requested page of products</P>
<P>SELECT</P> * FROM
<P> @Movies</P>
<P>WHERE</P> RowNumber >= 6 AND RowNumber <=
<P> 10 </P>
<P>[email protected] </P>
<P>37 </P>
<P>Luy*n t*p </P>
<P>K*t qu* t* câu truy v*n 3 (ch* l*y 3 c*t)</P>
<P> </P>
<P>Tuong t* nhu trên, vi*t thêm vào câu truy v*n 3 d* l*y d*y d* các c*t c*a b*ng </P>
<P>Movie</P><B>
<P>Câu 4: </B>L*y d* li*u t* dòng 1 </P>*
<P> 5 (trang 1, s* record t*i da trong 1 trang là 5) </P><B>
<P>DECLARE @PageNumber INT</P>
<P>DECLARE @PageSize INT</P></B></P>
<P>
<P><B>
<P>SET @PageNumber = 1 -- trang hien tai</P>
<P>SET @PageSize = 5 -- so record toi da cua mot trang</P></B></P>
<P>-- tao bang tam de chua du lieu</P></P>
<P>
<P>DECLARE</P> @Movies
<P>TABLE</P>
<P>(</P>
<P>RowNumber </P>INT
<P>,</P>
<P>Id </P>INT
<P>,</P>
<P>Title </P>NVARCHAR(100
<P>),</P>
<P>Director </P>NVARCHAR(100
<P>),</P>
<P>DateReleased </P>DATETIME
<P>,</P>
<P>InTheaters </P>BIT
<P>,</P>
<P>BoxOfficeTotals </P>MONEY
<P>,</P>
Description NVARCHAR(4000
<P>)</P>
<P>)</P>
<P>-- dua du lieu vao bang movie kem theo cot stt</P>
<P>INSERT</P> INTO
<P> @Movies</P>
<P>SELECT</P> ROW_NUMBER() OVER (ORDER BY Movie.Id) AS Row
<P>,</P>
<P>Id</P>, Title, Director, DateReleased, InTheaters, BoxOfficeTotals
<P>,</P>
<P>Description</P>
<P>FROM</P>
<P> Movie</P>
<P>-- tong so record cua bang tam @Movies</P>
<P> </P><B>
<P>SELECT COUNT(Id) FROM @Movies</P></B></P>
<P>
</P>
<P>
<P>-- lay cac record can thiet</P><B>
<P>SELECT * FROM @Movies </P>
<P>WHERE RowNumber > (@PageNumber -1) * @PageSize </P>
<P>AND RowNumber <= @PageNumber * @PageSize </P>
</B></P>
<P>
<P>S* d*ng SQL Server 2000 </P></P>
<P><B></P></B>
<P>
<P>
<P>[email protected] </P>
<P>38 </P>
<P>Luy*n t*p </P>
<P>DECLARE</P> @PageNumber
<P>INT</P>
<P>DECLARE</P> @PageSize
<P>INT</P>
<P>SET</P> @PageNumber = 1
<P>-- trang hien tai</P>
<P>SET</P> @PageSize = 5
<P>-- so record toi da cua mot trang</P>
<P>-- tao bang tam de chua du lieu</P>
<P>DECLARE</P> @Movies
<P>TABLE</P>
<P>(</P>
<P><B>RowNumber </P></B>INT IDENTITY (1,1
<P><B>),</P></B>
<P>Id </P>INT
<P>,</P>
<P>Title </P>NVARCHAR(100
<P>),</P>
<P>Director </P>NVARCHAR(100
<P>),</P>
<P>DateReleased </P>DATETIME
<P>,</P>
<P>InTheaters </P>BIT
<P>,</P>
<P>BoxOfficeTotals </P>MONEY
<P>,</P>
Description NVARCHAR(4000
<P>)</P>
<P>)</P>
<P>-- dua du lieu vao bang movie kem theo cot stt</P><B>
<P>INSERT INTO @Movies</P>
<P>SELECT Id, Title, Director, DateReleased, InTheaters, BoxOfficeTotals,</P>
<P>Description</P>
<P>FROM Movie </P></B>
<P>-- tong so record cua bang tam @Movies</P>
<P>SELECT</P> COUNT(Id) FROM
<P> @Movies</P>
<P>-- lay cac record can thiet</P>
<P>SELECT</P> * FROM
<P> @Movies</P>
<P>WHERE</P> RowNumber > (@PageNumber -1) *
<P> @PageSize </P>
<P>AND</P> RowNumber <= @PageNumber * @PageSize
<P><B> </P></B></P></P>
<P>K*t qu* truy v*n nhu sau </P>
<P>B*ng 1: t*ng s* record. T* t*ng s* record này, b*n có th* tính du*c t*ng s* </P>
<P>trang theo công th*c </P>
<P>So Trang = Làm tròn lên( TongRecord / PageSize ) </P>
<P>B*ng 2: ph*n d* li*u c*a trang 1 </P>
<P>
<P>[email protected] </P>
<P>39 </P>
<P>Luy*n t*p </P>
<B>
<P>Câu 5: </B>Tuong t* thay @PageNumber = 2 và xem k*t qu* </P><B>
<P>SET @PageNumber = 2 -- trang hien tai</P>
</B>
<P>Áp d*ng vi*t th* t*c <B>Movie_Paging</B> nhu sau </P>
<P>CREATE</P> PROCEDURE
<P> Movie_Paging</P>
<P>@PageNumber </P>INT
<P>,</P>
<P>@PageSize </P>
<P>INT</P>
<P>AS</P>
<P>-- tao bang tam de chua du lieu</P>
<P>DECLARE</P> @Movies
<P>TABLE</P>
<P>(</P>
<P>RowNumber </P>INT
<P>,</P>
<P>Id </P>INT
<P>,</P>
<P>Title </P>NVARCHAR(100
<P>),</P>
<P>Director </P>NVARCHAR(100
<P>),</P>
<P>DateReleased </P>DATETIME
<P>,</P>
<P>InTheaters </P>BIT
<P>,</P>
<P>BoxOfficeTotals </P>MONEY
<P>,</P>
Description NVARCHAR(4000
<P>)</P>
<P>)</P>
<P>-- dua du lieu vao bang movie kem theo cot stt</P>
<P>INSERT</P> INTO
<P> @Movies</P>
<P>SELECT</P> ROW_NUMBER() OVER (ORDER BY Movie.Id) AS Row
<P>,</P>
<P>Id</P>, Title, Director, DateReleased, InTheaters, BoxOfficeTotals
<P>,</P>
<P>Description</P>
<P>FROM</P>
<P> Movie</P>
<P>-- tong so record cua bang tam @Movies</P>
<P> </P><B>
<P>SELECT COUNT(Id) FROM @Movies</P></B></P>
<P>
</P>
<P>
<P>
<P>-- lay cac record can thiet</P><B>
<P>SELECT * FROM @Movies</P>
<P>WHERE RowNumber > (@PageNumber -1) * @PageSize </P>
<P>AND RowNumber <= @PageNumber * @PageSize </P></B>
<P>SQL Server 2000 </P>
<P>CREATE</P> PROCEDURE
<P> Movie_Paging</P>
<P>@PageNumber </P>INT
<P>,</P>
<P>@PageSize </P>
<P>INT</P>
<P>AS</P>
<P>-- tao bang tam de chua du lieu</P>
<P>DECLARE</P> @Movies
<P>TABLE</P>
<P>(</P>
<P>RowNumber </P>INT IDENTITY (1,1
<P>),</P>
<P>Id </P>INT
<P>,</P>
<P>Title </P>NVARCHAR(100
<P>),</P>
<P>Director </P>NVARCHAR(100
<P>),</P>
<P>DateReleased </P>DATETIME
<P>, </P>
<P>[email protected] </P>
<P>40 </P>
<P>Luy*n t*p </P>
<P>InTheaters </P>BIT
<P>,</P>
<P>BoxOfficeTotals </P>MONEY
<P>,</P>
Description NVARCHAR(4000
<P>)</P>
<P>)</P>
<P>-- dua du lieu vao bang movie kem theo cot stt</P>
<P>INSERT</P> INTO
<P> @Movies</P>
<P>SELECT</P> Id, Title, Director, DateReleased, InTheaters, BoxOfficeTotals
<P>,</P>
<P>Description</P>
<P>FROM</P>
<P> Movie</P>
<P>-- tong so record cua bang tam @Movies</P>
<P>SELECT</P> COUNT(Id) FROM
<P> @Movies</P>
<P>-- lay cac record can thiet</P>
<P>SELECT</P> * FROM
<P> @Movies</P>
<P>WHERE</P> RowNumber > (@PageNumber -1) *
<P> @PageSize </P>
<P>AND</P> RowNumber <= @PageNumber *
<P> @PageSize </P>
<P>Vi*t phuong th*c Paging trong l*p Movie nhu sau </P>
<P>public</P> static List<Movie> Paging(int page, int pageSize, out int
<P> howManyPages)</P>
<P>{</P>
IDataReader reader = null
<P>; </P>
<P>try</P>
<P>{ </P>
<P>reader = </P>DataProvider
<P>.Instance.ExecuteReader(</P>
"Movie_Paging"
<P>, page, pageSize);</P>
//lay du lieu tu bang 1
<P> </P>
<P>reader.Read();</P>
<P>//tinh so trang</P>
<P>howManyPages = (</P>int)Math.Ceiling((double
<P>)reader.GetInt32(0) / </P>
<P>(</P>double
<P>)pageSize);</P>
<P>//lay du lieu tu bang 2</P>
<P> </P>
<P>reader.NextResult();</P>
return CBO.FillCollection<Product
<P>>(reader); </P>
<P>}</P>
<P>catch</P>
<P>{ </P>
<P>// dam bao ket noi duoc dong neu co loi xay ra</P>
<P>if</P> (reader != null && reader.IsClosed == false
<P>) </P>
<P>reader.Close();</P>
<P>// so trang = 0</P>
<P>howManyPages = 0; </P>
<P>// tra ve ket qua la mot ds rong</P>
<P>return</P> new List<Product
<P>>(); </P>
<P>}</P>
<P>}</P>
<P> </P>
<P>[email protected] </P>
<P>41 </P>
<P>Luy*n t*p </P>
</P></P>
Bạn đang đọc truyện trên: AzTruyen.Top