0

嗨,我有一个 500 行的 excel 表,我想将该表上传到服务器上并将其数据插入到 sqlserver,并且我创建了名为 excel 的表,具有与 excel 表相同的列,数据需要存储到此表在上传 Excel 表时,当我只想检索一行时,我只想显示该行,这怎么可能,谁能告诉我?我不想使用 ms 访问和 oledb 连接,我有在我的项目中使用了 Sql server 2005.. 我试过这样,请告诉我错误在哪里............

protected void btnSend_Click(object sender, EventArgs e)
        {
            con.Open();
            String strConnection = "user id=sa;pssword=E@2013;Data Source=EKTHA-3D34;Initial Catalog=Ektha;Integrated Security=True";

                  string path = fileuploadExcel.PostedFile.FileName;
                         //Create connection string to Excel work book
                 // string excelConnectionString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + path + ";Extended Properties=Excel 12.0;Persist Security Info=False";
                  string excelConnectionString = "user id=sa;password=E@2013;Data Source=EKTHA-3D34;Persist Security Info=False";
                     //Create Connection to Excel work book
                     SqlConnection excelConnection =new SqlConnection(excelConnectionString);
                      //Create OleDbCommand to fetch data from Excel
                      SqlCommand cmd = new SqlCommand("Select * from [Sheet1$]",excelConnection);
                     excelConnection.Open();
                      SqlDataReader dReader;
                        dReader = cmd.ExecuteReader();
                        SqlBulkCopy sqlBulk = new SqlBulkCopy(strConnection);
                       //Give your Destination table name
                        sqlBulk.DestinationTableName = "PayDescription";
                          sqlBulk.WriteToServer(dReader);
                         excelConnection.Close();
                         }

    }
}
4

1 回答 1

0

你没必要那么挣扎。将第一行保留为表格中所需的列。然后将您的 excel 工作表转换为 xls(97-2003) 格式。右键单击您的数据库任务->导入->单击下一步->选择数据源 microsoft excel 并检查第一行作为列。给 next.confirm 身份验证给下一个,下一个,在网格中检查第二行并进行编辑选择正确的数据类型。然后继续。该表已导入数据库。只需根据需要重命名即可。

于 2013-11-01T07:04:31.617 回答