usingSystem;usingSystem.Collections.Generic;usingSystem.ComponentModel;usingSystem.Data;usingSystem.Drawing;usingSystem.Linq;usingSystem.Text;usingSystem.Windows.Forms;usingSystem.Data.SqlClient;namespaceshiyan4{ Public Partial classForm1:form {SqlDataAdapter adapter; DataTable table; PublicForm1 () {InitializeComponent (); } Private voidButton1_Click (Objectsender, EventArgs e) { stringVarnumber =TextBox1.Text.Trim (); if(Varnumber = =""|| Varnumber = =NULL) {MessageBox.Show ("Please enter the number you want to inquire","Prompt Information", messageboxbuttons.ok,messageboxicon.warning);} Try {stringConnStr ="Data source=.\\sqlexpress; attachdbfilename=| Datadirectory|\\database1.mdf;integrated security=true; User instance=true"; SqlConnection Conn=NewSqlConnection (CONNSTR); stringsql ="SELECT * from Table1 where name = '"+ Varnumber +"'"; Adapter=NewSqlDataAdapter (SQL, conn); SqlCommandBuilder Builder=NewSqlCommandBuilder (adapter); Table=NewDataTable (); Adapter. Fill (table); Datagridview1.datasource=table; //Conn. Close (); } Catch(Exception ee) {MessageBox.Show (EE. Message,"Prompt Information", MessageBoxButtons.OK, messageboxicon.warning); } } Private voidButton2_Click (Objectsender, EventArgs e) {TextBox1.Text=""; Datagridview1.endedit (); Try{adapter. Update (table); MessageBox.Show ("saved successfully"); } Catch(Exception ee) {MessageBox.Show (EE. Message,"Save failed"); } } Private voidDatagridview1_cellcontentclick (Objectsender, DataGridViewCellEventArgs e) { } }}
C # Experiment 4 database