Documentos de Académico
Documentos de Profesional
Documentos de Cultura
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.
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
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
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.
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()