在这里,我正在上传 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" />