0

在这里,我正在上传 Excelsheet,它将数据添加到 sql 表中。代码在本地主机上完美运行。在服务器上托管它给我错误后, 访问路径“D:\INETPUB\VHOSTS\vivek-vichar.org....\temp\excelsheets\Student RegistrationTrial2.xls”被拒绝。

我已经创建了 temp\excelsheets 目录。

protected void import_xls_Click(object sender, EventArgs e)
    {
        try
        {
            string savePath;
            savePath = Server.MapPath("/temp/excelsheets");

            if (xlsupload.HasFile)
            {
                string fileName = xlsupload.FileName;
                savePath = Path.Combine(savePath, fileName);
                xlsupload.SaveAs(savePath);                  

                string excelConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + savePath + ";Extended Properties='Excel 8.0;HDR=YES;IMEX=1'";

                OleDbConnection excelConnection = new OleDbConnection(excelConnectionString);
                excelConnection.Open();

                OleDbCommand cmd = new OleDbCommand("select * from [Sheet2$]", excelConnection);                    
                OleDbDataReader dReader = cmd.ExecuteReader();

                SqlBulkCopy sqlBulk = new SqlBulkCopy(SqlConnectionstring.mainConnectionString, SqlBulkCopyOptions.KeepIdentity);
                if(grouplist.SelectedItem.Text == "A")
                {
                    sqlBulk.DestinationTableName = "dbo.studentA"; 
                }
                else if (grouplist.SelectedItem.Text == "B")
                {
                    sqlBulk.DestinationTableName = "dbo.studentB";
                }
                else if (grouplist.SelectedItem.Text == "C")
                {
                    sqlBulk.DestinationTableName = "dbo.studentC";
                }
                sqlBulk.ColumnMappings.Add("UserName", "UserName");
                sqlBulk.ColumnMappings.Add("Password", "Password");
                sqlBulk.ColumnMappings.Add("Name", "Name");
                sqlBulk.ColumnMappings.Add("Standard", "Standard");
                sqlBulk.ColumnMappings.Add("Division", "Division");
                sqlBulk.ColumnMappings.Add("SchoolName", "SchoolName");
                sqlBulk.ColumnMappings.Add("Language", "Language");                                       

                sqlBulk.WriteToServer(dReader);

                lblmsg.Visible = true;
                lblmsg.Text = "Your data uploaded successfully";
                excelConnection.Close();
            }
        }
        catch (Exception ex)
        {
            lblmsg.Visible = true;
            lblmsg.Text = ex.Message.ToString();

        }

网络配置

<connectionStrings>
<add name="sqlconn" connectionString="Data Source=<servername>;User Id=******;Password=******;persist security info=False;Connect Timeout=0;Max Pool Size=10000" providerName="System.Data.SqlCient" />    

4

2 回答 2

1

在服务器上,找到包含它的文件或文件夹,然后:-

  • 右键单击并选择属性。
  • 将您的进程正在运行的用户帐户添加到具有读/写访问权限的安全权限。
于 2012-08-25T21:23:41.250 回答
0

您的连接字符串用于您的数据库。需要访问电子表格的“用户”是您的 IIS 应用程序正在运行的用户,可能是 IUSR 帐户,或者您的应用程序池配置为使用的帐户,具体取决于 IIS 的版本和设置。

于 2012-08-25T19:51:48.407 回答