📄 mymutliinsertdata.aspx.cs
字号:
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.Configuration;
using System.Data.SqlClient;
namespace Example_11_11
{
/// <summary>
/// Summary description for MyMutliInsertData.
/// </summary>
public class MyMutliInsertData : System.Web.UI.Page
{
protected System.Web.UI.WebControls.Button SureInsertBtn;
protected System.Web.UI.WebControls.Label SucessMessage;
protected System.Web.UI.WebControls.TextBox StaffName;
protected System.Web.UI.WebControls.Button AddStaffBtn;
protected System.Web.UI.WebControls.ListBox StaffList;
public static string SQLCONNECTIONSTRING = ConfigurationSettings.AppSettings["SQLCONNECTIONSTRING"].ToString();
private void Page_Load(object sender, System.EventArgs e)
{
//
}
private void InsertMutlilData(ArrayList sqlList)
{
///向数据库中插入数据
SqlConnection myConnection = new SqlConnection(SQLCONNECTIONSTRING);
SqlCommand myCommand = new SqlCommand("",myConnection);
///执行数据库查询
myConnection.Open();
SqlTransaction myTran = myConnection.BeginTransaction();
try
{
myCommand.Transaction = myTran;
for(int i = 0; i < sqlList.Count; i++)
{
myCommand.CommandText = sqlList[i].ToString();
myCommand.ExecuteNonQuery();
}
myTran.Commit();
}
catch
{
myTran.Rollback();
}
finally
{
myConnection.Close();
}
}
private void InsertMutliDataByDS(ArrayList valueList)
{
DataTable dataTable = new DataTable();
dataTable.Columns.Add(new DataColumn("Name",typeof(string)));
///定义一个新数据行
// DataRow dataRow = new DataRow();
// dataRow["Name"] = "Name";
// dataTable.Rows.Add(dataRow);
for(int i = 0; i < valueList.Count; i++)
{
DataRow row = dataTable.NewRow();
row["Name"] = valueList[i].ToString();
dataTable.Rows.Add(row);
}
///向数据库中插入数据
String cmdText = "INSERT INTO MyTable(Name)VALUES(@Name)";
SqlConnection myConnection = new SqlConnection(SQLCONNECTIONSTRING);
SqlCommand myCommand = new SqlCommand(cmdText,myConnection);
SqlDataAdapter da = new SqlDataAdapter();
SqlParameter paramName = new SqlParameter("@Name",SqlDbType.VarChar);
myCommand.Parameters.Add(paramName);
///执行数据库查询
myConnection.Open();
myCommand.Parameters["@Name"].SourceColumn = dataTable.Columns["Name"].ColumnName;
da.Update(dataTable);
myConnection.Close();
}
#region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: This call is required by the ASP.NET Web Form Designer.
//
InitializeComponent();
base.OnInit(e);
}
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.AddStaffBtn.Click += new System.EventHandler(this.AddStaffBtn_Click);
this.SureInsertBtn.Click += new System.EventHandler(this.SureInsertBtn_Click);
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion
private void SureInsertBtn_Click(object sender, System.EventArgs e)
{
ArrayList sqlList = new ArrayList();
for(int i = 0; i < StaffList.Items.Count; i++)
{
String subSQL = "INSERT INTO MyTable(Name)VALUES('" + StaffList.Items[i].Text + "')";
sqlList.Add(subSQL);
}
///使用事务进行批量插入数据
InsertMutlilData(sqlList);
ArrayList valueList = new ArrayList();
for(int i = 0; i < StaffList.Items.Count; i++)
{
sqlList.Add(StaffList.Items[i].Text);
}
///使用DataSet进行批量插入数据
InsertMutliDataByDS(valueList);
}
private void AddStaffBtn_Click(object sender, System.EventArgs e)
{
if(StaffName.Text.Trim().Length > 0)
{
StaffList.Items.Add(new ListItem(StaffName.Text));
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -