itemstyle>
headerstyle>
asp: editcommandcolumn>
asp: buttoncolumn>
Columns>
asp: datagrid> td>
TR>
TABLE>
center>
div>
form>
body>
Html>
Add.asp.cs code:
Using system;
Using system.collections;
Using system.componentmodel;
Using system.data;
Using system.data.sqlclient;
Using system.drawing;
Using system.Web;
Using system.Web.SessionState;
Using system.Web.ui;
Using system.Web.ui.webcontrols;
Using system.Web.ui.htmlcontrols;
Namespace TEACHSHOW.CHARPTER7.ACCESSDATABASE
{
///
/// add a summary description of the Add.
/// summary>
Public class address: system.Web.ui.page
{
protected system.web.ui.webcontrols.textbox textbox1;
protected system.web.ui.webcontrols.textbox textbox2;
protected system.web.ui.webcontrols.textbox textbox3;
protected system.web.ui.webcontrols.textbox textbox4;
Protected system.web.ui.webcontrols.linkbutton linkbutton1;
Protected system.web.ui.webcontrols.dataGrid DataGrid1;
protected system.web.ui.webcontrols.textbox textbox5;
Private Void Page_Load (Object Sender, System.EventArgs E)
{
/ / Place the user code here to initialize the page
IF (! this.ispostback)
{
this.bindgrid ();
}
}
#Region web form designer generated code
Override protected void oninit (Eventargs E)
{
//
// Codegen: This call is necessary for the ASP.NET Web Form Designer.
//
InitializationComponent ();
Base.onit (E);
}
///
/// Designer supports the required method - do not use the code editor to modify
/// This method is content.
/// summary>
Private vidinitiRizeComponent ()
{
This.LinkButton1.Click = new system.eventhandler (this.LinkButton1_click);
This.DataGrid1.disposed = new system.eventhandler (this.DataGrid1_disposed);
This.DataGrid1.cancelcommand = new system.Web.ui.WebControls.DataGridCommandeventHandler (this.DataGrid1_cancelcommand);
This.DataGrid1.editcommand = new system.web.ui.webcontrols.dataGridCommandeventHandler (this.DataGrid1_editCommand);
This.DataGrid1.updateCommand = new system.web.ui.webcontrols.dataGridCommandeventHandler (this.DataGrid1_updateCommand);
this.DataGrid1.DeleteCommand = new System.Web.UI.WebControls.DataGridCommandEventHandler (this.DataGrid1_DeleteCommand); this.DataGrid1.SelectedIndexChanged = new System.EventHandler (this.DataGrid1_SelectedIndexChanged);
This.Load = New System.EventHandler (this.page_load);
}
#ndregion
Private Void LinkButton1_Click (Object Sender, System.Eventargs E)
{
AddPublisher ();
}
///
/// Add to
/// summary>
Private void addpublisher ()
{
String SQL = "INSERT INTO PUBLISHERS (Pub_ID, Pub_Name, City, State, Country) VALUES (@ pubid, @ public, @ city, @ state, @ country);
SqlConnection Con = New SqlConnection ("Server = Accp-lzh; UID = SA; PWD = Sasa; Database = PUBS");
SQLCommand cmd = new SQLCOMMAND (SQL, CON);
Cmd.Parameters.Add (New Sqlparameter ("@ pubid", sqldbtype.char, 4));
CMD.Parameters ["@ pubid"]. value = this.textbox1.text;
cmd.Parameters.Add (New Sqlparameter ("@ Pubname", SqldbType.varchar, 40));
CMD.Parameters ["@ Pubname"]. Value = this.TextBox2.Text;
CMD.Parameters.Add (New Sqlparameter ("@ city", sqldbtype.char, 20));
Cmd.Parameters ["@ City"]. Value = this.TextBox3.Text;
Cmd.Parameters.Add (New Sqlparameter ("@ State", SqldbType.char, 2));
CMD.Parameters ["@ State"]. Value = this.TextBox4.Text;
Cmd.Parameters.Add (New SqlParameter ("@ country", sqldbtype.varchar, 30));
Cmd.Parameters ["@ country"]. value = this.textbox5.text;
cmd.connection.open ();
cmd.executenonquery ();
cmd.connection.close ();
THIS.TEXTBOX1.TEXT = "";
THIS.TEXTBOX2.TEXT = "";
THIS.TEXTBOX3.TEXT = ""
This.TextBox4.text = ""; this.textbox5.text = ";
this.bindgrid ();
}
Private void bindgrid ()
{
SqlConnection Con = New SqlConnection ("Server = Accp-lzh; UID = SA; PWD = Sasa; Database = PUBS");
Sqldataadapter mysqlcom = new sqldataadapter ("SELECT * FROM PUBLISHERS WHERE PUB_ID LIKE '99% '", CON);
DataSet myds = new dataset ();
MySQLCOM.FILL (MyDS, "Publishers");
This.DataGrid1.datasource = myds.tables ["publishers"]. defaultview;
THIS.DATAGRID1.DATABIND ();
}
///
// // click to trigger events when editing
/// summary>
/// param>
/// param>
Private void DataGrid1_EditCommand (Object Source, System.Web.ui.WebControls.DataGridCommandEventArgs E)
{
This.DataGrid1.edititeMindex = (int) E.Item.ItemIndex;
this.bindgrid ();
}
///
/// Click Cancel to trigger the event.
/// summary>
/// param>
/// param>
Private void DataGrid1_cancelcommand (Object Source, System.Web.ui.WebControls.DataGridCommandeventArgs E)
{
This.DataGrid1.columns [0] .Headertext = "Caquered";
THIS.DATAGRID1.EDITITEMINDEX = -1;
this.bindgrid ();
}
Private void DataGrid1_selectedIndexchanged (Object Sender, System.EventArgs E)
{
}
///
// / Trigger the event when you click Update.
/// summary>
/// param>
/// param>
Private void DataGrid1_UpdateCommand (Object Source, System.Web.ui.WebControls.DataGridCommandeventArgs E)
{
SqlConnection Con = New SqlConnection ("Server = Accp-lzh; UID = SA; PWD = Sasa; Database = PUBS"); sqlcommand selectcmd = con.createCommand ();
Selectcmd.commandtype = commandtype.text;
Selectcmd.commandtext = "SELECT * from Publishers where Pub_id Like '99% '"
SqlDataAdapter SQLADATPER = New SqlDataAdapter ();
SQLADATPER.SELECTCOMMAND = SELECTCMD;
DataSet DS = New DataSet ();
C.Open ();
SQLADATPER.FILL (DS, "Publishers");
C. close ();
Sqlcommand updatecmd = con.createcommand ();
Updatecmd.commandtext = "Update Publishers Set Pub_Name = @ Pubname, City = @ City, State = @ State, Country = @ Country WHERE PUB_ID = @ Pub_ID";
Sqlparameter PubnamePar = New SqlParameter ("@ Pubname", SqldbType.varchar, 40, "Pub_Name");
Updatecmd.Parameters.Add (PubnamePar);
SQLParameter Citypar = New Sqlparameter ("@ City", Sqldbtype.varchar, 20, "City");
Updatecmd.Parameters.Add (CityPar);
SQLParameter StatePar = New SqlParameter ("@ State", Sqldbtype.char, 2, "State");
Updatecmd.Parameters.Add (statepar);
SQLParameter CountryPar = New Sqlparameter ("@ country", sqldbtype.varchar, 30, "country);
Updatecmd.Parameters.Add (CountryPar);
SQLParameter PubidPar = New SqlParameter ("@ pub_id", sqldbtype.char, 4, "pub_id");
PubidPar.SourceVersion = DATAROWVERSION.ORIGINAL;
Updatecmd.Parameters.Add (PubidPar);
SQLADATPER.UPDATECOMMAND = UpdateCMD;
DataTable Table = DS.TABLES ["Publishers"];
Table.primaryKey = new datacolumn []
{
Table.columns ["pub_id"]
}
DataRow Row = Table.Rows.Find ((TextBox) (E.Item.cells [2] .controls [0])). Text);
ROW ["pub_name"] = ((TextBox) (E.Item.cells [3] .controls [0])). Text; row ["city"] = ((TextBox) (E.Item.cells [4] .Controls [0])).
ROW ["State"] = ((TextBox) (E.Item.cells [5] .controls [0])).
ROW ["country"] = ((TextBox) (E.Item.cells [6] .controls [0])).
C.Open ();
SQLADATPER.UPDATE (TABLE);
C. close ();
THIS.DATAGRID1.EDITITEMINDEX = -1;
this.bindgrid ();
}
///
// / Trigger the event when you click delete
/// summary>
/// param>
/// param>
Private void DataGrid1_DeleteCommand (Object Source, System.Web.ui.WebControls.DataGridCommandeventargs E)
{
SqlConnection Con = New SqlConnection ("Server = Accp-lzh; UID = SA; PWD = Sasa; Database = PUBS");
SQLCommand selectcmd = con.createcommand ();
Selectcmd.commandtext = "SELECT * from Publishers where Pub_id Like '99% '"
SQLCommand deletecmd = con.createcommand ();
Deletecmd.commandtext = "delete from public" where pub_id = @ pub_id ";
SQLParameter PubidPar = New SqlParameter ("@ pub_id", sqldbtype.char, 4, "pub_id");
PubidPar.SourceVersion = DATAROWVERSION.ORIGINAL;
Deletecmd.Parameters.Add (PubidPar);
SqlDataAdapter SQLADAPTER = New SqlDataAdapter ();
SQLADAPTER.SELECTCOMMAND = SELECTCMD;
SQLADAPTER.DELETECMMMAND = deletecmd;
DataSet DS = New DataSet ();
C.Open ();
SQLADAPTER.FILL (DS, "Publishers");
DataTable Table = new dataable ();
Table = DS.TABLES ["Publishers"];
Table.primarykey = new datacolumn [] // Defines the primary key to facilitate finding
{
Table.columns ["pub_id"]
}
DataRow Row = Table.Rows.Find (E.Item.cells [2] .text);
Row.delete ();
SQLADAPTER.UPDATE (TABLE); con.close ();
THIS.DATAGRID1.EDITITEMINDEX = -1;
this.bindgrid ();
}
Private void DataGrid1_disposed (Object Sender, System.EventArgs E)
{
}
}
}
The following is a screenshot of modification:
转载请注明原文地址:https://www.9cbs.com/read-46498.html