首先说明,本人纯粹菜鸟,没有系统的学过.NET只是看着文档的范例在开发。
帖子中放不下代码,有兴趣的可以联系我([email protected],qq:16967436)。
如有高手解决此问题,一定高分回报,解决后最好通知一声([email protected],qq:16967436),好及时给分。
遇到的问题如下:
无法使用DataGrid控件中的UpdateCommand来更新数据库中的数据。可能原因是没有为DataGrid设置DataKeyField,因为数据库表没有主键,数据库表如下(节选):
-------------------------------------------------------------
座位数 教室号 周次 周几 节次 用途
55 17306 1 二 9-12
55 17306 2 二 9-12
55 17306 3 二 9-12
55 17306 4 二 9-12
55 17306 5 二 9-12 选修课
55 17306 6 二 9-12 选修课
55 17306 7 二 9-12 选修课
55 17306 8 二 9-12 选修课
55 17306 9 二 9-12 选修课
70 17105 16 六 5-6
70 17105 17 六 5-6
70 17105 1 六 7-8 辅修课
70 17105 2 六 7-8 辅修课
70 17105 3 六 7-8 辅修课
70 17105 4 六 7-8 辅修课
---------------------------------------------------------------
其中“周次”就是第几周,取值1-17;“周几”就是星期几;
“节次”就是第几节课,取值有“1-2,3-4,5-6,7-8,9-12”。
所以现在无法设置主键,请问有何设置主键的高见?
帖子中放不下代码,有兴趣的可以联系我([email protected],qq:16967436)。
如有高手解决此问题,一定高分回报,解决后最好通知一声([email protected],qq:16967436),好及时给分。
遇到的问题如下:
无法使用DataGrid控件中的UpdateCommand来更新数据库中的数据。可能原因是没有为DataGrid设置DataKeyField,因为数据库表没有主键,数据库表如下(节选):
-------------------------------------------------------------
座位数 教室号 周次 周几 节次 用途
55 17306 1 二 9-12
55 17306 2 二 9-12
55 17306 3 二 9-12
55 17306 4 二 9-12
55 17306 5 二 9-12 选修课
55 17306 6 二 9-12 选修课
55 17306 7 二 9-12 选修课
55 17306 8 二 9-12 选修课
55 17306 9 二 9-12 选修课
70 17105 16 六 5-6
70 17105 17 六 5-6
70 17105 1 六 7-8 辅修课
70 17105 2 六 7-8 辅修课
70 17105 3 六 7-8 辅修课
70 17105 4 六 7-8 辅修课
---------------------------------------------------------------
其中“周次”就是第几周,取值1-17;“周几”就是星期几;
“节次”就是第几节课,取值有“1-2,3-4,5-6,7-8,9-12”。
所以现在无法设置主键,请问有何设置主键的高见?
<%@ Page language="c#" Codebehind="WebForm1.aspx.cs" AutoEventWireup="false" Inherits="WebApplication1.WebForm1" %>
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.SqlClient" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
<HEAD>
<title>WebForm1</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 style="FONT: 10pt verdana">
<form id="WebForm2" method="post" runat="server">
<h1><FONT face="verdana"> 教室信息管理</FONT></h1>
<table borderColor="navajowhite" border="1">
<tr>
<td style="HEIGHT: 31px" vAlign="top" borderColor="black" bgColor="#ccccff" colSpan="5"><FONT face="verdana">请输入查询条件:</FONT>
</td>
</tr>
<tr style="FONT: 12pt verdana" borderColor="black" bgColor="#ccccff">
<td vAlign="top"> 第
<asp:dropdownlist id="DropDownList1" runat="server">
<asp:ListItem value="1" selected="False">1</asp:ListItem>
<asp:ListItem value="2" selected="False">2</asp:ListItem>
<asp:ListItem value="3" selected="False">3</asp:ListItem>
<asp:ListItem value="4" selected="False">4</asp:ListItem>
<asp:ListItem value="5" selected="False">5</asp:ListItem>
<asp:ListItem value="6" selected="False">6</asp:ListItem>
<asp:ListItem value="7" selected="False">7</asp:ListItem>
<asp:ListItem value="8" selected="False">8</asp:ListItem>
<asp:ListItem value="9" selected="False">9</asp:ListItem>
<asp:ListItem value="10" selected="False">10</asp:ListItem>
<asp:ListItem value="11" selected="False">11</asp:ListItem>
<asp:ListItem value="12" selected="False">12</asp:ListItem>
<asp:ListItem value="13" selected="False">13</asp:ListItem>
<asp:ListItem value="14" selected="False">14</asp:ListItem>
<asp:ListItem value="15" selected="False">15</asp:ListItem>
<asp:ListItem value="16" selected="False">16</asp:ListItem>
<asp:ListItem value="17" selected="False">17</asp:ListItem>
<asp:ListItem value="18" selected="False">18</asp:ListItem>
</asp:dropdownlist> 周</td>
<td vAlign="top"> 星期
<asp:dropdownlist id="DropDownList2" runat="server">
<asp:ListItem value="一" selected="False">一</asp:ListItem>
<asp:ListItem value="二" selected="False">二</asp:ListItem>
<asp:ListItem value="三" selected="False">三</asp:ListItem>
<asp:ListItem value="四" selected="False">四</asp:ListItem>
<asp:ListItem value="五" selected="False">五</asp:ListItem>
<asp:ListItem value="六" selected="False">六</asp:ListItem>
<asp:ListItem value="日" selected="False">日</asp:ListItem>
</asp:dropdownlist> </td>
<td vAlign="top"> 第
<asp:dropdownlist id="DropDownList3" runat="server">
<asp:ListItem value="1-2" selected="False">1-2</asp:ListItem>
<asp:ListItem value="3-4" selected="False">3-4</asp:ListItem>
<asp:ListItem value="5-6" selected="False">5-6</asp:ListItem>
<asp:ListItem value="7-8" selected="False">7-8</asp:ListItem>
<asp:ListItem value="9-12" selected="False">9-12</asp:ListItem>
</asp:dropdownlist> 节</td>
<td vAlign="top"> 座位数>=
<asp:textbox id="TextBox1" runat="server" Width="64px"></asp:textbox> </td>
<td vAlign="top">
<asp:button id="Button1" runat="server" Text="查询"></asp:button> </td>
</tr>
<tr>
<td vAlign="top" colSpan="5"><asp:datagrid id="DataGrid1" runat="server" enableviewstate="False" headerstyle-backcolor="#aaaadd"
font-size="8pt" font-name="verdana" cellspacing="0" cellpadding="3" showfooter="False" bordercolor="black" backcolor="#ccccff"
width="700" OnUpdateCommand="DataGrid_Update" OnCancelCommand="DataGrid_Cancel" OnEditCommand="DataGrid_Edit" AutoGenerateColumns="false"
DataKeyField="教室号">
<HeaderStyle BackColor="#AAAADD"></HeaderStyle>
<Columns>
<asp:EditCommandColumn ButtonType="LinkButton" UpdateText="更新" CancelText="取消" EditText="编辑" ItemStyle-Wrap="false"></asp:EditCommandColumn>
<asp:BoundColumn DataField="座位数" SortExpression="座位数" ReadOnly="True" HeaderText="座位数" ItemStyle-Wrap="false"></asp:BoundColumn>
<asp:BoundColumn DataField="教室号" SortExpression="教室号" ReadOnly="True" HeaderText="教室号" ItemStyle-Wrap="false"></asp:BoundColumn>
<asp:BoundColumn DataField="周次" SortExpression="周次" ReadOnly="True" HeaderText="周次" ItemStyle-Wrap="false"></asp:BoundColumn>
<asp:BoundColumn DataField="周几" SortExpression="周几" ReadOnly="True" HeaderText="周几" ItemStyle-Wrap="false"></asp:BoundColumn>
<asp:BoundColumn DataField="节次" SortExpression="节次" ReadOnly="True" HeaderText="节次" ItemStyle-Wrap="false"></asp:BoundColumn>
<asp:BoundColumn DataField="用途" SortExpression="用途" HeaderText="用途" ItemStyle-Wrap="false"></asp:BoundColumn>
</Columns>
</asp:datagrid></td>
</tr>
<tr>
<td colspan="5"><span id="Message" runat="server" /></td>
</tr>
</table>
</FONT></form>
</body>
</HTML>
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 WebApplication1
{
/// <summary>
/// WebForm1 的摘要说明。
/// </summary>
public class WebForm1 : System.Web.UI.Page
{
protected System.Web.UI.WebControls.DropDownList DropDownList1;
protected System.Web.UI.WebControls.DropDownList DropDownList2;
protected System.Web.UI.WebControls.DropDownList DropDownList3;
protected System.Web.UI.WebControls.Button Button1;
protected System.Web.UI.WebControls.TextBox TextBox1;
protected System.Web.UI.HtmlControls.HtmlGenericControl Message;
protected System.Web.UI.WebControls.DataGrid DataGrid1;
protected SqlConnection myConnection;
private void Page_Load(object sender, System.EventArgs e)
{
// 在此处放置用户代码以初始化页面
myConnection = new SqlConnection("Data Source = localhost;database = 教室安排;user id = sa; password = 123;");
if (IsPostBack)
BindGrid();
}
public void DataGrid_Edit(object sender, DataGridCommandEventArgs e)
{
DataGrid1.EditItemIndex = (int) e.Item.ItemIndex;
BindGrid();
}
public void DataGrid_Cancel(object sender, DataGridCommandEventArgs e)
{
DataGrid1.EditItemIndex = -1;
BindGrid();
}
public void DataGrid_Update(object sender, DataGridCommandEventArgs e)
{
String updateCmd = "UPDATE [classroom] SET 用途 = @用途 where 教室号 = @教室号"
+ " and 周次 = @周次 and 周几 = @周几 and 节次 = @节次";
SqlCommand myCommand = new SqlCommand(updateCmd, myConnection);
myCommand.Parameters.Add(new SqlParameter("@用途", SqlDbType.NVarChar, 100));
myCommand.Parameters.Add(new SqlParameter("@教室号", SqlDbType.Int, 4));
myCommand.Parameters.Add(new SqlParameter("@周次", SqlDbType.TinyInt, 1));
myCommand.Parameters.Add(new SqlParameter("@周几", SqlDbType.Char, 2));
myCommand.Parameters.Add(new SqlParameter("@节次", SqlDbType.VarChar, 4)); myCommand.Parameters["@教室号"].Value = DataGrid1.DataKeys[(int)e.Item.ItemIndex];
String [] cols = {"@教室号", "@周次", "@周几", "@节次", "@用途"}; int numCols = e.Item.Cells.Count;
for (int i=3; i<numCols; i++)
{
String colvalue = ((TextBox)e.Item.Cells[i].Controls[0]).Text;
myCommand.Parameters[cols[i-2]].Value = colvalue;
Message.InnerHtml = "<b>++gdfgdfgsdfgsdfgdf</b>" ;
Message.Style["color"] = "red";
} myCommand.Connection.Open();
Message.InnerHtml = "updateCmd"; try
{
myCommand.ExecuteNonQuery();
Message.InnerHtml = "<b>Record Updated</b><br>" + updateCmd;
DataGrid1.EditItemIndex = -1;
}
catch (SqlException exc)
{
if (exc.Number == 2627)
Message.InnerHtml = "ERROR: A record already exists with the same primary key";
else
Message.InnerHtml = "ERROR: Could not update record, please ensure the fields are correctly filled out";
Message.Style["color"] = "red";
} myCommand.Connection.Close(); BindGrid();
/*String updateCmd = "UPDATE 教室用途 SET 用途 = @用途 where 教室号 = @教室号 and 周次 = @周次 "
+ "and 周几 = @周几 and 节次 = @节次";
SqlCommand cmd = new SqlCommand(updateCmd, myConnection);
cmd.Parameters.Add(new SqlParameter("@用途", SqlDbType.NVarChar, 100));
cmd.Parameters.Add(new SqlParameter("@教室号", SqlDbType.NVarChar, 5));
cmd.Parameters.Add(new SqlParameter("@周次", SqlDbType.NVarChar, 2));
cmd.Parameters.Add(new SqlParameter("@周几", SqlDbType.NChar, 1));
cmd.Parameters.Add(new SqlParameter("@节次", SqlDbType.NVarChar, 4));
//cmd.Parameters["@教室号"].Value = DataGrid1.DataKeys[(int)e.Item.ItemIndex]; String [] cols = {"@用途","@教室号", "@周次", "@周几", "@节次"}; cmd.Parameters[cols[1]].Value = ((TextBox)e.Item.Cells[2].Controls[0]).ToString();
cmd.Parameters[cols[2]].Value = ((TextBox)e.Item.Cells[3].Controls[0]).ToString();
cmd.Parameters[cols[3]].Value = ((TextBox)e.Item.Cells[4].Controls[0]).ToString();
cmd.Parameters[cols[4]].Value = ((TextBox)e.Item.Cells[5].Controls[0]).ToString();
cmd.Parameters[cols[0]].Value = ((TextBox)e.Item.Cells[6].Controls[0]).ToString();
cmd.Connection.Open();
cmd.ExecuteNonQuery();
cmd.Connection.Close();
DataGrid1.EditItemIndex = -1;
BindGrid();*/
/*String classRoomId = ((TextBox)e.Item.Cells[2].Controls[0]).Text;
String weekNumber = ((TextBox)e.Item.Cells[3].Controls[0]).Text;
String weekDay = ((TextBox)e.Item.Cells[4].Controls[0]).Text;
String classTime = ((TextBox)e.Item.Cells[5].Controls[0]).Text;
String usage = ((TextBox)e.Item.Cells[6].Controls[0]).Text;
String updateCmd = "update 教室用途 set 用途 = '" + usage + "' where 教室号 = " + classRoomId +
" and 周次 = " + weekNumber + " and 周几 = '" +
weekDay + "' and 节次 = '" + classTime + "'"; Message.InnerHtml = "Update Command is " + updateCmd;
SqlConnection myConnection = new
SqlConnection("Data Source = localhost;database = 教室安排;user id = sa; password = 123;");
SqlCommand cmd = new SqlCommand(updateCmd, myConnection);
cmd.Connection.Open();
cmd.ExecuteNonQuery();
cmd.Connection.Close();
DataGrid1.EditItemIndex = -1;
BindGrid();*/
} #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 public void BindGrid()
{
string queryString = "select * from classroom where 用途 is null and 周次 = " + DropDownList1.SelectedItem.Text
+ "and 周几 = '" + DropDownList2.SelectedItem.Text
+ "' and 节次 = '" + DropDownList3.SelectedItem.Text + "'";
if (TextBox1.Text != "")
queryString = queryString + " and 座位数 > = " + TextBox1.Text;
SqlDataAdapter da = new SqlDataAdapter(queryString, myConnection);
DataSet ds = new DataSet();
da.Fill(ds, "classroom");
DataGrid1.DataSource = ds.Tables["classroom"].DefaultView;
DataGrid1.DataBind();
}
private void Button1_Click(object sender, System.EventArgs e)
{
BindGrid();
}
}
}
把错误信息贴出来~~
55 17306 6 二 9-12 选修课
55 17306 7 二 9-12 选修课
一般upate 和delete 会影响多行,除非你愿意这样。可以增加一列值自动增长的列。这样虽然浪费空间,但是至少不会出错了