Vb. Graphic processing in the programming of net database

Source: Internet
Author: User
Tags button type count dba implement integer web database
Programming | data | database | Graphics Storing graphics file names in the database is a simple and effective way to deal with graphics in vb.net database programming. This article describes how to use this method in vb.net to design windows and Web graphics library programs.

Tags: vb.net, database, graphics, Windows programs, Web programs.
With the development of multimedia technology, graphic database has become more and more widely used in practical applications. One of the ways to handle graphics in vb.net database programming is to store graphics as a field in a database; the second method is to save the file name of the graphic as a field in the database, while a single picture is placed in the same folder as a separate file in the database. Although the first method can better protect the graphics file, but the writing program is more troublesome and has a certain degree of difficulty; the second method is an easy way to deal with graphics, which is also the method introduced in this article.

A simple method of processing graphics in 1:windows database program

To illustrate the problem, we first create a database in Access2000 (assuming its filename is Star.mdb), which has a data table named Star, with fields in the datasheet: number, name, sex, age, file name. The program runs as shown in Figure 1, where you can browse not only records, but also edit, add, and delete records. Because the file name of the graphic is used when editing and adding a record, it's a good idea to make it easier to manipulate the file name of the graphic to the same number as the record.

Figure 1Windows Program

Figure 2 Web Programs

To implement the functionality shown in Figure 1, we can do this:
①: Create a new Windows application in vb.net;

②: From the Data tab of the Toolbox, drag and drop oledbdataadapter onto the form, follow the on-screen prompts to establish a data connection, and match the data adapter.

③: Right-click on the OleDbDataAdapter1 below the form and select Generate DataSet.

At this point in the form below you can see three of the controls that are invisible when the program is running: OleDbDataAdapter1, OleDbConnection1, DataSet11, for the sake of writing the program, please rename them separately: Dba, DBC, Ds1.

④: Add controls such as text boxes, picture frames, command buttons, and so on on the form, and bind the text box to the corresponding field in the database by pressing F4 on the text box, the Properties window, and selecting text in DataBindings under the data item in the window. At this point you can see all the fields in the datasheet and select a field.

After doing this, we will write code to implement the program's functionality. First, define a subroutine (assuming its name is jth), and its function is to display the picture corresponding to the current record:

Dim N as Integer ' n indicates the position of the current record
Sub Jth () ' Custom subroutine
n = me.bindingcontext (Ds1, "Star"). Position ' Ds1 is the name of the dataset
Dim Str as String
str = ".. \ "+ ds1.tables (0). Rows (n) ("filename") ' filename ' is a field in the database
pictureBox1.Image = Image.FromFile (str)
End Sub

The form Load event corresponds to the program:

Me.Dbc.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;" & _
"Data source=. \star.mdb "' Use code to specify the connection to the database
Ds1.clear ()
Dba.fill (DS1, "star") ' fills the dataset
Jth ()

The following code for the Record command button is:

Me.BindingContext (DS1, "Star"). Position + 1
Jth ()

The corresponding code for the Record command button is:

Me.BindingContext (DS1, "Star"). Position-= 1;
Jth ()

The code for the first record command button is:

Me.BindingContext (DS1, "Star"). Position = 0;
Jth ()

The code for the last Record command button is:

Me.BindingContext (DS1, "Star"). Position=me.bindingcontext (DS1, "Star"). Count-1
Jth ()

The "Add" command button corresponds to a program that:

Me.BindingContext (DS1, "Star"). Endcurrentedit () ' Clears the current edit content
Me.BindingContext (DS1, "Star"). AddNew ()
pictureBox1.Image = Nothing
Catch Eendedit as System.Exception
System.Windows.Forms.MessageBox.Show (Eendedit.message)
End Try

The program for the Delete command button is:

If (Me.BindingContext (Ds1, "Star"). Count > 0) Then
Me.BindingContext (DS1, "Star"). RemoveAt (Me.BindingContext (Ds1, "Star"). Position)
End If

After you add and delete operations, you must click Update to truly implement updates to the database. The program for the UPDATE command button is:

Me.BindingContext (DS1, "Star"). Endcurrentedit ()
Dba.update (DS1, "Star")

The method of simple graphics processing in 2:web database program

