visual| data in this article, we will describe a basic operation of Visual C # to the database: How to add records to the database. We will use some examples of database operations, to specify. To illustrate this issue more clearly, two types of databases are currently typical in the selection of databases, one is the local database--access 2000 and the other is the remote database--sql SERVER 7.0. First, you can use Visual C # to add records for an Access 2000 database.
A Use Visual C # to add records for an Access 2000 database
(i) Design and operation of the environment settings:
(1) Windows 2000 Server Edition
(2) Microsoft Data acess Component 2.6 version (MDAC 2.6)
(3) Introduction to the database used by this procedure:
The database name used in the program is Sample.mdb, and there is a datasheet for books in this database. The data table is structured as follows:
Field Name field type represents meaning
BookID Numeric serial number
BookTitle Text book name
Bookauthor Text book author
Bookprice Digital Price
Bookstock Digital Bookshelf Number
(b) programming difficulties and issues to be noted:
How to correctly add records to the database is one of the key and difficult points to be discussed in this paper, the following is the specific idea to solve this problem:
(1) Create and open a OleDbConnection object.
(2) Create an SQL statement that inserts a record.
(3) Create a OleDbCommand object.
(4) The operation of inserting a record into the database is done through this OleDbCommand object.
The following are the specific statements that are implemented in the program:
String strconn = "Provider = microsoft.jet.oledb.4.0;" Data Source = Sample.mdb ";
OleDbConnection myconn = new OleDbConnection (strconn);
MyConn.Open ();
String Strinsert = "INSERT into books (BookID, BookTitle, Bookauthor, Bookprice, Bookstock) VALUES (";
Strinsert + = T_bookid. Text + ", '";
Strinsert + = T_booktitle. Text + "', '";
Strinsert + = T_bookauthor. Text + "',";
Strinsert + = T_bookprice. Text + ",";
Strinsert + = T_bookstock. Text + ")";
OleDbCommand Inst = new OleDbCommand (Strinsert, myconn);
Inst. ExecuteNonQuery ();
Myconn.close ();
(iii). Use Visual C # to insert the recorded program source code (Add.cs) and the executed interface:
The following figure is the Add.cs compiled execution interface:
Add.cs Source program code:
Using System;
Using System.Drawing;
Using System.ComponentModel;
Using System.Windows.Forms;
Using System.Data.OleDb;
Using System.Data;
Namespaces used in the import program
public class Dataadd:form {
Private Button Lastrec;
Private Button Nextrec;
Private Button Previousrec;
Private Button Firstrec;
Private Container components;
Private Label title;
Private Button t_new;
Private Button Save;
Private TextBox T_bookstock;
Private TextBox T_bookprice;
Private TextBox T_bookauthor;
Private TextBox T_booktitle;
Private TextBox T_bookid;
Private Label L_bookstock;
Private Label L_bookprice;
Private Label L_bookauthor;
Private Label L_booktitle;
Private Label L_bookid;
Private DataSet myDataSet;
Private BindingManagerBase Mybind;
Define the components to use in your program
Public Dataadd () {
Connect to a database
Getconnected ();
Initialize the content that is required in the form
InitializeComponent ();
}
Release the resources used by the program
public override void Dispose () {
Base. Dispose ();
Components. Dispose ();
}
public static void Main () {
Application.Run (New Dataadd ());
}
public void getconnected ()
{
try{
Create a OleDbConnection object
String Strcon = "Provider = microsoft.jet.oledb.4.0;" Data Source = Sample.mdb ";
OleDbConnection myconn = new OleDbConnection (Strcon);
String strcom = "SELECT * from Books";
Create a DataSet
myDataSet = new DataSet ();
MyConn.Open ();
Using OleDbDataAdapter to get a dataset
OleDbDataAdapter mycommand = new OleDbDataAdapter (strcom, myconn);
Bind a dataset to books data tables
Mycommand.fill (myDataSet, "books");
Close this OleDbConnection
Myconn.close ();
}
catch (Exception e)
{
MessageBox.Show ("Connection Error!" + e.tostring (), "error");
}
}
private void InitializeComponent ()
{
components = new System.ComponentModel.Container ();
Nextrec = new Button ();
Lastrec = new Button ();
Previousrec = new Button ();
Firstrec = new Button ();
T_bookprice = new TextBox ();
L_booktitle = new Label ();
L_bookprice = new Label ();
L_bookauthor = new Label ();
T_bookid = new TextBox ();
Save = new Button ();
title = new Label ();
T_bookauthor = new TextBox ();
T_booktitle = new TextBox ();
T_new = new Button ();
L_bookstock = new Label ();
T_bookstock = new TextBox ();
L_bookid = new Label ();
The following is an initialization of the four buttons for data browsing
Firstrec. Location = new System.Drawing.Point (65, 312);
Firstrec. ForeColor = System.Drawing.Color.Black;
Firstrec. Size = new System.Drawing.Size (40, 24);
Firstrec. Font = new System.Drawing.Font ("imitation", 8f);
Firstrec. Text = "First record";
Firstrec. Click + + new System.EventHandler (Gofirst);
Previousrec. Location = new System.Drawing.Point (135, 312);
Previousrec. ForeColor = System.Drawing.Color.Black;
Previousrec. Size = new System.Drawing.Size (40, 24);
Previousrec. Font = new System.Drawing.Font ("imitation", 8f);
Previousrec. Text = "previous One";
Previousrec. Click + + new System.EventHandler (goprevious);
Nextrec. Location = new System.Drawing.Point (205, 312);
Nextrec. ForeColor = System.Drawing.Color.Black;
Nextrec. Size = new System.Drawing.Size (40, 24);
Nextrec. Font = new System.Drawing.Font ("imitation", 8f);
Nextrec. Text = "Next Bar";
Nextrec. Click + + new System.EventHandler (GoNext);
Lastrec. Location = new System.Drawing.Point (275, 312);
Lastrec. ForeColor = System.Drawing.Color.Black;
Lastrec. Size = new System.Drawing.Size (40, 24);
Lastrec. Font = new System.Drawing.Font ("imitation", 8f);
Lastrec. Text = "tail record";
Lastrec. Click + + new System.EventHandler (golast);
The following is an initialization of the display label
L_bookid. Location = new System.Drawing.Point (24, 56);
L_bookid. Text = "Book Serial number:";
L_bookid. Size = new System.Drawing.Size (112, 20);
L_bookid. Font = new System.Drawing.Font ("imitation", 10f);
L_bookid. TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
L_booktitle. Location = new System.Drawing.Point (24, 108);
L_booktitle. Text = "title:";
L_booktitle. Size = new System.Drawing.Size (112, 20);
L_booktitle. Font = new System.Drawing.Font ("imitation", 10f);
L_booktitle. TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
L_bookprice. Location = new System.Drawing.Point (24, 212);
L_bookprice. Text = "Price:";
L_bookprice. Size = new System.Drawing.Size (112, 20);
L_bookprice. Font = new System.Drawing.Font ("imitation", 10f);
L_bookprice. TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
L_bookstock. Location = new System.Drawing.Point (24, 264);
L_bookstock. Text = "Bookshelf number:";
L_bookstock. Size = new System.Drawing.Size (112, 20);
L_bookstock. Font = new System.Drawing.Font ("imitation", 10f);
L_bookstock. TabIndex = 16;
L_bookstock. TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
L_bookauthor. Location = new System.Drawing.Point (24, 160);
L_bookauthor. Text = "Author:";
L_bookauthor. Size = new System.Drawing.Size (112, 20);
L_bookauthor. Font = new System.Drawing.Font ("imitation", 10f);
L_bookauthor. TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
Title. Location = new System.Drawing.Point (32, 16);
Title. Text = "Use vsiual C # to increase data logging!" " ;
Title. Size = new System.Drawing.Size (336, 24);
Title. ForeColor = System.Drawing.Color.Green;
Title. Font = new System.Drawing.Font ("imitation", 14f, System.Drawing.FontStyle.Bold);
The following is an initialization of the labels and text boxes that are set up to display data records and bind the records to a different bound to the text box "text" Property
T_bookid. Location = new System.Drawing.Point (184, 56);
T_bookid. Size = new System.Drawing.Size (80, 20);
T_bookid. Databindings.add ("Text", myDataSet, "Books.bookid");
T_bookstock. Location = new System.Drawing.Point (184, 264);
T_bookstock. Size = new System.Drawing.Size (80, 20);
T_bookstock. Databindings.add ("Text", myDataSet, "Books.bookstock");
T_booktitle. Location = new System.Drawing.Point (184, 108);
T_booktitle. Size = new System.Drawing.Size (176, 20);
T_booktitle. Databindings.add ("Text", myDataSet, "Books.booktitle");
T_bookprice. Location = new System.Drawing.Point (184, 212);
T_bookprice. Size = new System.Drawing.Size (80, 20);
T_bookprice. Databindings.add ("Text", myDataSet, "Books.bookprice");
T_bookauthor. Location = new System.Drawing.Point (184, 160);
T_bookauthor. Size = new System.Drawing.Size (128, 20);
T_bookauthor. Databindings.add ("Text", myDataSet, "Books.bookauthor");
T_new. Location = new System.Drawing.Point (62, 354);
T_new. Size = new System.Drawing.Size (96, 32);
T_new. Text = "new Record";
T_new. Click + + new System.EventHandler (T_newclick);
Save. Location = new System.Drawing.Point (222, 354);
Save. Size = new System.Drawing.Size (96, 32);
Save. TabIndex = 4;
Save. Text = "Save Record";
Save. Click + + new System.EventHandler (Saveclick);
This. Text = "Use vsiual C # to increase the data logging of the program window!" " ;
This. AutoScaleBaseSize = new System.Drawing.Size (5, 13);
This. FormBorderStyle = FormBorderStyle.Fixed3D;
This. ClientSize = new System.Drawing.Size (390, 400);
Add the following components to the form
This. Controls.Add (LASTREC);
This. Controls.Add (NEXTREC);
This. Controls.Add (PREVIOUSREC);
This. Controls.Add (FIRSTREC);
This. Controls.Add (title);
This. Controls.Add (t_new);
This. Controls.Add (save);
This. Controls.Add (T_bookstock);
This. Controls.Add (T_bookprice);
This. Controls.Add (T_bookauthor);
This. Controls.Add (T_booktitle);
This. Controls.Add (T_bookid);
This. Controls.Add (L_bookstock);
This. Controls.Add (L_bookprice);
This. Controls.Add (L_bookauthor);
This. Controls.Add (L_booktitle);
This. Controls.Add (L_bookid);
Bind the object dataset and the Books data table to this Mybind object
Mybind= this. BindingContext [myDataSet, "books"];
}
protected void Saveclick (object sender, System.EventArgs e)
{
Try
{
To determine if all fields have been added, execute when finished, or pop-up prompts
if (T_bookid. Text!= "" && t_booktitle. Text!= "" && t_bookauthor. Text!= "" && t_bookprice. Text!= "" && t_bookstock. Text!= "")
{
String strconn = "Provider = microsoft.jet.oledb.4.0;" Data Source = Sample.mdb ";
OleDbConnection myconn = new OleDbConnection (strconn);
MyConn.Open ();
String Strinsert = "INSERT into books (BookID, BookTitle, Bookauthor, Bookprice, Bookstock) VALUES (";
Strinsert + = T_bookid. Text + ", '";
Strinsert + = T_booktitle. Text + "', '";
Strinsert + = T_bookauthor. Text + "',";
Strinsert + = T_bookprice. Text + ",";
Strinsert + = T_bookstock. Text + ")";
OleDbCommand Inst = new OleDbCommand (Strinsert, myconn);
Inst. ExecuteNonQuery ();
Myconn.close ();
}
Else
{
MessageBox.Show ("All field values must be filled!") "," the mistake! " ) ;
}
}
catch (Exception ed)
{
MessageBox.Show ("Save data Record Occurrence" + ed.) ToString (), "Error!" " ) ;
}
}
protected void T_newclick (object sender, System.EventArgs e)
{
T_bookid. Text = "";
T_booktitle. Text = "";
T_bookauthor. Text = "";
T_bookprice. Text = "";
T_bookstock. Text = "";
}
Button "Tail Record" object event program
protected void Golast (object sender, System.EventArgs e)
{
Mybind.position = mybind.count-1;
}
Button "Next" Object event program
protected void GoNext (object sender, System.EventArgs e)
{
if (mybind.position = = mybind.count-1)
MessageBox.Show ("It's the last record!") " ) ;
Else
Mybind.position + 1;
}
Button "Previous" Object event program
protected void Goprevious (object sender, System.EventArgs e)
{
if (mybind.position = 0)
MessageBox.Show ("It's the first record!") " ) ;
Else
Mybind.position-= 1;
}
Button "First record" object event program
protected void Gofirst (object sender, System.EventArgs e)
{
mybind.position = 0;
}
}
Successfully compiling the above code allows you to insert records into the Access 2000 database.
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.