Theory
રાત જેમાં bills survive થાય છે
આજે graduation છે: cashier Save પર click કરે છે, ShopKeeper bill ને ShopDB માં લખે છે, power ગમે તેટલું flicker થઈ શકે છે, અને બીજે દિવસે day report everything ને back read કરે છે.
તમારી પાસે already દરેક part છે: tested connection string, 5-object model, BCA205 નું SQL, Unit 3 નું Try/Catch, અને DataSource ની waiting DataGrid. આ lesson ફક્ત ASSEMBLES કરે છે: ત્રણ code shapes (read connected, write, fill disconnected) જે between them દરેક ADO.NET exam program ને cover કરે છે જે આ paper sets કરે છે.
Practical
Shape 1 અને 2: connected read, પછી write
Imports System.Data.SqlClient
Module BillStore
Dim cs As String = "Data Source=.;Initial Catalog=ShopDB;Integrated Security=True"
Sub ReprintBill(ByVal billNo As Integer)
Dim cn As New SqlConnection(cs)
Try
cn.Open() ' dial
Dim cmd As New SqlCommand( _
"SELECT Item, Amount FROM Bills WHERE BillNo = " & billNo, cn)
Dim rd As SqlDataReader = cmd.ExecuteReader()
While rd.Read() ' one row per pass
MsgBox(rd("Item") & " Rs " & rd("Amount"))
End While
rd.Close()
Catch ex As Exception
MsgBox("Database error: " & ex.Message)
Finally
cn.Close() ' always hang up
End Try
End Sub
Sub SaveLine(ByVal item As String, ByVal amount As Decimal)
Dim cn As New SqlConnection(cs)
cn.Open()
Dim ins As New SqlCommand( _
"INSERT INTO Bills(Item, Amount) VALUES('" & item & "', " & amount & ")", cn)
Dim rows As Integer = ins.ExecuteNonQuery() ' 1 row affected
cn.Close()
End Sub
End Module
Theory
read ને reading
ReprintBill ના middle ને walk કરો:
- ExecuteReader() SELECT ને send કરે છે અને live SqlDataReader ને return કરે છે
- rd.Read() next row પર advance કરે છે, rows remain હોય ત્યાં સુધી True answer કરે છે: natural While loop; દરેક column ને name દ્વારા reachable છે,
rd("Item") - forward-only એટલે કોઈ second pass નહીં: rows ને re-walk કરવા, re-execute
અને note કરો કે Close() ક્યાં sits કરે છે: connection Finally માં closes થાય છે, એટલે even mid-read exception line ને hang up કરે છે. Unit 3 ની grammar, Unit 5 ના resource ને guarding: આ pairing એ છે જે examiners good practice કહે છે અને accordingly mark કરે છે.
Practical
Shape 3: disconnected fill
Sub LoadDayReport()
Dim cn As New SqlConnection(cs)
Dim da As New SqlDataAdapter("SELECT * FROM Bills", cn)
Dim ds As New DataSet()
da.Fill(ds, "Bills") ' cn ને automatically opens AND closes કરે છે
dgvBill.DataSource = ds.Tables("Bills") ' Unit 3 નું glass, filled
' owner grid માં rows ને edit કરે છે... later:
' da.Update(ds, "Bills") ' changes ને ShopDB માં back push કરે છે
End Sub
Sub ShowDayTotal()
Dim cn As New SqlConnection(cs)
cn.Open()
Dim cmd As New SqlCommand("SELECT SUM(Amount) FROM Bills", cn)
Dim total As Decimal = CDec(cmd.ExecuteScalar()) ' એક single value
cn.Close()
MsgBox("Today: Rs " & total)
End Sub
Quiz
Dim rows As Integer = ins.ExecuteNonQuery() single-row INSERT ને successfully run કરે છે. rows માં શું છે, અને ExecuteNonQuery શા માટે built હતું?
- 1: તે affected rows ની number ને return કરે છે, અને INSERT/UPDATE/DELETE માટે exists કરે છે
- 0: તે success flag ને return કરે છે જ્યાં 0 એટલે no error
- નવું row નું data: તે whatever statement ને produce કર્યું તે return કરે છે
- તે INSERT ને run નથી કરી શકતું: ફક્ત SELECT statements ADO.NET માં execute થાય છે
Show the answer
1: તે affected rows ની number ને return કરે છે, અને INSERT/UPDATE/DELETE માટે exists કરે છે
ExecuteNonQuery એ verb છે SQL માટે જે rows ને return કરવાને બદલે data ને CHANGES કરે છે, અને તેનો Integer answer touched rows ને count કરે છે: આ insert માટે 1, possibly dozens broad UPDATE માટે, 0 એટલે કંઈ match નથી થયું (જે option B success તરીકે misread કરે છે: 0 ઘણીવાર signal કરે છે કે તમારા WHERE clause એ કોઈ ને find નથી કર્યું). Row-returning work ExecuteReader ને belong કરે છે, single values ExecuteScalar ને: 3 executes ને તેમના SQL kinds સાથે match કરવું એ આ unit નો most reliable exam question છે. real code માં તે count ને checking કરવું silent failures ને catch કરે છે.
Think first
કોણે Fill માટે connection ને open કર્યું?
LoadDayReport ક્યારેય cn.Open() અથવા cn.Close() ને call નથી કરતું, પણ તે work કરે છે અને કંઈ leak નથી કરતું. ReprintBill એ બંને ને do કરવું પડ્યું. difference ને explain કરો tap કરતા પહેલા.
Show the answer
DataAdapter.Fill connection ને itself manage કરે છે: તે closed find કરે છે, તે open કરે છે, rows ને DataSet માં pull કરે છે, અને ફરી close કરે છે: એક self-contained trip, જે exactly disconnected philosophy છે. READER એટલું નથી કરી શકતું: તે live line પર stream કરે છે, એટલે line ને તમે Read() કરો ત્યાં સુધી open રહેવું જોઈએ, અને closing તમારી responsibility બને છે (એટલે Finally). exam sentence: Fill connection ને automatically open અને close કરે છે; DataReader ને તેના whole lifetime માટે explicitly open connection જોઈએ છે.
Watch out
Habits જે pass ને distinction થી separate કરે છે
Unclosed connections: દરેક એક database slot ને hold કરે છે; busy till એમને leak કરે છે જ્યાં સુધી server refuse ન કરે. Finally માં Close કરો, દરેક path.
user text ને SQL સાથે Gluing કરવું: આપણા teaching listings readability માટે concatenate કરે છે, પણ O'Brien નામ નું item string ને break કરે છે, અને worse શક્ય છે: professional fix અને examiners nod તરીકે parameterised commands (cmd.Parameters) ને mention કરો.
Wrong execute: INSERT પર ExecuteReader, SELECT પર ExecuteNonQuery: બંને run થાય છે, બંને useless results આપે છે. verb ને SQL સાથે match કરો.
Theory
ShopKeeper, complete
પાછળ જુઓ: Windows Forms face (Unit 3), Items, Bills અને GST-polymorphic Products નો OOP heart (Unit 4), અને હવે રાત ને survive કરતી memory (Unit 5), બધું CLR machinery પર running જે તમે end to end narrate કરી શકો છો (Units 1 અને 2). એ આખો BCA404 syllabus છે જે એક till માં living છે. જ્યારે BCA503-01 તમને later server-side data access આપે છે, આ exact object model new names સાથે returns થાય છે, અને તમે એને old supplier જેમ greet કરશો.
Summary
Key takeaways
- Connected read: Open, Command + ExecuteReader, While rd.Read() rd("Column") સાથે, Finally માં Close.
- Writes: INSERT/UPDATE/DELETE માટે ExecuteNonQuery, affected-row count ને return કરે છે (single insert માટે 1; 0 ઘણીવાર એટલે WHERE કંઈ match નથી થયું).
- Single values: ExecuteScalar (SUM, COUNT): એક call, એક result.
- Disconnected: DataAdapter.Fill(ds, "Bills") તમારા માટે opens AND closes કરે છે; ds.Tables("Bills") ને bind કરો; Update(ds) edits ને sync કરે છે.
- database work ને Try/Catch/Finally માં wrap કરો; error path પર ક્યારેય connection ને open છોડો નહીં.
- real code માં concatenated SQL ઉપર parameterised commands ને prefer કરો.
- Memory hook: readers line ને hold કરે છે, adapters trip ને make કરે છે, counts confess કરે છે કે શું થયું.