C # Database Thread 2

Source: Internet
Author: User
Tags count modify connect thread tostring zip
Data | database four. Inserting data records: </P><P> Insert record operations and delete records operations on a database The basic idea is to use ado.net to insert data into the database first, and then make the necessary modifications to the DataSet object. The following code is the code that modifies the current record in an Access 2000 database model: </p><p>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://to modify the specified record from the database

String STRUPDT = "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 the specified record error:" + ed.) ToString (), "Error!" " ) ;

}

Mybind.position = i;

} </P><P> because the difference between the SQL Server 2000 data Logging modification operation and the Access 2000 data record modification operation is only in different data links, the specific code can refer to the code in the "Delete data record", which is not available here.



Five Insert Data record:



In line with the previous two operations, the idea is to first insert data into the database through ado.net, and then make the necessary modifications to the DataSet object. The following code is the code that inserts a data record for the model in an Access 2000 database



protected void Insert_record (object sender, System.EventArgs e)

{

Try

{

file://to determine if all fields have been added, execute when finished, or pop-up prompts

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 ("All field values must be filled!") "," the mistake! " ) ;

}

}

catch (Exception ed)

{

MessageBox.Show ("Save data Record Occurrence" + ed.) ToString (), "Error!" " ) ;

}

}



Also, the difference between an Insert record operation for a SQL Server 2000 database and an Access 2000 database insert record is only a different data link, and the code can refer to the code in the "Delete data record", which is not available here.

Six Visual C # Database programming completes the main interface of source code and program running:



With the above points in hand, it's easy to write a complete database programming program, followed by the complete code for database programming in Visual C # (Data01.cs), designed in an Access 2000 database model, as follows:



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 that is required in the form

InitializeComponent ();

}

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 a 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://use OleDbDataAdapter to get a dataset

OleDbDataAdapter mycommand = new OleDbDataAdapter (strcom, myconn);

file://bind the dataset to the Books data table

Mycommand.fill (myDataSet, "person");

file://close this OleDbConnection

Myconn.close ();

}

catch (Exception e)

{

MessageBox.Show ("Connection Error!" + e.tostring (), "error");

}

}

private void InitializeComponent ()

{



file://add controls, slightly



This. Name = "Data";

This. Text = "Database programming for Visual C #!" " ;

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://to determine if all fields have been added, execute when finished, or pop-up prompts

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 ("All field values must be filled!") "," the mistake! " ) ;

}

}

catch (Exception ed)

{

MessageBox.Show ("Save data Record Occurrence" + ed.) ToString (), "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://to modify the specified record from the database

String STRUPDT = "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 the specified record error:" + ed.) ToString (), "Error!" " ) ;

}

Mybind.position = i;

}



protected void Delete_record (object sender, System.EventArgs e)

{

DialogResult r = MessageBox.Show ("Delete current record!") "," delete the current record! ", Messageboxbuttons.yesno, messageboxicon.question);

int ss = (int) r;

if (ss = 6)//press 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 Strdele = "DELETE from person WHERE id=" + t_id. Text;

OleDbCommand mycommand = new OleDbCommand (Strdele, myconn);

file://deletes the specified record from the database

Mycommand.executenonquery ();

file://deletes 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 logging error message:" + ed.) ToString (), "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 ("It's 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 ("It's 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 that is modeled on a SQL Server 2000 database, simply link the data in the Data01.cs, that is:

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 means: Open the SQL Server database, the server name is Server1, the database is Data1

You can get the complete source code that Visual C # has programmed for the SQL Server 2000 database for templates. So this article is no longer available.



Seven Summarize:



Database programming is always a key and difficult point in program programming content. These operations are the most basic and important content of database programming. Those complex programming is nothing more than a number of these processes superimposed.








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.