下面的代码是为了实现更新数据库,可是没有实现数据库的更新,提示数据库更新失败!请问如何更改啊???
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 UpdateDatabase
{
/// <summary>
/// WebForm1 的摘要说明。
/// </summary>
public class WebForm1 : System.Web.UI.Page
{
protected System.Web.UI.WebControls.DataGrid DataGrid1;
protected SqlConnection myConn=new SqlConnection();
// protected string myConn;
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.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.Load += new System.EventHandler(this.Page_Load);
}
#endregion
private void BindGrid()
{
SqlConnection myConn=new SqlConnection("server=HAOJIE;uid=sa;pwd=;database=Aspnet");
string strCmd="select * from stu_score";
SqlDataAdapter sdr=new SqlDataAdapter(strCmd,myConn);
DataSet ds=new DataSet();
sdr.Fill(ds,"scores");
this.DataGrid1.DataKeyField="Sno";
this.DataGrid1.DataSource=ds.Tables["scores"].DefaultView;
this.DataGrid1.DataBind();
}
private void DataGrid1_EditCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
this.DataGrid1.EditItemIndex=(int)e.Item.ItemIndex;
this.BindGrid();
}
private void DataGrid1_CancelCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
this.DataGrid1.EditItemIndex=-1;
this.BindGrid();
}
private void DataGrid1_UpdateCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
string stuSno=this.DataGrid1.DataKeys[e.Item.ItemIndex].ToString();
string Sname=((TextBox)(e.Item.Cells[1].Controls[0])).Text;
string yuwen=((TextBox)(e.Item.Cells[2].Controls[0])).Text;
string shuxue=((TextBox)(e.Item.Cells[3].Controls[0])).Text;
string yingyu=((TextBox)(e.Item.Cells[4].Controls[0])).Text;
string zhengzhi=((TextBox)(e.Item.Cells[5].Controls[0])).Text;
string jisuanji=((TextBox)(e.Item.Cells[6].Controls[0])).Text;
SqlConnection myConn=new SqlConnection("server=HAOJIE;uid=sa;pwd=;database=Aspnet");
string UpdateCmd="UPDATE stu_score where Sno='"+stuSno+"'";
SqlCommand myCmd1=new SqlCommand(UpdateCmd,myConn);
myCmd1.Connection.Open();
// Response.Write("更新成功!");
try
{
myCmd1.ExecuteNonQuery();
this.DataGrid1.EditItemIndex=-1;
}
catch
{
Response.Write("<script language='javascript'>alert('数据库更新失败!请检查错误!')</script>");
}
this.BindGrid();
}
}
}