[ÕªÒª]Ç°Ã漸¸öÀý×ÓѧϰÁË´ÓDataBaseÀïÃæ¶ÁÊý¾Ý£¬ÏÂÃæÎÒÃÇѧϰ½«Êý¾Ý²åÈëµ½Êý¾Ý¿âÖУ¬¿ÉÒÔÏòÒ³ÖÐÌí¼Ó¼òµ¥µÄÊäÈë´°Ì壬²¢ÔÚ´°ÌåÌύʼþ´¦Àí³ÌÐòÖÐÖ´ÐвåÈëÃüÁî¡£ÓëÇ°Á½¸öʾÀýÒ»Ñù£¬Ê¹ÓÃÃüÁî¶ÔÏóµÄ Parameters ¼¯ºÏÌî³äÃüÁîµÄÖµ¡£×¢Ò⣬ÔÚÊÔͼ²åÈëµ½Êý¾Ý¿âÖÐ֮ǰ£¬»¹Òª¼ì²éÒÔÈ·±£ËùÐèµÄÖµ·Ç¿Õ¡£Õ⽫·ÀÖ¹ÓëÊý...
Ç°Ã漸¸öÀý×ÓѧϰÁË´ÓDataBaseÀïÃæ¶ÁÊý¾Ý£¬ÏÂÃæÎÒÃÇѧϰ½«Êý¾Ý²åÈëµ½Êý¾Ý¿âÖУ¬¿ÉÒÔÏòÒ³ÖÐÌí¼Ó¼òµ¥µÄÊäÈë´°Ì壬²¢ÔÚ´°ÌåÌύʼþ´¦Àí³ÌÐòÖÐÖ´ÐвåÈëÃüÁî¡£ÓëÇ°Á½¸öʾÀýÒ»Ñù£¬Ê¹ÓÃÃüÁî¶ÔÏóµÄ Parameters ¼¯ºÏÌî³äÃüÁîµÄÖµ¡£×¢Ò⣬ÔÚÊÔͼ²åÈëµ½Êý¾Ý¿âÖÐ֮ǰ£¬»¹Òª¼ì²éÒÔÈ·±£ËùÐèµÄÖµ·Ç¿Õ¡£Õ⽫·ÀÖ¹ÓëÊý¾Ý¿âµÄ×Ö¶ÎÔ¼ÊøÒâÍâ³åÍ»¡£»¹ÐèÔÚ try/catch ¿éµÄÄÚ²¿Ö´ÐвåÈëÃüÁÒÔ·À²åÈëÐеÄÖ÷¼üÒѾ´æÔÚ¡£
ÏÈ¿´aspxÎļþ£º
<body MS_POSITIONING="GridLayout">
<form runat="server" ID="Form1">
<h3><font face="ËÎÌå">²åÈë¾ßÓÐÑéÖ¤µÄÊý¾ÝÐÐ</font></h3>
<table width="95%">
<tr>
<td valign="top">
<ASP:DataGrid id="MyDataGrid" runat="server" Width="700" BackColor="#ccccff" BorderColor="black" ShowFooter="false" CellPadding="3" CellSpacing="0" Font-Name="ËÎÌå" Font-Size="8pt" HeaderStyle-BackColor="#aaaadd" EnableViewState="false" />
</td>
<td valign="top">
<table style="FONT: 9pt ËÎÌå">
<tr>
<td colspan="2" bgcolor="#aaaadd" style="FONT:10.5pt ËÎÌå">Ìí¼ÓÐÂ×÷Õߣº</td>
</tr>
<tr>
<td nowrap>×÷Õß ID£º</td>
<td>
<input type="text" id="au_id" value="000-00-0000" runat="server" NAME="au_id">
<asp:RequiredFieldValidator id="au_idReqVal" ControlToValidate="au_id" Display="Static" Font-Name="Verdana" Font-Size="12" runat="server"> *</asp:RequiredFieldValidator>
</td>
</tr>
<tr>
<td nowrap>ÐÕÊÏ£º</td>
<td>
<input type="text" id="au_lname" value="Doe" runat="server" NAME="au_lname">
<asp:RequiredFieldValidator id="au_lnameReqVal" ControlToValidate="au_lname" Display="Static" Font-Name="Verdana" Font-Size="12" runat="server"> *</asp:RequiredFieldValidator>
</td>
</tr>
<tr>
<td nowrap>Ãû×Ö£º</td>
<td>
<input type="text" id="au_fname" value="John" runat="server" NAME="au_fname">
<asp:RequiredFieldValidator id="au_fnameReqVal" ControlToValidate="au_fname" Display="Static" Font-Name="Verdana" Font-Size="12" runat="server"> *</asp:RequiredFieldValidator>
</td>
</tr>
<tr>
<td>µç»°£º</td>
<td><nobr> <input type="text" id="phone" value="808 555-5555" runat="server" NAME="phone">
<asp:RequiredFieldValidator id="phoneReqVal" ControlToValidate="phone" Display="Static" Font-Name="Verdana" Font-Size="12" runat="server"> *</asp:RequiredFieldValidator></nobr>
</td>
</tr>
<tr>
<td>µØÖ·£º</td>
<td><input type="text" id="address" value="One Microsoft Way" runat="server" NAME="address"></td>
</tr>
<tr>
<td>³ÇÊУº</td>
<td><input type="text" id="city" value="Redmond" runat="server" NAME="city"></td>
</tr>
<tr>
<td>ÖÝ£º</td>
<td>
<select id="state" runat="server" NAME="state">
<option selected>CA</option>
<option>IN</option>
<option>KS</option>
<option>MD</option>
<option>MI</option>
<option>OR</option>
<option>TN</option>
<option>UT</option>
</select>
</td>
</tr>
<tr>
<td nowrap>ÓÊÕþ±àÂ룺</td>
<td><input type="text" id="zip" value="98005" runat="server" NAME="zip"></td>
</tr>
<tr>
<td>ж¨£º</td>
<td>
<select id="contract" runat="server" NAME="contract">
<option value="0" selected>¼Ù</option>
<option value="1">Õæ</option>
</select>
</td>
</tr>
<tr>
<td></td>
<td style="PADDING-TOP:15px">
<input type="submit" value="Ìí¼Ó×÷Õß" runat="server" ID="Submit1" NAME="Submit1">
</td>
</tr>
<tr>
<td colspan="2" style="PADDING-TOP:15px" align="middle">
<span id="Message" EnableViewState="false" runat="server">
<asp:RegularExpressionValidator id="RegularExpressionValidator1" ASPClass="RegularExpressionValidator" ControlToValidate="zip" ValidationExpression="[0-9]{5}" Display="Dynamic" Font-Name="Arial" Font-Size="11" runat="server">* ÓÊÕþ±àÂë±ØÐëÊÇ 5 λÊý×Ö<br></asp:RegularExpressionValidator>
<asp:RegularExpressionValidator id="phoneRegexVal" ControlToValidate="phone" ValidationExpression="[0-9]{3} [0-9]{3}-[0-9]{4}" Display="Dynamic" Font-Name="Arial" Font-Size="11" runat="server">* µç»°ºÅÂë¸ñʽ±ØÐëΪ£ºXXX XXX-XXXX <br></asp:RegularExpressionValidator> <asp:RegularExpressionValidator id="au_idRegexVal" ControlToValidate="au_id" ValidationExpression="[0-9]{3}-[0-9]{2}-[0-9]{4}" Display="Dynamic" Font-Name="Arial" Font-Size="11" runat="server">* ×÷Õß ID ±ØÐëÊÇÊý×Ö£ºXXX-XX-XXXX <br></asp:RegularExpressionValidator></span>
</td>
</tr>
</table>
</td>
</tr>
</table>
</form>
</body>
aspx.csÎļþ
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
namespace study
{
/// <summary>
/// datagrid_5 µÄժҪ˵Ã÷¡£
/// </summary>
public class datagrid_5 : System.Web.UI.Page
{
protected System.Web.UI.WebControls.DataGrid MyDataGrid;
protected System.Web.UI.WebControls.RequiredFieldValidator au_idReqVal;
protected System.Web.UI.WebControls.RequiredFieldValidator au_lnameReqVal;
protected System.Web.UI.WebControls.RequiredFieldValidator au_fnameReqVal;
protected System.Web.UI.WebControls.RequiredFieldValidator phoneReqVal;
protected System.Web.UI.WebControls.RegularExpressionValidator RegularExpressionValidator1;
protected System.Web.UI.WebControls.RegularExpressionValidator phoneRegexVal;
protected System.Web.UI.WebControls.RegularExpressionValidator au_idRegexVal;
protected System.Web.UI.HtmlControls.HtmlInputText au_id;
protected System.Web.UI.HtmlControls.HtmlInputText au_lname;
protected System.Web.UI.HtmlControls.HtmlInputText au_fname;
protected System.Web.UI.HtmlControls.HtmlInputText phone;
protected System.Web.UI.HtmlControls.HtmlInputText address;
protected System.Web.UI.HtmlControls.HtmlInputText city;
protected System.Web.UI.HtmlControls.HtmlSelect state;
protected System.Web.UI.HtmlControls.HtmlInputText zip;
protected System.Web.UI.HtmlControls.HtmlSelect contract;
protected System.Web.UI.HtmlControls.HtmlInputButton Submit1;
protected System.Web.UI.HtmlControls.HtmlGenericControl Message;
SqlConnection myConnection;
private void Page_Load(object sender, System.EventArgs e)
{
myConnection = new SqlConnection("user id=sa;password=;initial catalog=pubs;data source=jeff");
if (!IsPostBack)
BindGrid();
}
#region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
//
// CODEGEN£º¸Ãµ÷ÓÃÊÇ ASP.NET Web ´°ÌåÉè¼ÆÆ÷Ëù±ØÐèµÄ¡£
//
InitializeComponent();
base.OnInit(e);
}
/// <summary>
/// Éè¼ÆÆ÷Ö§³ÖËùÐèµÄ·½·¨ - ²»ÒªÊ¹ÓôúÂë±à¼Æ÷ÐÞ¸Ä
/// ´Ë·½·¨µÄÄÚÈÝ¡£
/// </summary>
private void InitializeComponent()
{
this.Submit1.ServerClick += new System.EventHandler(this.Submit1_ServerClick);
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion
private void Submit1_ServerClick(object sender, System.EventArgs e)
{
Message.InnerHtml = "";
if (Page.IsValid)
{
String insertCmd = "insert into Authors (au_id, au_lname, au_fname, phone, address, city, state, zip, contract) values (@Id, @LName, @FName, @Phone, @Address, @City, @State, @Zip, @Contract)";
SqlCommand myCommand = new SqlCommand(insertCmd, myConnection);
myCommand.Parameters.Add(new SqlParameter("@Id", SqlDbType.NVarChar, 11));
myCommand.Parameters["@Id"].Value = au_id.Value;
myCommand.Parameters.Add(new SqlParameter("@LName", SqlDbType.NVarChar, 40));
myCommand.Parameters["@LName"].Value = au_lname.Value;
myCommand.Parameters.Add(new SqlParameter("@FName", SqlDbType.NVarChar, 20));
myCommand.Parameters["@FName"].Value = au_fname.Value;
myCommand.Parameters.Add(new SqlParameter("@Phone", SqlDbType.NChar, 12));
myCommand.Parameters["@Phone"].Value = phone.Value;
myCommand.Parameters.Add(new SqlParameter("@Address", SqlDbType.NVarChar, 40));
myCommand.Parameters["@Address"].Value = address.Value;
myCommand.Parameters.Add(new SqlParameter("@City", SqlDbType.NVarChar, 20));
myCommand.Parameters["@City"].Value = city.Value;
myCommand.Parameters.Add(new SqlParameter("@State", SqlDbType.NChar, 2));
myCommand.Parameters["@State"].Value = state.Value;
myCommand.Parameters.Add(new SqlParameter("@Zip", SqlDbType.NChar, 5));
myCommand.Parameters["@Zip"].Value = zip.Value;
myCommand.Parameters.Add(new SqlParameter("@Contract", SqlDbType.NVarChar,1));
myCommand.Parameters["@Contract"].Value = contract.Value;
myCommand.Connection.Open();
try
{
myCommand.ExecuteNonQuery();
Message.InnerHtml = "<b>ÒÑÌí¼Ó¼Ç¼</b><br>" + insertCmd + "<p>";
}
catch (SqlException ex)
{
if (ex.Number == 2627)
Message.InnerHtml = "´íÎó£ºÒÑ´æÔÚ¾ßÓÐÏàͬÖ÷¼üµÄ¼Ç¼<p>";
else
Message.InnerHtml = "´íÎó£ºÎ´ÄÜÌí¼Ó¼Ç¼£¬ÇëÈ·±£ÕýÈ·ÌîдÁË×Ö¶Î<p>";
Message.Style["color"] = "red";
}
myCommand.Connection.Close();
}
BindGrid();
}
public void BindGrid()
{
SqlDataAdapter myCommand = new SqlDataAdapter("select * from Authors", myConnection);
DataSet ds = new DataSet();
myCommand.Fill(ds, "Authors");
MyDataGrid.DataSource=ds.Tables["Authors"].DefaultView;
MyDataGrid.DataBind();
}
}
}
……