C#.NET中若何批量拔出年夜量數據到數據庫中。本站提示廣大學習愛好者:(C#.NET中若何批量拔出年夜量數據到數據庫中)文章只能為提供參考,不一定能成為您想要的結果。以下是C#.NET中若何批量拔出年夜量數據到數據庫中正文
在WEB項目開辟進程中有時會碰著批量拔出數據到數或許是將EXCEL文件據入到數據庫中.為了便利完成可以先將EXCEL導入到GRIDVIEW中然後一次批量拔出.完成代碼以下:
前台代碼
<asp:GridView ID="dgBom" runat="server" AutoGenerateColumns="false" CellPadding="1" CellSpacing="2">
<HeaderStyle BackColor="#ededed" />
<Columns>
<asp:TemplateField HeaderText="學號">
<ItemTemplate>
<asp:TextBox ID="studentnumber" runat="server" Text='<%#Eval("studentnumber") %>' ></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="先生姓名">
<ItemTemplate>
<asp:TextBox ID="studentname" runat="server" Text='<%#Eval("studentname") %>'></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
<asp:FileUpload ID="FileUpload1" runat="server" Font-Italic="False" />
<asp:Button ID="btn2" runat="server" OnClick="btn2_Click" Text="導入數據" />
<asp:Button ID="btninsert" runat="server" OnClick="btninsert_Click" Text="拔出到數據庫中"/>
後台代碼:
//起首在定名空間中參加以下兩行
using System.Data.SqlClient;
using System.Data.OleDb;
protected void btn2_Click(object sender, EventArgs e)
{
string filepath = FileUpload1.PostedFile.FileName;
ReadExcel(filepath, dgBom);
}
public void ReadExcel(string sExcelFile, GridView dgBom)
{
DataTable ExcelTable;
DataSet ds = new DataSet();
//Excel的銜接
OleDbConnection objConn = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + sExcelFile + ";" + "Extended Properties=Excel 8.0;");
objConn.Open();
DataTable schemaTable = objConn.GetOleDbSchemaTable(System.Data.OleDb.OleDbSchemaGuid.Tables, null);
string tableName = schemaTable.Rows[0][2].ToString().Trim();//獲得 Excel 的表名,默許值是sheet1
string strSql = "select * from [" + tableName + "]";
OleDbCommand objCmd = new OleDbCommand(strSql, objConn);
OleDbDataAdapter myData = new OleDbDataAdapter(strSql, objConn);
myData.Fill(ds, tableName);//填湊數據
dgBom.DataSource =ds;
dgBom.DataBind();
objConn.Close();
ExcelTable = ds.Tables[tableName];
int iColums = ExcelTable.Columns.Count;//列數
int iRows = ExcelTable.Rows.Count;//行數
//界說二維數組存儲 Excel 表中讀取的數據
string[,] storedata = new string[iRows, iColums];
for(int i=0;i<ExcelTable.Rows.Count;i++)
for (int j = 0; j < ExcelTable.Columns.Count; j++)
{
//將Excel表中的數據存儲到數組
storedata[i, j] = ExcelTable.Rows[i][j].ToString();
}
int excelBom = 0;//記載表中有效信息的行數,有效信息是指除去表的題目和表的欄目,本例中表的用用信息是從第三行開端
//肯定有效的行數
for (int k = 2; k < ExcelTable.Rows.Count; k++)
if (storedata[k, 1] != "")
excelBom++;
if (excelBom == 0)
{
Response.Write("<script language=javascript>alert('您導入的表格不及格式!')</script>");
}
else
{
//LoadDataToDataBase(storedata,excelBom)//該函數重要擔任將 storedata 中有效的數據寫入到數據庫中,在此不是成績的症結省略
}
}
protected void btninsert_Click(object sender, EventArgs e)
{
foreach (GridViewRow gv in dgBom.Rows)
{
//我的銜接字符串是寫在WEB.CONFIG中的.
string con = System.Configuration.ConfigurationManager.AppSettings["ConnectionString1"].ToString();
SqlConnection conn = new SqlConnection(con);
SqlCommand cmd = conn.CreateCommand();
cmd.CommandType = CommandType.Text;
cmd.CommandText = "insert into student (studentnumber,studentname) values(@studentnumber,@studentname)";
cmd.Parameters.Add("@studentnumber", SqlDbType.NVarChar, 20);
cmd.Parameters.Add("@studentname", SqlDbType.NVarChar, 10);
cmd.Parameters["@studentname"].Value = ((TextBox)gv.FindControl("studentname")).Text;
cmd.Parameters["@studentnumber"].Value = ((TextBox)gv.FindControl("studentnumber")).Text;
try
{
conn.Open();
cmd.ExecuteNonQuery();
conn.Close();
}
finally
{
if (conn != null)
conn.Dispose();
}
}
}
以上內容就是本文的全體論述,願望對年夜家進修C#.NET中若何批量拔出年夜量數據到數據庫中有所贊助。