In the ASP era, we can use visual InterDev6.0 to design Web programs. When ASP.net is coming to us, we can use vb.net to design a new generation of Web programs (ASP. NET program). Although you can use a text editor, such as Notepad, to write asp.net scripts, this method is very demanding for programmers, even the master programmer often finds it difficult to find errors in the program because of misspelled English words. Using vb.net to design Web programs has the advantages of less code and automatic error checking, which is certainly a boon for Web programmers.

Figure 2 shows a Web program designed with VB.net. The database used by this program is exactly the same as the previous one. When a viewer clicks on a person's name, it displays his picture on the right, and if you want to see the rest of the record, you can click the page link. We refer to the column in which the name is chosen as the selection column. This procedure is very common on the Internet. To implement the functionality shown in Figure 2, first create a new ASP.net Web application in vb.net → drag OleDbDataAdapter from the Toolbox Data tab onto the form. Then follow the on-screen prompts to configure the data adapter and generate the DataSet → Add the datagrid that displays the data to the form, right-click on the DataGrid, and select Property Builder to see the window shown in Figure 3. Select the general in Figure 3 to set the data source to the dataset in the form DS1, set the data member to the data table star in the database, and set the Data key field to the primary key in the database (in our database, the Number field).

Figure 3 DataGrid Property Builder

To implement the paging browsing record, select "Paging" in Figure 3, and the window shown in Figure 4 appears. Please select "Allow paging" in Figure 4 and determine the size of each page (that is, the number of rows per display, for example, 3 rows). Select the "Show Navigation button" to select the location of the page navigation as "bottom", the mode selected as "page number", the numeric button (that is, the number of pages displayed in the page link) set to an appropriate value, the specific settings are shown in Figure 4.

Figure 4 Paging functionality in the DataGrid Property Builder

Now select "Column" in Figure 3 and the window shown in Figure 5, remove the "Automatically create columns at run Time" feature, and add number, sex, and age in Available columns to selected columns. Add "Select" in the button column in Figure 5 to the selected columns. Then, in text field, select the last name segment, select LinkButton in the button type, and enter name in the header text, as shown in Figure 5.

Finally, on the form, use the image control that displays the picture (note the image control for the Web form). When you're done, you write code to implement the program's functionality, and the code in the form Load event is:

dbc.connectionstring = "Provider=Microsoft.Jet.OLEDB.4.0;Data source=" & _
Server.MapPath ("Star.mdb") ' DBC is the name of the data connection, Star.mdb is the database
Dba.fill (DS1, "star") ' DBA and DS1 are the names of data adapters and datasets, and the Star is the datasheet
If not Page.IsPostBack Then
Datagrid1.databind ()
End If

The first statement here is used to specify the database connection, which resolves the database path problem. Because we operate on the absolute path of the database when we configure the data adapter and establish the data connection, it is likely that you will not be able to find the database when you copy or publish the program to another server (the machine conforms to the ASP.NET operating environment). To avoid this problem, we add this statement to the form Load event code. But be careful to put the database and the ASP.net program in the same Web folder.

In response to the paging of the DataGrid, we will add the following code to the DataGrid1 pageindexchanged event:

Datagrid1.currentpageindex = E.newpageindex
Datagrid1.databind ()
Image1.imageurl = Nothing ' the picture on the previous page will not be displayed when you flip it

Figure 5 Column functionality in the DataGrid Property Builder

To achieve the function of selecting columns, we also write the following program for the DATAGRID1 SelectedIndexChanged event:

Dim Index as Integer
index = Datagrid1.selectedindex + (datagrid1.currentpageindex) * 3
Dim fname as String
fname = ds1.tables (0). Rows (Index) ("filename")
Image1.imageurl = fname

So, can such a program run and debug on other servers? This is the majority of program designers are very concerned about the problem. To copy the program to another server to run and debug, please compile on this computer by selecting build and browse on the File menu, which will generate two files with a DLL extension in the bin directory in the Web folder in which the program resides. If you are simply copying the program to another server, you can simply copy the main file with the ASPX extension, the database file, and the Bin folder to any virtual directory (or Web shared folder) on the other server. If you copy the program to another server to debug, you should copy all the relevant files, but the name of the virtual directory can be different.

From here we can also see: The ASP in the annoying database paging and graphics display problems, if the vb.net to design, it is a very easy thing.

All of the above programs are debugged and passed under Vb.net+windows 2000.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.