为什么点击按钮无法添加数据?提示这么个东西:“当传递具有新行的 DataRow 集合时,更新要求有效的 InsertCommand。 ”
表的格式:(id自动编号)
id name chinese math
1 周荣 100 100
2 jack 90 90
dataset.aspx如下:
<%@ Page language="c#" Codebehind="Dataset.aspx.cs" AutoEventWireup="false" Inherits="dbms.dataset" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
<HEAD>
<title>dataset</title>
<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">
<asp:label id="Label1" style="Z-INDEX: 101; LEFT: 24px; POSITION: absolute; TOP: 32px" runat="server"
Width="184px" Height="32px" BackColor="LavenderBlush" BorderColor="#0000C0">DataSet演示</asp:label>
<asp:Button id="Button1" style="Z-INDEX: 105; LEFT: 48px; POSITION: absolute; TOP: 232px" runat="server"
BackColor="Tomato" Text="添加学生资料1"></asp:Button>
<asp:datagrid id="DataGrid1" style="Z-INDEX: 102; LEFT: 48px; POSITION: absolute; TOP: 88px" runat="server"
Width="408px" HeaderStyle-BackColor="#ff3333" AutoGenerateColumns="False">
<Columns>
<asp:BoundColumn HeaderText="编号" DataField="id"></asp:BoundColumn>
<asp:BoundColumn HeaderText="姓名" DataField="name"></asp:BoundColumn>
<asp:BoundColumn HeaderText="语文" DataField="chinese"></asp:BoundColumn>
<asp:BoundColumn HeaderText="数学" DataField="math"></asp:BoundColumn>
</Columns>
</asp:datagrid></form>
</body>
</HTML>
dataset.aspx.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 dbms
{
/// <summary>
/// dataset 的摘要说明。
/// </summary>
public class dataset : System.Web.UI.Page
{
protected System.Web.UI.WebControls.Label Label1;
protected System.Web.UI.WebControls.Button Button1;
protected System.Web.UI.WebControls.DataGrid DataGrid1;
public SqlConnection conn;
public string sql;
private void Page_Load(object sender, System.EventArgs e)
{
// 在此处放置用户代码以初始化页面
conn=new SqlConnection("server=localhost;uid=sa;pwd=;database=test");
conn.Open();
sql="select * from score";
DataSet ds=new DataSet();
SqlDataAdapter myadapt=new SqlDataAdapter(sql,conn);
myadapt.Fill(ds,"score");
DataGrid1.DataSource=ds;
DataGrid1.DataMember="score";
DataGrid1.DataBind();
ds.Clear();
}
#region Web 窗体设计器生成的代码
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
//
InitializeComponent();
base.OnInit(e);
}
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.Button1.Click += new System.EventHandler(this.Button1_Click);
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion
private void Button1_Click(object sender, System.EventArgs e)
{
DataSet addscore=new DataSet();
SqlDataAdapter myscore=new SqlDataAdapter(sql,conn);
myscore.Fill(addscore,"score");
DataGrid1.DataSource=addscore;
DataGrid1.DataMember="score";
DataGrid1.DataBind();
DataRow rs=addscore.Tables["score"].NewRow();
rs["name"]="jack";
rs["chinese"]=60;
rs["math"]=80;
addscore.Tables["score"].Rows.Add(rs);
myscore.Update(addscore,"score");
}
}
}