C # database programming 2

zhaozj2021-02-16  51

four. Insert data record:

Inserting a database inserting a record operation and deleting a record operation Basic idea is through

ADO.NET

First insert the data record to the database, then

"Dataset"

Objects make necessary modifications. The following code is

Access 2000

The database modifies the current record code:

protected void update_record (Object Sender, System.EventArgs E)

{

INT i = mybind.position;

Try {

File: // Connect to a database

String strcon = "provider = microsoft.jet.Oledb.4.0; data source = db.mdb";

OLEDBCONNECTION MyConn = New OLEDBCONNECTION (STRCON);

Myconn.open ();

MyDataSet.Tables ["Person"]. rows [mybind.position]. Beginedit ();

File: // Modify the specified record from the database

String strunt = "Update Person Set XM = '"

T_Xm.Text "', XB ='"

T_XB.Text "', NL ="

T_NL.Text ", Zip ="

T_Books.Text "Where id =" t_id.text;

OLEDBCommand myCommand = New OLEDBCOMMAND (STRUPDT, MyConn);

mycommand.executenonquery ();

MyDataSet.Tables ["Person"]. rows [mybind.position]. Endedit ();

MyDataSet.Tables ["Person"]. acceptchanges ();

MyConn.close ();

}

Catch (Exception ED)

{

Messagebox.show ("Modify Specify Record Error:" Ed.toTString (), "Error!");

}

mybind.position = i;

}

Due to the difference between SQL Server 2000 Data Record Modification and Access 2000 Data Record Modification Action is only in different data links, the specific code can refer to the code in "Delete Data Record", here Not available.

Fives. Insert data record:

And the previous two operations are consistent in the idea, that is, through ADO.NET first inserted into the database, and then make the necessary modifications to the "DataSet" object. The following code is to insert a data record as a model as an Access 2000 database.

Protected Void Insert_Record (Object Sender, System.Eventargs E)

{

Try

{

File: // Determine if all fields are added, add it to the execution, reverse pop-up prompt

IF (t_id.text! = "" && t_xm.text! = "" && t_xb.text! = "&& t_nl.text! =" && t_books.text! = "")

{

String myconn1 = "provider = microsoft.jet.Oledb.4.0; data source = db.mdb";

OLEDBConnection myconn = new oledbconnection (myConn1);

Myconn.open ();

String strinsert = "INSERT INTO PERSON (ID, XM, XB, NL, ZIP) VALUES (";

Strinsert = T_ID.Text ", '"

Strinsert = t_xm.text "','"

Strinsert = t_xb.text ""; "

Strinsert = t_nl.text ",";

Strinsert = T_Books.Text ")";

OLEDBCOMMAND INST = New OLEDBCOMMAND (Strinsert, MyConn);

INST.EXECUTENONQUERY ();

MyConn.close ();

MyDataSet.Tables ["Person"]. rows [mybind.position]. Beginedit ();

MyDataSet.Tables ["Person"]. rows [mybind.position]. Endedit ();

MyDataSet.Tables ["Person"]. acceptchanges ();

}

Else

{

Messagebox.show ("must fill all field values!", "Error!");

}

}

Catch (Exception ED)

{

Messagebox.show ("Save Data Record" Ed.toTString (), "Error!");

}

}

The difference between the SQL Server 2000 database is inserted and the Access 2000 database insertion record operation is only only for different data links, and the specific code can refer to the code in "Deleting Data Record", which is not available here.

six. Visual C # database programming completion source code and program running main interface:

Master the above points, write a complete database programming program is very easy, the following is the full code of Visual C # for database programming (Data01.cs), this code is designed with the Access 2000 database as model, specifically :

Using system;

Using system.drawing;

Using system.componentmodel;

Using system.windows.forms;

Using system.data.oledb;

Using system.data;

Public Class Data: Form

