동적 조합 SQL 문장 방식 으로 대량 업 데 이 트 를 실현 하 는 실례
Default.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Index.aspx.cs" Inherits="Index" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title> </title>
</head>
<body class="Font">
<form id="form1" runat="server">
<div style="text-align: left" align="left"><asp:Panel ID="Panel2" runat="server">
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
OnRowDataBound="GridView1_RowDataBound"
OnSelectedIndexChanging="GridView1_SelectedIndexChanging" Font-Size="9pt"
AllowPaging="True" EmptyDataText=" !"
OnPageIndexChanging="GridView1_PageIndexChanging" CellPadding="4"
ForeColor="#333333" GridLines="None" DataKeyNames="id">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:CheckBox ID="cbSingleOrMore" runat="server" />
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="id" HeaderText=" ID" />
<asp:BoundField DataField="name" HeaderText=" " />
<asp:BoundField DataField="type" HeaderText=" " />
<asp:BoundField DataField="content" HeaderText=" " />
<asp:BoundField DataField="userName" HeaderText=" " />
<asp:BoundField DataField="lineMan" HeaderText=" " />
<asp:BoundField DataField="issueDate" HeaderText=" "
DataFormatString="{0:d}" />
</Columns>
<FooterStyle BackColor="#990000" Font-Bold="True" ForeColor="White" />
<RowStyle BackColor="#FFFBD6" ForeColor="#333333" />
<SelectedRowStyle BackColor="#FFCC66" Font-Bold="True" ForeColor="Navy" />
<PagerStyle BackColor="#FFCC66" ForeColor="#333333" HorizontalAlign="Right" />
<HeaderStyle BackColor="#990000" Font-Bold="True" ForeColor="White" />
<AlternatingRowStyle BackColor="White" />
</asp:GridView>
</asp:Panel>
<asp:CheckBox ID="cbAll" runat="server" AutoPostBack="True"
Font-Size="9pt" OnCheckedChanged="cbAll_CheckedChanged"
Text=" / " />
<asp:Button ID="btnUpdateTime" runat="server" onclick="btnUpdateTime_Click"
Text=" " />
</div>
</form>
</body>
</html>
Default.aspx.cs
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Text;
using System.Data.SqlClient;
public partial class Index : System.Web.UI.Page
{
SqlConnection sqlcon;
string strCon = ConfigurationManager.AppSettings["conStr"];
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
this.GV_DataBind();
}
}
public void GV_DataBind()
{
string sqlstr = "select * from tb_inf";
sqlcon = new SqlConnection(strCon);
SqlDataAdapter da = new SqlDataAdapter(sqlstr, sqlcon);
DataSet ds = new DataSet();
sqlcon.Open();
da.Fill(ds, "tb_inf");
sqlcon.Close();
this.GridView1.DataSource = ds;
this.GridView1.DataKeyNames = new string[] { "id" };
this.GridView1.DataBind();
if (GridView1.Rows.Count > 0)
{
return;// ,
}
else//
{
StrHelper.GridViewHeader(GridView1);
}
}
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
string gIntro = e.Row.Cells[4].Text;
e.Row.Cells[4].Text = StrHelper.GetFirstString(gIntro, 12);
}
}
protected void GridView1_SelectedIndexChanging(object sender, GridViewSelectEventArgs e)
{
string id = this.GridView1.DataKeys[e.NewSelectedIndex].Value.ToString();
sqlcon = new SqlConnection(strCon);
SqlCommand com = new SqlCommand("select [check] from tb_inf where id='" + id + "'", sqlcon);
sqlcon.Open();
string count = Convert.ToString(com.ExecuteScalar());
if (count == "False")
{
count = "1";
}
else
{
count = "0";
}
com.CommandText = "update tb_inf set [check]=" + count + " where id=" + id;
com.ExecuteNonQuery();
sqlcon.Close();
this.GV_DataBind();
}
protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
this.GridView1.PageIndex = e.NewPageIndex;
this.GV_DataBind();
}
protected void cbAll_CheckedChanged(object sender, EventArgs e)
{
for (int i = 0; i <= GridView1.Rows.Count - 1; i++)//
{
CheckBox cbox = (CheckBox)GridView1.Rows[i].FindControl("cbSingleOrMore");
if (cbAll.Checked == true)
{
cbox.Checked = true;
}
else
{
cbox.Checked = false;
}
}
}
protected void btnUpdateTime_Click(object sender, EventArgs e)
{
StringBuilder builder = new StringBuilder();
int i = 0;
foreach (GridViewRow row in this.GridView1.Rows)// GridView , IN
{
CheckBox cbox = row.FindControl("cbSingleOrMore") as CheckBox;
if (cbox.Checked)//
{
// , IN
builder.AppendFormat("'{0}',", this.GridView1.DataKeys[row.RowIndex].Value.ToString());
i++;
continue;
}
continue;
}
if (builder.ToString().Length == 0)// IN ,
{
StrHelper.Alert(" , !");
return;
}
// StringBuilder “,”
builder.Remove(builder.ToString().LastIndexOf(","), 1);
// SQL
string SqlBuilderCopy = string.Format("Update tb_inf set issueDate='{0}' WHERE id IN ({1})", DateTime.Now.ToString(), builder.ToString());
sqlcon = new SqlConnection(strCon);//
SqlCommand sqlcom;//
int result = 0;
if (sqlcon.State.Equals(ConnectionState.Closed))
sqlcon.Open();//
sqlcom = new SqlCommand(SqlBuilderCopy, sqlcon);
SqlTransaction tran = sqlcon.BeginTransaction();// ,
sqlcom.Transaction = tran;//
try
{
result = sqlcom.ExecuteNonQuery();//
tran.Commit();//
}
catch (SqlException ex)
{
StrHelper.Alert(string.Format("SQL , :
{0}", ex.Message));
tran.Rollback();// , ,
return;
}
finally
{
sqlcon.Close();
}
if (result == i)//
{
StrHelper.Alert(" !");
}
else
{
StrHelper.Alert(" , !");
}
GV_DataBind();//
return;
}
}
StrHelper.cs
using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
//
using System.Text.RegularExpressions;
using System.Text;
/// <summary>
///StrHelper
/// </summary>
public class StrHelper
{
public StrHelper(){}
/// <summary>
///
/// </summary>
/// <param name="str"> </param>
/// <param name="num"> </param>
/// <returns></returns>
static public string GetSubString(string str, int num)
{
#region
return (str.Length > num) ? str.Substring(0, num) + "..." : str;
#endregion
}
/// <summary>
///
/// </summary>
/// <param name="stringToSub"> </param>
/// <param name="length"> </param>
/// <returns></returns>
public static string GetFirstString(string stringToSub, int length)
{
#region
Regex regex = new Regex("[\u4e00-\u9fa5]+", RegexOptions.Compiled);
char[] stringChar = stringToSub.ToCharArray();
StringBuilder sb = new StringBuilder();
int nLength = 0;
bool isCut = false;
for (int i = 0; i < stringChar.Length; i++)
{
if (regex.IsMatch((stringChar[i]).ToString()))//regex.IsMatch
{
sb.Append(stringChar[i]);// StringBuilder
nLength += 2;
}
else
{
sb.Append(stringChar[i]);
nLength = nLength + 1;
}
if (nLength > length)//
{
isCut = true;
break;
}
}
if (isCut)
return sb.ToString() + "...";
else
return sb.ToString();
#endregion
}
/// JavaScript
/// </summary>
/// <param name="js"> </param>
public static void Alert(string message)
{
#region
string js = @"<Script language='JavaScript'>
alert('" + message + "');</Script>";
HttpContext.Current.Response.Write(js);
#endregion
}
public static void GridViewHeader(GridView gdv)//
{
//
GridViewRow row = new GridViewRow(-1, -1, DataControlRowType.EmptyDataRow, DataControlRowState.Normal);
foreach (DataControlField field in gdv.Columns)
{
TableCell cell = new TableCell();
cell.Text = field.HeaderText;
cell.Width = field.HeaderStyle.Width;
cell.Height = field.HeaderStyle.Height;
cell.ForeColor = field.HeaderStyle.ForeColor;
cell.Font.Size = field.HeaderStyle.Font.Size;
cell.Font.Bold = field.HeaderStyle.Font.Bold;
cell.Font.Name = field.HeaderStyle.Font.Name;
cell.Font.Strikeout = field.HeaderStyle.Font.Strikeout;
cell.Font.Underline = field.HeaderStyle.Font.Underline;
cell.BackColor = field.HeaderStyle.BackColor;
cell.VerticalAlign = field.HeaderStyle.VerticalAlign;
cell.HorizontalAlign = field.HeaderStyle.HorizontalAlign;
cell.CssClass = field.HeaderStyle.CssClass;
cell.BorderColor = field.HeaderStyle.BorderColor;
cell.BorderStyle = field.HeaderStyle.BorderStyle;
cell.BorderWidth = field.HeaderStyle.BorderWidth;
row.Cells.Add(cell);
}
TableItemStyle headStyle = gdv.HeaderStyle;
TableItemStyle emptyStyle = gdv.EmptyDataRowStyle;
emptyStyle.Width = headStyle.Width;
emptyStyle.Height = headStyle.Height;
emptyStyle.ForeColor = headStyle.ForeColor;
emptyStyle.Font.Size = headStyle.Font.Size;
emptyStyle.Font.Bold = headStyle.Font.Bold;
emptyStyle.Font.Name = headStyle.Font.Name;
emptyStyle.Font.Strikeout = headStyle.Font.Strikeout;
emptyStyle.Font.Underline = headStyle.Font.Underline;
emptyStyle.BackColor = headStyle.BackColor;
emptyStyle.VerticalAlign = headStyle.VerticalAlign;
emptyStyle.HorizontalAlign = headStyle.HorizontalAlign;
emptyStyle.CssClass = headStyle.CssClass;
emptyStyle.BorderColor = headStyle.BorderColor;
emptyStyle.BorderStyle = headStyle.BorderStyle;
emptyStyle.BorderWidth = headStyle.BorderWidth;
//
GridViewRow row1 = new GridViewRow(0, -1, DataControlRowType.EmptyDataRow, DataControlRowState.Normal);
TableCell cell1 = new TableCell();
cell1.Text = " !";
cell1.BackColor = System.Drawing.Color.White;
row1.Cells.Add(cell1);
cell1.ColumnSpan = 6;//
if (gdv.Controls.Count == 0)
{
gdv.Page.Response.Write("<script language='javascript'>alert(' DataBind EmptyDataText !');</script>");
}
else
{
gdv.Controls[0].Controls.Clear();
gdv.Controls[0].Controls.AddAt(0, row);
gdv.Controls[0].Controls.AddAt(1, row1);
}
}
}
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
동적 조합 SQL 문장 방식 으로 대량 업 데 이 트 를 실현 하 는 실례텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.