Use Visual C # To add data records

Source: Internet
Author: User

Author: Wang Tian

In this article, we will introduce a basic operation of Visual C # on the database, that is, how to add records to the database. We will introduce some database operation examples in detail. To better illustrate this problem, two typical databases are used in database selection. One is local database -- Access 2000, and the other is remote database -- SQL SERVER 7.0. First, we will introduce how to use Visual C # To add Access 2000 database records.

1. Use Visual C # To add Access 2000 database records
(1). environment settings for program design and operation:
(1) Windows 2000 Server Edition
(2) Microsoft Data Acess Component 2.6 or later (MDAC 2.6)
(3) Introduction to the databases used in this article:

The database name used in the program is sample. mdb, which contains a data table "books. The structure of this data table is as follows:
Field Name field type meaning
Bookid number
Booktitle text book name
Bookauthor text book author
Bookprice
Bookstock digital bookshelf No.

(2). Difficulties in program design and problems to be paid attention:
How to correctly add records to the database is a key and difficult point to be discussed in this article. The following describes how to solve this problem:
(1) Create and open an OleDbConnection object.
(2) create an SQL statement to insert a record.
(3) create an OleDbCommand object.
(4) Insert a record to the database using this OleDbCommand object.
The specific statements implemented in the program are as follows:
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 ();


(3). Use Visual C # To insert the recorded program source code (add. cs) and the executed interface:
Is the execution interface after adding. cs Compilation:



Add. cs source code:
Using System;
Using System. Drawing;
Using System. ComponentModel;
Using System. Windows. Forms;
Using System. Data. OleDb;
Using System. Data;
// The namespace used in the import program
Public class DataAdd: Form {
Private Button lastrec;
Private Button nextrec;
Private Button previusrec;
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 be used in the program
Public DataAdd (){
// Connect to a database
GetConnected ();
// Initialize the content required in the form
InitializeComponent ();
}
// Release 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 an 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 ();
// Use OleDbDataAdapter to obtain a dataset
OleDbDataAdapter myCommand = new OleDbDataAdapter (strCom, myConn );
// Bind Dataset to the books data table
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 ();
Previusrec = new Button ();
Firstrec = new Button ();
T_bookprice = new TextBox ();
Rochelle booktitle = new Label ();
Rochelle price = new Label ();
Rochelle author = new Label ();
T_bookid = new TextBox ();
Save = new Button ();
Title = new Label ();
T_bookauthor = new TextBox ();
T_booktitle = new TextBox ();
T_new = new Button ();
Rochelle bookstock = new Label ();
T_bookstock = new TextBox ();
Rochelle bookid = new Label ();
// The following four buttons for data browsing are initialized:
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 ("", 8f );
Firstrec. Text = "first record ";
Firstrec. Click + = new System. EventHandler (GoFirst );
Previusrec. Location = new System. Drawing. Point (135,312 );
Previusrec. ForeColor = System. Drawing. Color. Black;
Previusrec. Size = new System. Drawing. Size (40, 24 );
Previusrec. Font = new System. Drawing. Font ("", 8f );
Previusrec. Text = "previous ";
Previusrec. 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 ("", 8f );
Nextrec. Text = "Next ";
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 ("", 8f );
Lastrec. Text = "tail record ";
Lastrec. Click + = new System. EventHandler (GoLast );
// Initialize the display label
Rochelle bookid.location = new System. Drawing. Point (24, 56 );
Rochelle bookid.text = "book no :";
L_bookid.Size = new System. Drawing. Size (112, 20 );
Rochelle bookid.font = new System. Drawing. Font ("", 10f );
Rochelle bookid.textalign = System. Drawing. ContentAlignment. MiddleCenter;
Rochelle booktitle.location = new System. Drawing. Point (24,108 );
Rochelle booktitle.text = "book name :";
Rochelle booktitle.size = new System. Drawing. Size (112, 20 );
Rochelle booktitle.font = new System. Drawing. Font ("", 10f );
Rochelle booktitle.textalign = System. Drawing. ContentAlignment. MiddleCenter;
Rochelle price.location = new System. Drawing. Point (24,212 );
L_bookprice.Text = "Price :";
Rochelle price.size = new System. Drawing. Size (112, 20 );
L_bookprice.Font = new System. Drawing. Font ("", 10f );
L_bookprice.TextAlign = System. Drawing. ContentAlignment. MiddleCenter;
Rochelle stock.location = new System. Drawing. Point (24,264 );
Rochelle stock.text = "shelf number :";
Rochelle stock.size = new System. Drawing. Size (112, 20 );
Rochelle stock.font = new System. Drawing. Font ("", 10f );
Rochelle stock.tabindex = 16;
Rochelle stock.textalign = System. Drawing. ContentAlignment. MiddleCenter;
L_bookauthor.Location = new System. Drawing. Point (24,160 );
Rochelle author.text = "Author :";
L_bookauthor.Size = new System. Drawing. Size (112, 20 );
L_bookauthor.Font = new System. Drawing. Font ("", 10f );
L_bookauthor.TextAlign = System. Drawing. ContentAlignme

Related Article

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.