Está en la página 1de 5

Homework Title /No:4 Course Code: CAP407

Course Instructor: Ram Singh Course Tutor (if applicable):

Date of Allotment: Date of submission:

Student’s Roll No: Rtb902A16 Section No: TB902

Declaration:
I declare that this assignment is my individual work. I have not copied from any
other student’s work or from any other source except where due
acknowledgement is explicated the teacher has any part been written for me by
another person.

Student’s Signature: Akshey kumar


Evaluator’s comments:

Marks obtained: out of


PART A
1. Write a program to read, write and edit your personal information in a text file.

Ans. Dim oFile as System.IO.File


Dim oWrite as System.IO.StreamWriter
oWrite = oFile.CreateText(“C:\sample.txt”)
OpenText

The OpenText method opens an existing text file for reading and returns a
System.IO.StreamReader object. With the StreamReader object, you can then read the file.
Let’s see how to open a text file for reading:
Dim oFile as System.IO.File
Dim oRead as System.IO.StreamReader
oRead = oFile.OpenText(“C:\sample.txt”)
Writing to a text file

The methods in the System.IO.StreamWriter class for writing to the text file are Write and
WriteLine. The difference between these methods is that the WriteLine method appends a
newline character at the end of the line while the Write method does not. Both of these
methods are overloaded to write various data types and to write formatted text to the file. The
following example demonstrates how to use the WriteLine method:
oWrite.WriteLine(“Write a line to the file”)
oWrite.WriteLine() ‘Write a blank line to the file
Formatting the output

The Write and WriteLine methods both support formatting of text during output. The ability
to format the output has been significantly improved over previous versions of Visual Basic.
There are several overloaded methods for producing formatted text. Let’s look at one of these
methods:
oWrite.WriteLine(“{0,10}{1,10}{2,25}”, “Date”, “Time”, “Price”)
oWrite.WriteLine(“{0,10:dd MMMM}{0,10:hh:mm tt}{1,25:C}”, Now(), 13455.33)
oWrite.Close()

2. Write a program to read, write and edit your personal information in a binary
file.
Imports System.IO
'NameSpace required to be imported to work with files
Public Class Form1 Inherits System.Windows.Forms.Form
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e_
As System.EventArgs) Handles MyBase.Load
Dim fs as New FileStream("file.doc", FileMode.Create, FileAccess.Write)
Dim s as new StreamWriter(fs)
s.BaseStream.Seek(0,SeekOrigin.End)
s.WriteLine("This is an example of using file handling concepts in VB .NET.")
s.WriteLine("This concept is interesting.")
s.Close()
'closing the file
End Sub
End Class……..
Public Class Form1 Inherits System.Windows.Forms.Form
Private Sub Button1_Click(ByVal....., Byval.....)Handles Button1.Click
Dim fs as New FileStream("file.doc", FileMode.Create, FileAccess.Write)
Dim s as new StreamWriter(fs)
s.WriteLine("This is an example of using file handling concepts in VB .NET.")
s.WriteLine("This concept is interesting.")
'writing text to the newly created file
s.Close()
'closing the file

fs=New FileStream("file.doc",FileMode.Open,FileAccess.Read)
Dim d as new StreamReader(fs)
d.BaseStream.Seek(0,SeekOrigin.Begin)
while d.peek()>-1
RichTextbox1.Text &= d.readLine()
End while
d.close()
End Sub
3. Write a program to show student data in gridview control by selecting a
student rollno in combobox control

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As


System.EventArgs) Handles MyBase.Load
da.SelectCommand = New OleDbCommand
da.SelectCommand.Connection = Conn
da.SelectCommand.CommandText = "select * from master"
da.SelectCommand.CommandType = CommandType.Text
Conn.Open()
da.SelectCommand.ExecuteNonQuery()

da.Fill(ds, "master")
Conn.Close()
MessageBox.Show("One record Added Successfully")
dt = ds.Tables("master")
DataGridView1.DataSource = dt
DataGridView1.Refresh()
Combobox1. DataSource = dt

PART B

4. What is the difference between DataTable and DataSet.


Ans. DataReader
1. Its an connection oriented, whenever you want fetch the data from database that you need
the connection. after fetch the data connection is diconnected.
2. Its an Read only format, you cann't update records.

DataSet
1. Its connectionless. whenever you want fetch data from database. its connects indirectly to
the database and create a virtual database in local system. then disconnected from database.
2. Its easily read and write data from virtual database.

DataTable
A DataTable object represents a single table in the database. It has a name rows and columns.
There is not much difference between dataset and datatable, dataset is just the collection of
datatables.

5. Write a program to insert and delete records from a table


Ans. Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button2.Click

da.InsertCommand = New OleDbCommand


da.InsertCommand.Connection = Conn
da.InsertCommand.CommandText = "insert into master(customer_name,
customer_id, Address,phone_no,E-mail) values ('" & TextBox1.Text & "', " &
TextBox2.Text & ",' " & TextBox3.Text & "','" & TextBox4.Text & "','" &
TextBox5.Text & "')"
da.InsertCommand.CommandType = CommandType.Text
Conn.Open()
da.InsertCommand.ExecuteNonQuery()
da.Fill(ds, "master")
Conn.Close()
MessageBox.Show("One record Added Successfully")
dt = ds.Tables("master")
DataGridView1.DataSource = dt
DataGridView1.Refresh()
da.InsertCommand = New OleDbCommand
da.DeleteCommand.Connection = Conn
da. DeleteCommand.CommandText = "Delete from mater where
id="&TextBox2.Text
da. DeleteCommand.CommandType = CommandType.Text
Conn.Open()
da. DeleteCommand.ExecuteNonQuery()
da.Fill(ds, "master")
Conn.Close()
MessageBox.Show("One record Delete Successfully")
dt = ds.Tables("master")
DataGridView1.DataSource = dt
DataGridView1.Refresh()

6. Write a program to create a query editor using textbox control. Show result of
query in gridview control when user presses ExecuteQuery button on form.
Ans.
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
da.SelectCommand = New OleDbCommand
da.SelectCommand.Connection = Conn
da.SelectCommand.CommandText = TextBox2.Text

da.SelectCommand.CommandType = CommandType.Text
Conn.Open()
da.SelectCommand.ExecuteNonQuery()

da.Fill(ds, 0)
Conn.Close()
MessageBox.Show("One record Added Successfully")
dt = ds.Tables("master")
DataGridView1.DataSource = dt
DataGridView1.Refresh()

También podría gustarte