This program involves adding, modifying, and deleting databases.
I am too lazy to write it. I can paste the interface and draw it according to the interface. This example Database: the pubs database attached to sqlserver2000. You can see the connection string clearly. If you want to run the command on this machine, change UID and PWD to your own SQL login username and password.
Create a Web page named Add. aspx.
Interface Design
Add. aspx code:
<% @ Page Language = "C #" codebehind = "Add. aspx. cs" autoeventwireup = "false" inherits = "teachshow. charpter7.accessdatabase. Add" %>
<! Doctype HTML public "-// W3C // dtd html 4.0 transitional // en">
<HTML>
<Head>
<Title> Add </title>
<Link href = ".../../style.css" type = "text/CSS" rel = "stylesheet">
<Meta content = "Microsoft Visual Studio. NET 7.1" name = "generator">
<Meta content = "C #" name = "code_language">
<Meta content = "JavaScript" name = "vs_defaultclientscript">
<Meta content = "http://schemas.microsoft.com/intellisense/ie5" name = "vs_targetschema">
</Head>
<Body ms_positioning = "gridlayout">
<Form ID = "form1" method = "Post" runat = "server">
<Div align = "center">
<Center>
<Table class = "smallblack" Height = "318" cellspacing = "0" cellpadding = "0" width = "429" border = "0">
<Tr>
<TD class = "title" valign = "TOP" width = "429" colspan = "2" Height = "31"> Add a new publisher </TD>
</Tr>
<Tr>
<TD valign = "TOP" width = "79" Height = "23"> issuer ID: </TD>
<TD valign = "TOP" width = "350" Height = "23"> <asp: textbox id = "textbox1" runat = "server" Height = "18px" cssclass = "smallred"> </ASP: textbox> <font face = ""> (4 digits in total) </font> </TD>
</Tr>
<Tr>
<TD valign = "TOP" width = "79" Height = "23"> <font face = ""> name: </font> </TD>
<TD valign = "TOP" width = "350" Height = "23"> <asp: textbox id = "textbox2" runat = "server" Height = "18px" cssclass = "smallred"> </ASP: textbox> </TD>
</Tr>
<Tr>
<TD valign = "TOP" width = "79" Height = "23"> <font face = ""> City: </font> </TD>
<TD valign = "TOP" width = "350" Height = "23"> <asp: textbox id = "textbox3" runat = "server" Height = "18px" cssclass = "smallred"> </ASP: textbox> </TD>
</Tr>
<Tr>
<TD valign = "TOP" width = "79" Height = "23"> <font face = ""> Province: </font> </TD>
<TD valign = "TOP" width = "350" Height = "23"> <asp: textbox id = "textbox4" runat = "server" Height = "18px" cssclass = "smallred"> </ASP: textbox> <font face = ""> (2 characters) </font> </TD>
</Tr>
<Tr>
<TD valign = "TOP" width = "79" Height = "24"> <font face = ""> country: </font> </TD>
<TD valign = "TOP" width = "350" Height = "24"> <asp: textbox id = "textbox5" runat = "server" Height = "18px" cssclass = "smallred"> </ASP: textbox> </TD>
</Tr>
<Tr>
<TD valign = "TOP" align = "center" width = "429" colspan = "2" Height = "24"> <asp: linkbutton id = "linkbutton1" runat = "server"> submit to database </ASP: linkbutton> </TD>
</Tr>
<Tr>
<TD width = "429" Height = "147" valign = "TOP" colspan = "2">
<Asp: DataGrid id = "datagrid1" runat = "server" Height = "120px" cssclass = "general" width = "428px">
<Itemstyle width = "50px"> </itemstyle>
<Columns>
<Asp: editcommandcolumn buttontype = "linkbutton" updatetext = "Update" canceltext = "cancel" edittext = "edit">
<Headerstyle width = "60px">
</ASP: editcommandcolumn>
<Asp: buttoncolumn text = "delete" commandname = "delete"> </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.html controls;
Namespace teachshow. charpter7.accessdatabase
{
/// <Summary>
/// Add abstract description.
/// </Summary>
Public class Add: 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 user code here to initialize the page
If (! This. ispostback)
{
This. bindgrid ();
}
}
# Code generated by region web Form Designer
Override protected void oninit (eventargs E)
{
//
// Codegen: This call is required by the Asp.net web form designer.
//
Initializecomponent ();
Base. oninit (E );
}
/// <Summary>
/// The designer supports the required methods-do not use the code editor to modify
/// Content of this method.
/// </Summary>
Private void initializecomponent ()
{
This. linkbutton1.click + = new system. eventhandler (this. linkbutton#click );
This. Maid + = new system. eventhandler (this. Maid disposed );
This. Maid + = new system. Web. UI. webcontrols. Fig cancelcommand );
This. Maid + = new system. Web. UI. webcontrols. Maid (this. Maid editcommand );
This. Maid + = new system. Web. UI. webcontrols. Maid (this. Maid updatecommand );
This. Maid + = new system. Web. UI. webcontrols. Maid (this. Maid deletecommand );
This. Maid + = new system. eventhandler (this. Maid selectedindexchanged );
This. Load + = new system. eventhandler (this. page_load );
}
# Endregion
Private void linkbutton#click (Object sender, system. eventargs E)
{
Addpublisher ();
}
/// <Summary>
/// Add
/// </Summary>
Private void addpublisher ()
{
String SQL = "insert into Publishers (pub_id, pub_name, city, state, country) values (@ pubid, @ pubname, @ 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.exe cutenonquery ();
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 '20140901'", con );
Dataset myds = new dataset ();
Mysqlcom. Fill (myds, "Publishers ");
This. datagrid1.datasource = myds. Tables ["Publishers"]. defaultview;
This. datagrid1.databind ();
}
/// <Summary>
/// Triggered when you click Edit
/// </Summary>
/// <Param name = "Source"> </param>
/// <Param name = "E"> </param>
Private void datagrid1_editcommand (Object source, system. Web. UI. webcontrols. datagridcommandeventargs E)
{
This. Maid = (INT) E. Item. itemindex;
This. bindgrid ();
}
/// <Summary>
/// This event is triggered when you click Cancel.
/// </Summary>
/// <Param name = "Source"> </param>
/// <Param name = "E"> </param>
Private void datagrid1_cancelcommand (Object source, system. Web. UI. webcontrols. datagridcommandeventargs E)
{
This. Maid [0]. headertext = "canceled ";
This. Maid =-1;
This. bindgrid ();
}
Private void maid (Object sender, system. eventargs E)
{
}
/// <Summary>
/// Trigger this event when you click Update.
/// </Summary>
/// <Param name = "Source"> </param>
/// <Param name = "E"> </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 '2013 '";
Sqldataadapter sqladatper = new sqldataadapter ();
Sqladatper. selectcommand = selectcmd;
Dataset DS = new dataset ();
Con. open ();
Sqladatper. Fill (DS, "Publishers ");
Con. 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]). text;
Row ["state"] = (textbox) (E. Item. cells [5]. controls [0]). text;
Row ["country"] = (textbox) (E. Item. cells [6]. controls [0]). text;
Con. open ();
Sqladatper. Update (table );
Con. Close ();
This. Maid =-1;
This. bindgrid ();
}
/// <Summary>
/// This event is triggered when you click Delete.
/// </Summary>
/// <Param name = "Source"> </param>
/// <Param name = "E"> </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 '2013 '";
Sqlcommand deletecmd = con. createcommand ();
Deletecmd. commandtext = "delete from publishers 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. deletecommand = deletecmd;
Dataset DS = new dataset ();
Con. open ();
Sqladapter. Fill (DS, "Publishers ");
Datatable table = new datatable ();
Table = Ds. Tables ["Publishers"];
Table. primarykey = new datacolumn [] // defines the primary key for ease of searching
{
Table. Columns ["pub_id"]
};
Datarow ROW = table. Rows. Find (E. Item. cells [2]. Text );
Row. Delete ();
Sqladapter. Update (table );
Con. Close ();
This. Maid =-1;
This. bindgrid ();
}
Private void maid (Object sender, system. eventargs E)
{
}
}
}