{

Private system.componentmodel.Container Components = NULL;

PRIVATE button LastRec;

PRIVATE button NEXTREC;

Private button previousRec

PRIVATE button firstREC;

Private textbox t_books;

Private textbox t_nl;

Private ComboBox T_XB;

Private textbox t_xm;

Private textbox t_id;

PRIVATE LABEL L_BOOKS;

Private label l_nl;

PRIVATE LABEL L_XB;

Private label l_xm;

PRIVATE LABEL L_ID;

PRIVATE LABEL LABEL1;

Private dataset mydataset;

Private button button1;

Private button button2;

Private button button3;

Private button button4;

Private bindingmanagerbase mybind;

Public Data ()

{

File: // Connect to a database

GetConnected ();

/ / Initialize the content required in the form

InitializationComponent ();

}

File: / / Clear the resources used in the program

Protected Override Void Dispose (Bool Disposing)

{

IF (Disposing)

{

IF (Components! = NULL)

{

Components.dispose ();

}

}

Base.dispose (Disposing);

}

Public static void main ()

{

Application.run (New Data ());

}

Public void getConnected ()

{

Try

{

File: // Create an OLEDBCONNECTION

String strcon = "provider = microsoft.jet.Oledb.4.0; data source = db.mdb";

OLEDBCONNECTION MyConn = New OLEDBCONNECTION (STRCON);

String strcom = "SELECT * from Person";

File: // Create a DataSet

MyDataSet = new dataset ();

Myconn.open ();

FILE: // Get a dataset with OLEDBDataAdapter

OLEDBDataAdapter mycommand = new oledbdataadapter (strcom, myconn);

FILE: // Bind the DataSet Books Data Sheet

MyCommand.Fill (MyDataSet, "Person");

File: // Turn this OLEDBCONNECTION

MyConn.close ();

}

Catch (Exception E)

{

MessageBox.show ("Connection Error!" E.toString (), "Error");

}

Private vidinitiRizeComponent ()

{

File: // Add control, slightly

THIS.NAME = "DATA";

THIS.TEXT = "Visual C # database programming!";

This.ResumeLayout (false);

mybind = this.bindingContext [MyDataSet, "Person"];

}

Protected Void New_Record (Object Sender, System.Eventargs E)

{

t_id.text = (mybind.count 1) .tostring ();

t_xm.text = ""

t_xb.text = "";

t_nl.text = "";

t_books.text = "";

}

Protected Void Insert_Record (Object Sender, System.Eventargs E)

{

Try

{

File: // Determine if all fields are added, add it to the execution, reverse pop-up prompt

IF (t_id.text! = "" && t_xm.text! = "" && t_xb.text! = "&& t_nl.text! =" && t_books.text! = "")

{

String myconn1 = "provider = microsoft.jet.Oledb.4.0; data source = db.mdb";

OLEDBConnection myconn = new oledbconnection (myConn1);

Myconn.open ();

String strinsert = "INSERT INTO PERSON (ID, XM, XB, NL, ZIP) VALUES (";

Strinsert = T_ID.Text ", '"

Strinsert = t_xm.text "','"

Strinsert = t_xb.text ""; "

Strinsert = t_nl.text ",";

Strinsert = T_Books.Text ")";

OLEDBCOMMAND INST = New OLEDBCOMMAND (Strinsert, MyConn);

INST.EXECUTENONQUERY ();

MyConn.close ();

MyDataSet.Tables ["Person"]. rows [mybind.position]. Beginedit ();

MyDataSet.Tables ["Person"]. rows [mybind.position]. Endedit (); mydataset.tables ["person"]. acceptchanges ();

}

Else

{

Messagebox.show ("must fill all field values!", "Error!");

}

}

Catch (Exception ED)

{

Messagebox.show ("Save Data Record" Ed.toTString (), "Error!");

}

}

Protected Void Update_Record (Object Sender, System.Eventargs E)

{

INT i = mybind.position;

Try {

File: // Connect to a database

String strcon = "provider = microsoft.jet.Oledb.4.0; data source = db.mdb";

OLEDBCONNECTION MyConn = New OLEDBCONNECTION (STRCON);

Myconn.open ();

MyDataSet.Tables ["Person"]. rows [mybind.position]. Beginedit ();

File: // Modify the specified record from the database

String strunt = "Update Person Set XM = '"

T_Xm.Text "', XB ='"

T_XB.Text "', NL ="

T_NL.Text ", Zip ="

T_Books.Text "Where id =" t_id.text;

OLEDBCommand myCommand = New OLEDBCOMMAND (STRUPDT, MyConn);

mycommand.executenonquery ();

MyDataSet.Tables ["Person"]. rows [mybind.position]. Endedit ();

MyDataSet.Tables ["Person"]. acceptchanges ();

MyConn.close ();

}

Catch (Exception ED)

{

Messagebox.show ("Modify Specify Record Error:" Ed.toTString (), "Error!");

}

mybind.position = i;

}

Protected Void Delete_Record (Object Sender, System.Eventargs E)

{

DialogResult r = messagebox.show ("Whether to delete the current record!", "Delete the current record!", MessageBoxButtons.yesno, MessageBoxicon.question;

INT SS = (int))

IF (ss == 6) // Press the "OK" button {

Try {

File: // Connect to a database

String strcon = "provider = microsoft.jet.Oledb.4.0; data source = db.mdb";

OLEDBCONNECTION MyConn = New OLEDBCONNECTION (STRCON);

Myconn.open ();

String strde = "delete from person where id =" t_id.text;

OLEDBCommand mycommand = new oledbcommand (strdele, myconn);

File: // Remove the specified record from the database

mycommand.executenonquery ();

File: // Remove the specified record from the DataSet

MyDataSet.tables ["Person"]. Rows [mybind.position]. delete ();

MyDataSet.Tables ["Person"]. acceptchanges ();

MyConn.close ();

}

Catch (Exception ED)

{

Messagebox.show ("Delete Record Error Message:" Ed.toTString (), "Error!");

}

}

}

File: // Button "Tail Record" Object Event Program

Protected Void GoLast (Object Sender, System.EventArgs E)

{

Mybind.position = mybind.count - 1;

}

File: // Button "Next" Object Event Program

Protected Void Gonext (Object Sender, System.EventArgs E)

{

IF (mybind.position == mybind.count -1)

Messagebox.show ("has reached the last record!", "Information Tips!", MessageBoxButtons.ok, MessageBoxicon.information;

Else

Mybind.position = 1;

}

File: // Button "Previous" Object Event Program

Protected Void Goprevious (Object Sender, System.EventArgs E)

{

IF (mybind.position == 0)

Messagebox.show ("has reached the first record!", "Information Tips!", MessageBoxButtonS.ok, MessageBoxicon.information;

Else

Mybind.position - = 1;

}

FILE: // Button "First Record" Object Event Program

Protected Void Gofirst (Object Sender, System.EventArgs E)

{

Mybind.position = 0;

}

}

For program code as a model with SQL Server 2000 database, just link data in Data01.cs, namely:

String myconn1 = "provider = microsoft.jet.Oledb.4.0; data source = db.mdb"; change to:

String strcon = "provider = sqloledb.1; persist security info = false; user id = sa; initial catalog = data1; data source = server1";

Note: This data link represents the meaning of: Open the SQL Server database, the server name is Server1, the database is DATA1

You can get Visual C # to perform the source program code for the SQL Server 2000 database for template programming. So this article is no longer available.

Seven. to sum up:

Database programming is always a key and difficult point in programming content. These operations described above are the most basic and most important content in database programming. Those complex programming is nothing more than a number of stacked plies.

转载请注明原文地址:https://www.9cbs.com/read-21844.html

New Post(0)