Code
using System;
using System.Collections;
using System.Configuration;
using System.Data;
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.Data.SqlClient;
public partial class 批量插入数据 : System.Web.UI.Page
{
public static string sqlcon = "server=(local);user id=sa;password=;database=OfficeAutoDB";
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
int i = 0;
if (List1.Items.Count > 0)
{
while (i < List1.Items.Count)
{
if (FileName.Text.Trim() == List1.Items[i].Text.Trim().ToString())
{
Response.Write("<script language=javascript>alert('该文件名字已存在!');</script>");
break;
}
else
{
i++;
}
}
}
else
List1.Items.Add(new ListItem(FileName.Text));
if (i == List1.Items.Count)
{
List1.Items.Add(new ListItem(FileName.Text));
}
}
protected void Button2_Click(object sender, EventArgs e)
{
//创建保存Sql语句的ArrayList对象
ArrayList list1 = new ArrayList();
for (int i = 0; i < List1.Items.Count; i++)
{
string sql = "insert into Files(FileName) values('" + List1.Items[i].Text + "')";
list1.Add(sql);
}
insertMutilData(list1);
}
protected void Button3_Click(object sender, EventArgs e)
{
ArrayList valuelist = new ArrayList();
for (int i = 0; i < List1.Items.Count; i++)
{
//逐个处理插入的数据值,并添加到集合中
valuelist.Add(List1.Items[i].Text.ToString());
}
InsertMutilDataByDS(valuelist);
}
private void insertMutilData(ArrayList sqllist)
{
SqlConnection cn = new SqlConnection(sqlcon);
SqlCommand cmd = new SqlCommand(" ",cn);
cn.Open();
SqlTransaction myTran = cn.BeginTransaction();
try
{
//设置插入对象的数据库事物
cmd.Transaction = myTran;
for (int i = 0; i < sqllist.Count; i++)
{
cmd.CommandText = sqllist[i].ToString();
cmd.ExecuteNonQuery();
}
myTran.Commit();//提交数据库事物
}
catch
{
myTran.Rollback();
}
finally
{
cn.Close();
}
}
private void InsertMutilDataByDS(ArrayList valuelist)
{
DataTable table = new DataTable();
//获取该表的列的集合
table .Columns.Add(new DataColumn("FileName",typeof(string )));//新建一个指定列名和数据类型
for (int i = 0; i < List1.Items.Count;i++ )
{
DataRow row = table.NewRow();//创建一个与该表具有相同架构的新行
row["FileName"]=valuelist[i].ToString();
table.Rows.Add(row);//把添加的行添加到表中
}
string sql = "insert into Files(FileName) values(@FileName)";
SqlConnection cn = new SqlConnection(sqlcon);
cn.Open();
SqlCommand cmd = new SqlCommand(sql,cn );
SqlParameter paramFileName = new SqlParameter("@FileName", SqlDbType.VarChar, 50);
cmd.Parameters.Add(paramFileName);
cmd.Parameters["@FileName"].SourceColumn=table .Columns["FileName"].ColumnName;
SqlDataAdapter adapter = new SqlDataAdapter();
adapter.Update(table);//执行批量更新
cn.Close();
}
protected void Button4_Click(object sender, EventArgs e)
{
List1.Items.Remove(List1 .SelectedItem);
}
}
using System;
using System.Collections;
using System.Configuration;
using System.Data;
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.Data.SqlClient;
public partial class 批量插入数据 : System.Web.UI.Page
{
public static string sqlcon = "server=(local);user id=sa;password=;database=OfficeAutoDB";
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
int i = 0;
if (List1.Items.Count > 0)
{
while (i < List1.Items.Count)
{
if (FileName.Text.Trim() == List1.Items[i].Text.Trim().ToString())
{
Response.Write("<script language=javascript>alert('该文件名字已存在!');</script>");
break;
}
else
{
i++;
}
}
}
else
List1.Items.Add(new ListItem(FileName.Text));
if (i == List1.Items.Count)
{
List1.Items.Add(new ListItem(FileName.Text));
}
}
protected void Button2_Click(object sender, EventArgs e)
{
//创建保存Sql语句的ArrayList对象
ArrayList list1 = new ArrayList();
for (int i = 0; i < List1.Items.Count; i++)
{
string sql = "insert into Files(FileName) values('" + List1.Items[i].Text + "')";
list1.Add(sql);
}
insertMutilData(list1);
}
protected void Button3_Click(object sender, EventArgs e)
{
ArrayList valuelist = new ArrayList();
for (int i = 0; i < List1.Items.Count; i++)
{
//逐个处理插入的数据值,并添加到集合中
valuelist.Add(List1.Items[i].Text.ToString());
}
InsertMutilDataByDS(valuelist);
}
private void insertMutilData(ArrayList sqllist)
{
SqlConnection cn = new SqlConnection(sqlcon);
SqlCommand cmd = new SqlCommand(" ",cn);
cn.Open();
SqlTransaction myTran = cn.BeginTransaction();
try
{
//设置插入对象的数据库事物
cmd.Transaction = myTran;
for (int i = 0; i < sqllist.Count; i++)
{
cmd.CommandText = sqllist[i].ToString();
cmd.ExecuteNonQuery();
}
myTran.Commit();//提交数据库事物
}
catch
{
myTran.Rollback();
}
finally
{
cn.Close();
}
}
private void InsertMutilDataByDS(ArrayList valuelist)
{
DataTable table = new DataTable();
//获取该表的列的集合
table .Columns.Add(new DataColumn("FileName",typeof(string )));//新建一个指定列名和数据类型
for (int i = 0; i < List1.Items.Count;i++ )
{
DataRow row = table.NewRow();//创建一个与该表具有相同架构的新行
row["FileName"]=valuelist[i].ToString();
table.Rows.Add(row);//把添加的行添加到表中
}
string sql = "insert into Files(FileName) values(@FileName)";
SqlConnection cn = new SqlConnection(sqlcon);
cn.Open();
SqlCommand cmd = new SqlCommand(sql,cn );
SqlParameter paramFileName = new SqlParameter("@FileName", SqlDbType.VarChar, 50);
cmd.Parameters.Add(paramFileName);
cmd.Parameters["@FileName"].SourceColumn=table .Columns["FileName"].ColumnName;
SqlDataAdapter adapter = new SqlDataAdapter();
adapter.Update(table);//执行批量更新
cn.Close();
}
protected void Button4_Click(object sender, EventArgs e)
{
List1.Items.Remove(List1 .SelectedItem);
}
}