代码:
<%@ Import Namespace="System.Data"%>
<%@ Import Namespace="System.Data.SqlClient"%>
<script Language="C#" runat="server">
void Page_Load()
{
string strConn,strSQL;
SqlConnection objConn;
SqlDataAdapter objAdapter;
SqlCommandBuilder objBuilder=null;
DataSet objDataSet = new DataSet();
strConn="server=CHANGYUN;";
strConn+="database=Northwind;uid=sa;password=13965012674;";
strSQL="select FirstName,LastName from Employees";
objConn = new SqlConnection(strConn);
objAdapter = new SqlDataAdapter(strSQL,objConn);
objAdapter.Fill(objDataSet,"Employees");
dd1.DataSource=objDataSet.Tables["Employees"].DefaultView;
dd1.DataBind();
DataTable objTable;
DataRow objNewRow;
objTable = objDataSet.Tables["Employees"];
objNewRow=objTable.NewRow();
objNewRow["FirstName"]="汪";
objNewRow["LastName"]="永国";
objTable.Rows.Add(objNewRow);
objNewRow=objTable.NewRow();
objNewRow["FirstName"]="张";
objNewRow["LastName"]="辽";
objTable.Rows.Add(objNewRow);
dd2.DataSource=objTable.DefaultView;
dd2.DataBind();
DataRow objRow;
objRow= objTable.Rows[3];
objRow["FirstName"]="吴";
objRow["LastName"]="欢";
dd3.DataSource=objTable.DefaultView;
dd3.DataBind();
objBuilder=new SqlCommandBuilder(objAdapter);
objAdapter.UpdateCommand=objBuilder.GetUpdateCommand();
objAdapter.InsertCommand=objBuilder.GetInsertCommand();
objAdapter.DeleteCommand=objBuilder.GetDeleteCommand();
objAdapter.Update(objDataSet,"Employees");
strSQL="select FirstName,LastName from Employees";
objConn.Open();
SqlCommand objCmd = new SqlCommand(strSQL,objConn);
dd.DataSource=objCmd.ExecuteReader(CommandBehavior.CloseConnection);
dd.DataBind();
}
</script>
<html>
<body>
<asp:DataGrid ID="dd1" runat="server"/><p>
<asp:DataGrid ID="dd2" runat="server"/><p>
<asp:DataGrid ID="dd3" runat="server"/><p>
<asp:DataGrid ID="dd" runat="server"/><p>
</body>
</html>
错误提示:
异常详细信息: System.InvalidOperationException: 对于不返回任何键列信息的 SelectCommand 不支持 UpdateCommand 的动态 SQL 生成。