您可以从 Datatable 在数据库表中插入批量行。
使用我多次使用的以下内容:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Microsoft.ApplicationBlocks.Data;
using System.Data.SqlClient;
using System.Data;
using AssamExitPollReport;
using System.IO;
using System.Drawing;
using Spire.Xls;
using System.Data.Sql;
public partial class Default : System.Web.UI.Page
{
DataSet ds = new DataSet();
clsdata obj = new clsdata();
protected void Page_Load(object sender, EventArgs e)
{
string tablename = Request.QueryString["tablename"];
// Response.ContentType = "text/xml"; //Set Content MIME Type.
Response.Write("<h3>Execution Started on " + DateTime.Now.ToLocalTime()+"</h3>");
bool flg=false;
if (tablename != null)
{
flg = BulkInsertDataTable(tablename);
if (flg)
Response.Write("<br><h3>Successfully executed on " + DateTime.Now.ToLocalTime() + "</h3>");
else
Response.Write("<br><h3>Oops! Something is wrong.</h3>");
}
else
Response.Write("<br><h3>Oops! @parameter \"tablename\" is missing.</h3>");
}
//public bool BulkInsertDataTable(string tableName, DataTable dataTable)
public bool BulkInsertDataTable(string tablename)
{
bool isSuccuss=true;
try
{
string client = "Server=databasehost\\SQLEXPRESS;Database=dbname;Uid=username;Pwd=yourpassword;Trusted_Connection=no";
ds = obj.Executedatasetcount("select_tablename");
obj.ExecuteDataset("delete_temp", new object[] { tablename });
using (SqlConnection destinationConnection = new SqlConnection(client))
using (SqlBulkCopy bulkCopy = new SqlBulkCopy(destinationConnection))
{
destinationConnection.Open();
bulkCopy.DestinationTableName = tablename;
bulkCopy.WriteToServer(ds.Tables[0]);
destinationConnection.Close();
}
}
catch (Exception ex)
{
isSuccuss = false;
}
return isSuccuss;
}
private void elseif(bool p)
{
throw new NotImplementedException();
}
}
clsdata 类用于执行过程:
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 Microsoft.ApplicationBlocks.Data;
namespace AssamExitPollReport
{
public class clsdata
{
// string conn = "Data Source=ADMIN-B19C3BADF;Initial Catalog=bazarkhodro;Integrated Security=True";
public DataSet Executedataset(string spName)
{
return SqlHelper.ExecuteDataset(System.Configuration.ConfigurationManager.ConnectionStrings["conn1"].ConnectionString, CommandType.StoredProcedure, spName);
}
public DataSet ExecuteDataset(string spName, object[] values)
{
return SqlHelper.ExecuteDataset(System.Configuration.ConfigurationManager.ConnectionStrings["conn1"].ConnectionString, spName, values);
}
public DataSet ExecuteDataset(string spName, object[] values, string conn)
{
return SqlHelper.ExecuteDataset(conn, spName, values);
}
public int ExecuteNonQuery(string spName, object[] values, string conn)
{
int i = 0;
try
{
i = SqlHelper.ExecuteNonQuery(conn, spName, values);
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
return i;
}
public int ExecuteScaler(string spName, object[] values)
{
int i = 0;
try
{
i = Convert.ToInt32(SqlHelper.ExecuteScalar(System.Configuration.ConfigurationManager.ConnectionStrings["conn"].ConnectionString, spName, values));
}
catch (Exception)
{
throw;
}
return i;
}
public int ExecuteScaler(string spName, object[] values, string conn)
{
int i = 0;
try
{
i = Convert.ToInt32(SqlHelper.ExecuteScalar(conn, spName, values));
}
catch (Exception)
{
throw;
}
return i;
}
}
}
其中 conn(connection string) 在 web.config 中初始化