ASP.NET的DataGrid批量添加数据
在以前的一个学校项目中,学生管理系统中应用了 ASP.NET的DataGrid批量添加数据,应用DataGrid的循环添加数据
for (i = 0; i < (int)this.MyDataGrid.Items.Count; i++)
{
//处理
}
截图
浏览数据
添加数据
数据库表数据
学生表
stu_Information
[id] [nvarchar]
[name] [nvarchar]
[sex] [nvarchar]
[birth] [nvarchar]
[age] [nvarchar]
[department] [nvarchar]
[class] [nvarchar]
[post] [nvarchar]
[Political] [nvarchar]
[address] [nvarchar]
[email] [nvarchar]
[phone] [nvarchar]
[Telephone1] [nvarchar]
[Telephone2] [nvarchar]
[Information] [nvarchar]
考勤表
kaoqin
[kaoqinID] [int] IDENTITY (1, 1) NOT NULL ,
[datetime] [datetime] NULL ,
[id] [char]
[title] [char]
[week] [char]
[dayno] [char]
[type] [char]
[times] [int] NULL ,
[information] [char]
页面aspx文件
<asp:DataGrid ID="MyDataGrid" AutoGenerateColumns="False" Width="100%" HeaderStyle-HorizontalAlign="center"
ItemStyle-HorizontalAlign="center" runat="server">
<Columns>
<asp:BoundColumn DataField="id" SortExpression="id" HeaderStyle-Width="10%" HeaderStyle-CssClass="topbar"
HeaderStyle-ForeColor="#ffffff" HeaderText="学号" />
<asp:BoundColumn DataField="name" SortExpression="name" HeaderStyle-Width="8%" HeaderStyle-CssClass="topbar"
HeaderStyle-ForeColor="#ffffff" HeaderText="姓名" />
<asp:BoundColumn DataField="sex" SortExpression="sex" HeaderStyle-Width="5%" HeaderStyle-CssClass="topbar"
HeaderStyle-ForeColor="#ffffff" HeaderText="性别" />
<asp:TemplateColumn HeaderText="迟到(次数)" HeaderStyle-Width="15%" HeaderStyle-CssClass="topbar"
HeaderStyle-ForeColor="#ffffff">
<ItemTemplate>
<asp:TextBox ID="Late" Width="30px" runat="server" />
</ItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn HeaderText="早退(次数)" HeaderStyle-Width="15%" HeaderStyle-CssClass="topbar"
HeaderStyle-ForeColor="#ffffff">
<ItemTemplate>
<asp:TextBox ID="LeaveEarly" Width="30px" runat="server" />
</ItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn HeaderText="旷课(次数)" HeaderStyle-Width="15%" HeaderStyle-CssClass="topbar"
HeaderStyle-ForeColor="#ffffff">
<ItemTemplate>
<asp:TextBox ID="Absenteeism" Width="30px" runat="server" />
</ItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn HeaderText="请假(次数)" HeaderStyle-Width="15%" HeaderStyle-CssClass="topbar"
HeaderStyle-ForeColor="#ffffff">
<ItemTemplate>
<asp:TextBox ID="Leave" Width="30px" runat="server" />
</ItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn HeaderText="备注" HeaderStyle-Width="40%" HeaderStyle-CssClass="topbar"
HeaderStyle-ForeColor="#ffffff">
<ItemTemplate>
<asp:TextBox ID="Information" Width="200px" runat="server" />
</ItemTemplate>
</asp:TemplateColumn>
</Columns>
</asp:DataGrid>
后台aspx.cs
protected void btn_Add(object sender, ImageClickEventArgs e)
{
string link1 = ConfigurationSettings.AppSettings["link_local"].ToString();
SqlConnection conn1 = new SqlConnection(link1);
string sqlcmd = "";
int i;
bool flag = false;
conn1.Open();
for (i = 0; i < (int)this.MyDataGrid.Items.Count; i++)
{
string typename = "";
if (((TextBox)MyDataGrid.Items[i].Cells[3].Controls[1]).Text.Trim() != "")
{
typename = "迟到";
flag = true;
sqlcmd = "insert into kaoqin(id,week,datetime,type,times,information) values(@id,@week,@datetime,@type,@times,@information)";
SqlCommand comm = new SqlCommand(sqlcmd, conn1);
comm.Parameters.Add("@id", SqlDbType.Char, 20);
comm.Parameters.Add("@week", SqlDbType.Char, 10);
comm.Parameters.Add("@datetime", SqlDbType.DateTime, 8);
comm.Parameters.Add("@type", SqlDbType.Char, 10);
comm.Parameters.Add("@times", SqlDbType.Char, 10);
comm.Parameters.Add("@information", SqlDbType.Char, 100);
comm.Parameters["@id"].Value = this.MyDataGrid.Items[i].Cells[0].Text.Trim();
comm.Parameters["@week"].Value = Week.SelectedItem.Value;
comm.Parameters["@datetime"].Value = DateTime.Now.ToString("yyyy年MM月dd日");
comm.Parameters["@type"].Value = typename.Trim();
comm.Parameters["@times"].Value = ((TextBox)MyDataGrid.Items[i].Cells[3].Controls[1]).Text.Trim();
comm.Parameters["@information"].Value = ((TextBox)this.MyDataGrid.Items[i].Cells[7].Controls[1]).Text.Trim();
comm.ExecuteNonQuery();
}
if (((TextBox)MyDataGrid.Items[i].Cells[4].Controls[1]).Text.Trim() != "")
{
typename = "早退";
flag = true;
sqlcmd = "insert into kaoqin(id,week,datetime,type,times,information) values(@id,@week,@datetime,@type,@times,@information)";
SqlCommand comm = new SqlCommand(sqlcmd, conn1);
comm.Parameters.Add("@id", SqlDbType.Char, 20);
comm.Parameters.Add("@week", SqlDbType.Char, 10);
comm.Parameters.Add("@datetime", SqlDbType.DateTime, 8);
comm.Parameters.Add("@type", SqlDbType.Char, 10);
comm.Parameters.Add("@times", SqlDbType.Char, 10);
comm.Parameters.Add("@information", SqlDbType.Char, 100);
comm.Parameters["@id"].Value = this.MyDataGrid.Items[i].Cells[0].Text.Trim();
comm.Parameters["@week"].Value = Week.SelectedItem.Value;
comm.Parameters["@datetime"].Value = DateTime.Now.ToString("yyyy年MM月dd日");
comm.Parameters["@type"].Value = typename.Trim();
comm.Parameters["@times"].Value = ((TextBox)MyDataGrid.Items[i].Cells[4].Controls[1]).Text.Trim();
comm.Parameters["@information"].Value = ((TextBox)this.MyDataGrid.Items[i].Cells[7].Controls[1]).Text.Trim();
comm.ExecuteNonQuery();
}
if (((TextBox)MyDataGrid.Items[i].Cells[5].Controls[1]).Text.Trim() != "")
{
typename = "旷课";
flag = true;
sqlcmd = "insert into kaoqin(id,week,datetime,type,times,information) values(@id,@week,@datetime,@type,@times,@information)";
SqlCommand comm = new SqlCommand(sqlcmd, conn1);
comm.Parameters.Add("@id", SqlDbType.Char, 20);
comm.Parameters.Add("@week", SqlDbType.Char, 10);
comm.Parameters.Add("@datetime", SqlDbType.DateTime, 8);
comm.Parameters.Add("@type", SqlDbType.Char, 10);
comm.Parameters.Add("@times", SqlDbType.Char, 10);
comm.Parameters.Add("@information", SqlDbType.Char, 100);
comm.Parameters["@id"].Value = this.MyDataGrid.Items[i].Cells[0].Text.Trim();
comm.Parameters["@week"].Value = Week.SelectedItem.Value;
comm.Parameters["@datetime"].Value = DateTime.Now.ToString("yyyy年MM月dd日");
comm.Parameters["@type"].Value = typename.Trim();
comm.Parameters["@times"].Value = ((TextBox)MyDataGrid.Items[i].Cells[5].Controls[1]).Text.Trim();
comm.Parameters["@information"].Value = ((TextBox)this.MyDataGrid.Items[i].Cells[7].Controls[1]).Text.Trim();
comm.ExecuteNonQuery();
}
if (((TextBox)MyDataGrid.Items[i].Cells[6].Controls[1]).Text.Trim() != "")
{
typename = "请假";
flag = true;
sqlcmd = "insert into kaoqin(id,week,datetime,type,times,information) values(@id,@week,@datetime,@type,@times,@information)";
SqlCommand comm = new SqlCommand(sqlcmd, conn1);
comm.Parameters.Add("@id", SqlDbType.Char, 20);
comm.Parameters.Add("@week", SqlDbType.Char, 10);
comm.Parameters.Add("@datetime", SqlDbType.DateTime, 8);
comm.Parameters.Add("@type", SqlDbType.Char, 10);
comm.Parameters.Add("@times", SqlDbType.Char, 10);
comm.Parameters.Add("@information", SqlDbType.Char, 100);
comm.Parameters["@id"].Value = this.MyDataGrid.Items[i].Cells[0].Text.Trim();
comm.Parameters["@week"].Value = Week.SelectedItem.Value;
comm.Parameters["@datetime"].Value = DateTime.Now.ToString("yyyy年MM月dd日");
comm.Parameters["@type"].Value = typename.Trim();
comm.Parameters["@times"].Value = ((TextBox)MyDataGrid.Items[i].Cells[6].Controls[1]).Text.Trim();
comm.Parameters["@information"].Value = ((TextBox)this.MyDataGrid.Items[i].Cells[7].Controls[1]).Text.Trim();
comm.ExecuteNonQuery();
}
}
conn1.Close();
if (flag == true)
{
lblMessage.Text = "添加成功!";
}
else
lblMessage.Text = "未添加信息";
}
源代码下载