0

ExcelシートデータをSQLサーバーテーブルにインポートできますが、列マッピングを実装できません。助けてください。

protected void Button1_Click(object sender, EventArgs e)
{
    string strFileType = System.IO.Path.GetExtension(FileUpload1.FileName).ToString().ToLower();
    string strFileName = FileUpload1.PostedFile.FileName.ToString();

    FileUpload1.SaveAs(Server.MapPath("~/Import/" + strFileName + strFileType));
    string strNewPath = Server.MapPath("~/Import/" + strFileName + strFileType);






        string excelConnectionString = String.Format(@"Provider=Microsoft.Jet.OLEDB.4.0;" + "Data Source="+strNewPath +"; Extended Properties=Excel 8.0;");

    //string excelConnectionString = String.Format(@"Provider=Microsoft.Jet.OLEDB.4.0;" + "Data Source=C:\\myFolder\\Book1.xls;" + "Extended Properties=Excel 8.0;"); 


        // Create Connection to Excel Workbook
        using (OleDbConnection connection = new OleDbConnection(excelConnectionString))
        {
            OleDbCommand command = new OleDbCommand("Select ID,Data FROM [Sheet1$]", connection);

            connection.Open();

            // Create DbDataReader to Data Worksheet
            using (DbDataReader dr = command.ExecuteReader())
            {
                // SQL Server Connection String
                string sqlConnectionString = "Data Source=DITSEC3;Initial Catalog=test;Integrated Security=True";

                con.Open();
                DataTable dt1 = new DataTable();
                string s = "select count(*) from ExcelTable"; 
                string r = ""; 
                SqlCommand cmd1 = new SqlCommand(s, con);
                try 
                { 
                    SqlDataAdapter da1 = new SqlDataAdapter(cmd1); 
                    da1.Fill(dt1);
                }
                catch { } 
                int RecordCount; 
                RecordCount = Convert.ToInt32(cmd1.ExecuteScalar());
                r = RecordCount.ToString(); 
                Label1.Text = r;
                con.Close();
                int prv = Convert.ToInt32(r);





                // Bulk Copy to SQL Server
                using (SqlBulkCopy bulkCopy = new SqlBulkCopy(sqlConnectionString))
                {



                    bulkCopy.DestinationTableName = "ExcelTable";
                    bulkCopy.WriteToServer(dr);

                    con.Open();
                    SqlBulkCopyColumnMapping mapping1 = new SqlBulkCopyColumnMapping("id", "ida");
                    SqlBulkCopyColumnMapping mapping2 = new SqlBulkCopyColumnMapping("data", "dataa");
                    con.Close();

                }
                con.Open();



                DataTable dt = new DataTable(); 
                s = "select count(*) from ExcelTable";  r = ""; 
                SqlCommand cmd = new SqlCommand(s, con); 
                try { SqlDataAdapter da = new SqlDataAdapter(cmd);
                    da.Fill(dt); 
                }
                catch { } 
                RecordCount = Convert.ToInt32(cmd.ExecuteScalar()); 
                r = RecordCount.ToString(); Label1.Text = r;
                con.Close();
                int ltr = Convert.ToInt32(r);
                if (prv == ltr)
                {
                    Label1.Text = "No records Added";
                }
                else
                {
                    Label1.Text = "Records Added Successfully !";
                }

            }
        }

エラー:1つ以上の必須パラメーターに値が指定されていません。

4

1 に答える 1

0

にマッピングを作成しましたが、追加しませんでしたSqlBulkCopy

以下のコードを追加します。

bulkCopy.ColumnMappings.Add(mapping1);
bulkCopy.ColumnMappings.Add(mapping2);
于 2012-10-24T08:59:16.800 に答える