各位大哥,我有一个可以实现更新的例子,我想把它改为连接数据库里其它的表,以实现其它的表的更新,但不知为何改完后可以显示数据,但不能实现更新,请大家帮帮我这个初学.net的女孩。我改后的.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 WebApplicationCSharp
{
/// <summary>
/// VC69 的摘要描述。
/// </summary>
public class VC69 : System.Web.UI.Page
{
protected System.Web.UI.WebControls.DataGrid StudentDataGrid;
protected System.Web.UI.WebControls.Label Message;
private void Page_Load(object sender, System.EventArgs e)
{
if (!IsPostBack)
BindGridToSource();
} #region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: 此呼叫为 ASP.NET Web Form 设计工具的必要项。
//
InitializeComponent();
base.OnInit(e);
}
/// <summary>
/// 此为设计工具支援所必需的方法 - 请勿使用程式码编辑器修改
/// 这个方法的内容。
/// </summary>
private void InitializeComponent()
{
this.StudentDataGrid.CancelCommand += new System.Web.UI.WebControls.DataGridCommandEventHandler(this.StudentDataGrid_CancelCommand);
this.StudentDataGrid.EditCommand += new System.Web.UI.WebControls.DataGridCommandEventHandler(this.StudentDataGrid_EditCommand);
this.StudentDataGrid.UpdateCommand += new System.Web.UI.WebControls.DataGridCommandEventHandler(this.StudentDataGrid_UpdateCommand);
this.Load += new System.EventHandler(this.Page_Load); }
#endregion private void StudentDataGrid_EditCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
StudentDataGrid.EditItemIndex = e.Item.ItemIndex;
BindGridToSource();
} private void StudentDataGrid_CancelCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
StudentDataGrid.EditItemIndex = -1;
BindGridToSource();
Message.Text = "";
} private void StudentDataGrid_UpdateCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
SqlConnection MyConnection = new SqlConnection("server=miracle;user id=sa;password=1234;database=北风贸易;");
// 建立 UPDATE 命令字符串
string UpdateCmd = "UPDATE 产品类别 SET 类别名称 = @SName, 说明 = @PName," +
" WHERE 类别编号 = @uid";
SqlCommand MyCommand = new SqlCommand(UpdateCmd, MyConnection);
// 设定 UPDATE 命令字串中各个参数的数据类型与长度
MyCommand.Parameters.Add(new SqlParameter("@uid", SqlDbType.NVarChar, 18));
MyCommand.Parameters.Add(new SqlParameter("@SName", SqlDbType.NVarChar, 12));
MyCommand.Parameters.Add(new SqlParameter("@PName", SqlDbType.NVarChar, 12));
// 取得主索引键之值(此处为身份证号码)
MyCommand.Parameters["@uid"].Value = StudentDataGrid.DataKeys[e.Item.ItemIndex];
string[] Cols = new string[] {"@uid", "@SName","@PName", };
// 取得 TableCell 物件的数目,也就是每一数据列之储存格的数目(等於 DataGrid 伺服器控制项中数据行的数目)
int NumCols = e.Item.Cells.Count;
// 略过第一栏与第二栏。因为第一栏是按钮,第二栏的身份证号码则已在之前取得。
for (int i = 2; i<= NumCols - 1; i++)
{
TextBox CurrentTextBox = (TextBox)e.Item.Cells[i].Controls[0];
string ColValue = CurrentTextBox.Text;
// 检查字段(储存格)的内容是否为 Null 值
if (ColValue == "")
{
Message.Text = "错误: 每一个字段都必须输入数据不允许 Null 值!";
return;
}
// 将各字段(储存格)的数据指派给 UPDATE 命令中的参数
} // 开启连接
MyCommand.Connection.Open();
try
{
// 呼叫 ExecuteNonQuery() 方法以便针对数据来源执行 UPDATE 命令
MyCommand.ExecuteNonQuery();
Message.Text = "<b>已更新数据纪录</b><br>";
// 完成更新作业后使数据行跳出编辑模式
StudentDataGrid.EditItemIndex = -1;
}
catch (SqlException Exp)
{
if (Exp.Number == 2627)
Message.Text = "错误: 具有相同主索引键的数据纪录已经存在。";
else
Message.Text = "错误: 无法更新数据纪录,请确定各字段是否都已正确输入。";
}
// 关闭连接
MyCommand.Connection.Close();
BindGridToSource();
} private void BindGridToSource()
{
SqlConnection MyConnection = new SqlConnection("server=miracle;user id=sa;password=1234;database=北风贸易;");
SqlDataAdapter MyCommand = new SqlDataAdapter("SELECT 类别编号,类别名称,说明 FROM 产品类别", MyConnection);
DataSet ds = new DataSet();
MyCommand.Fill(ds, "产品类别");
StudentDataGrid.DataSource = ds.Tables["产品类别"].DefaultView;
StudentDataGrid.DataBind();
}
}
}
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 WebApplicationCSharp
{
/// <summary>
/// VC69 的摘要描述。
/// </summary>
public class VC69 : System.Web.UI.Page
{
protected System.Web.UI.WebControls.DataGrid StudentDataGrid;
protected System.Web.UI.WebControls.Label Message;
private void Page_Load(object sender, System.EventArgs e)
{
if (!IsPostBack)
BindGridToSource();
} #region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: 此呼叫为 ASP.NET Web Form 设计工具的必要项。
//
InitializeComponent();
base.OnInit(e);
}
/// <summary>
/// 此为设计工具支援所必需的方法 - 请勿使用程式码编辑器修改
/// 这个方法的内容。
/// </summary>
private void InitializeComponent()
{
this.StudentDataGrid.CancelCommand += new System.Web.UI.WebControls.DataGridCommandEventHandler(this.StudentDataGrid_CancelCommand);
this.StudentDataGrid.EditCommand += new System.Web.UI.WebControls.DataGridCommandEventHandler(this.StudentDataGrid_EditCommand);
this.StudentDataGrid.UpdateCommand += new System.Web.UI.WebControls.DataGridCommandEventHandler(this.StudentDataGrid_UpdateCommand);
this.Load += new System.EventHandler(this.Page_Load); }
#endregion private void StudentDataGrid_EditCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
StudentDataGrid.EditItemIndex = e.Item.ItemIndex;
BindGridToSource();
} private void StudentDataGrid_CancelCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
StudentDataGrid.EditItemIndex = -1;
BindGridToSource();
Message.Text = "";
} private void StudentDataGrid_UpdateCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
SqlConnection MyConnection = new SqlConnection("server=miracle;user id=sa;password=1234;database=北风贸易;");
// 建立 UPDATE 命令字符串
string UpdateCmd = "UPDATE 学生 SET 学生姓名 = @SName, 性别 = @Gender, 家长姓名 = @PName," +
"身高 = @Height, 体重 = @Weight, 血型 = @BloodType WHERE 身份证号码 = @uid";
SqlCommand MyCommand = new SqlCommand(UpdateCmd, MyConnection);
// 设定 UPDATE 命令字串中各个参数的数据类型与长度
MyCommand.Parameters.Add(new SqlParameter("@uid", SqlDbType.NVarChar, 18));
MyCommand.Parameters.Add(new SqlParameter("@SName", SqlDbType.NVarChar, 12));
MyCommand.Parameters.Add(new SqlParameter("@Gender", SqlDbType.Bit));
MyCommand.Parameters.Add(new SqlParameter("@PName", SqlDbType.NVarChar, 12));
MyCommand.Parameters.Add(new SqlParameter("@Height", SqlDbType.Real));
MyCommand.Parameters.Add(new SqlParameter("@Weight", SqlDbType.Real));
MyCommand.Parameters.Add(new SqlParameter("@BloodType", SqlDbType.NVarChar, 3));
// 取得主索引键之值(此处为身份证号码)
MyCommand.Parameters["@uid"].Value = StudentDataGrid.DataKeys[e.Item.ItemIndex];
string[] Cols = new string[] {"@uid", "@SName", "@Gender", "@PName", "@Height", "@Weight", "@BloodType"};
// 取得 TableCell 物件的数目,也就是每一数据列之储存格的数目(等於 DataGrid 伺服器控制项中数据行的数目)
int NumCols = e.Item.Cells.Count;
// 略过第一栏与第二栏。因为第一栏是按钮,第二栏的身份证号码则已在之前取得。
for (int i = 2; i<= NumCols - 1; i++)
{
TextBox CurrentTextBox = (TextBox)e.Item.Cells[i].Controls[0];
string ColValue = CurrentTextBox.Text;
// 检查字段(储存格)的内容是否为 Null 值
if (ColValue == "")
{
Message.Text = "错误: 每一个字段都必须输入数据不允许 Null 值!";
return;
}
// 将各字段(储存格)的数据指派给 UPDATE 命令中的参数
if (i == 3)
{
// 第四个字段(储存格)是「性别」,因此必须将 True 转换成 1,
// 并将 False 转换成 0,然後再指派给参数 @Gender 。
if (CurrentTextBox.Text == "True")
MyCommand.Parameters["@Gender"].Value = 1;
else
MyCommand.Parameters["@Gender"].Value = 0;
}
else
MyCommand.Parameters[Cols[i - 1]].Value = ColValue;
} // 开启连接
MyCommand.Connection.Open();
try
{
// 呼叫 ExecuteNonQuery() 方法以便针对数据来源执行 UPDATE 命令
MyCommand.ExecuteNonQuery();
Message.Text = "<b>已更新数据纪录</b><br>";
// 完成更新作业后使数据行跳出编辑模式
StudentDataGrid.EditItemIndex = -1;
}
catch (SqlException Exp)
{
if (Exp.Number == 2627)
Message.Text = "错误: 具有相同主索引键的数据纪录已经存在。";
else
Message.Text = "错误: 无法更新数据纪录,请确定各字段是否都已正确输入。";
}
// 关闭连接
MyCommand.Connection.Close();
BindGridToSource();
} private void BindGridToSource()
{
SqlConnection MyConnection = new SqlConnection("server=miracle;user id=sa;password=1234;database=北风贸易;");
SqlDataAdapter MyCommand = new SqlDataAdapter("SELECT 身份证号码,学生姓名,性别,家长姓名,身高,体重,血型 FROM 学生", MyConnection);
DataSet ds = new DataSet();
MyCommand.Fill(ds, "学生");
StudentDataGrid.DataSource = ds.Tables["学生"].DefaultView;
StudentDataGrid.DataBind();
}
}
}
{
Message.Text = Exp.ToString();
}
catch (SqlException Exp)
{
Message.Text = Exp.ToString();
}
无用啊,好象显示的错误更多了
catch (SqlException Exp)
{
Message.Text = Exp.ToString();
}
无用啊,好象显示的错误更多了你把那个更多的错误信息贴到这里没准儿就知道什么原因了,要学会看那堆错误信息,里面很可能就包含着引导你找出问题原因的内容。
骗人的,别相信,是个男的!不过为你顶住!------------------------------------------------------------------------------
你妈妈是男的吗?
赋什么值啊??
{
Message.Text = Exp.ToString();
}
之后 ,按F5有数据,但点了更新后显示错误:System.Data.SqlClient.SqlException: 在关键字 'WHERE' 附近有语法错误。 at System.Data.SqlClient.SqlCommand.ExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream) at System.Data.SqlClient.SqlCommand.ExecuteNonQuery() at WebApplicationCSharp.VC69.StudentDataGrid_UpdateCommand(Object source, DataGridCommandEventArgs e) in c:\inetpub\wwwroot\webapplication57\vc69.aspx.cs:line 114
本来想进来鄙视你这个标题的,不过看你那么着急,就不鄙视你了。呵呵~--------------------------------------------------------------------------------
为什么要BS我啊?我是抱着开玩笑的心态来发贴的啊,我是之前看见这里有人发这样类型的标题我才来发的,那你之前为什么不去BS他们啊?这种贴子还有不少呢?还有我之前不是说了下次不用这种标题了吗? Grapevine(巴巴爸爸家族一号人物) ( ) 信誉:100 2005-04-25 20:35:00 得分: 0
太没自尊了,什么冰天雪地?有点骨气好吗!~
--------------------------------------------------------------------------------
冰天雪地叫无骨气吗?我觉得如果真有人能在冰天雪地里赤身裸体那是有勇气的表现?你这样说那我想你也不会有勇气这样做了。当然最后那个词是开玩笑的
楼主先上个冰天雪地赤身裸体照片 ~~
-------------------------------------------------------------------------------
你给个理由给我为什么要放??