(ASP.NET)修改和刪除DataGrid行——資料庫訪問

來源:互聯網
上載者:User
本程式涉及到資料庫的添加,修改和刪除操作。

 

懶得寫了,把介面貼出來,照著介面畫就可以了。本例資料庫:sqlserver2000附帶的pubs資料庫,看一下連接字串就很清楚了。如果要在本機器上運行,把uid和pwd改成你自己sql登陸使用者名稱和密碼。

 

建立一個web頁面,命名為:add.aspx。

介面設計

 

add.aspx代碼:

<%@ 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">添加一個新的發行者</td>

</tr>

<tr>

<td valign="top" width="79" height="23">發行者id:</td>

<td valign="top" width="350" height="23"><asp:textbox id="textbox1" runat="server" height="18px" cssclass="smallred"></asp:textbox><font face="宋體">(以99打頭,共4位元字)</font></td>

</tr>

<tr>

<td valign="top" width="79" height="23"><font face="宋體">姓名:</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="宋體">城市:</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="宋體">省份:</font></td>

<td valign="top" width="350" height="23"><asp:textbox id="textbox4" runat="server" height="18px" cssclass="smallred"></asp:textbox><font face="宋體">(2個字元)</font></td>

</tr>

<tr>

<td valign="top" width="79" height="24"><font face="宋體">國家:</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">提交到資料庫</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="更新" canceltext="取消" edittext="編輯">

<headerstyle width="60px"></headerstyle>

</asp:editcommandcolumn>

<asp:buttoncolumn text="刪除" commandname="delete"></asp:buttoncolumn>

</columns>

</asp:datagrid></td>

</tr>

</table>

</center>

</div>

</form>

</body>

</html>

 

add.asp.cs代碼:

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

{

/// <summary>

/// add 的摘要說明。

/// </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)

{

// 在此處放置使用者代碼以初始化頁面

if(!this.ispostback)

{

this.bindgrid();

}

}

 

#region web 表單設計器產生的程式碼

override protected void oninit(eventargs e)

{

//

// codegen: 該調用是 asp.net web 表單設計器所必需的。

//

initializecomponent();

base.oninit(e);

}

 

/// <summary>

/// 設計器支援所需的方法 - 不要使用代碼編輯器修改

/// 此方法的內容。

/// </summary>

private void initializecomponent()

{

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);

 

}

#endregion

 

private void linkbutton1_click(object sender, system.eventargs e)

{

addpublisher();

}

 

/// <summary>

/// 添加

/// </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.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();

}

 

/// <summary>

/// 點擊編輯時觸發事件

/// </summary>

/// <param name="source"></param>

/// <param name="e"></param>

private void datagrid1_editcommand(object source, system.web.ui.webcontrols.datagridcommandeventargs e)

{

this.datagrid1.edititemindex=(int)e.item.itemindex;

this.bindgrid();

}

 

/// <summary>

/// 點擊取消時觸發該事件。

/// </summary>

/// <param name="source"></param>

/// <param name="e"></param>

private void datagrid1_cancelcommand(object source, system.web.ui.webcontrols.datagridcommandeventargs e)

{

this.datagrid1.columns[0].headertext="已取消";

this.datagrid1.edititemindex=-1;

this.bindgrid();

}

 

private void datagrid1_selectedindexchanged(object sender, system.eventargs e)

{

 

}

 

/// <summary>

/// 點擊更新時觸發該事件。

/// </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 '99%'";

 

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.datagrid1.edititemindex=-1;

this.bindgrid();

}

 

/// <summary>

/// 點擊刪除時觸發該事件

/// </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 '99%'";

 

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[]//定義主鍵,便於尋找

{

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)

{

 

}

}

}

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.