从server.mathpath获取文件夹路径时出错 [英] Error when getting the folderpath from server.mathpath

查看:175
本文介绍了从server.mathpath获取文件夹路径时出错的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,

我正在使用以下代码读取excel文件并将其保存到数据库,基于此示例(从Excel导入数据到SQL Server 2008

Hello,
I'm using the following code for reading an excel file and save it to database, based on this example (Import data from Excel to SQL Server 2008)

string FileName = lblFileName.Text;
string Extension = Path.GetExtension(FileName);
string FolderPath = Server.MapPath(ConfigurationManager.AppSettings["FolderPath"]);
string CommandText = "";

switch (Extension)
{
    case ".xls": //Excel 97-03
        CommandText = "Questionnaire.spx_ImportFromExcel03";
        break;
    case ".xlsx": //Excel 07
        CommandText = "Questionnaire.spx_ImportFromExcel07";
        break;
}

//Read Excel Sheet using Stored Procedure
//And import the data into Database Table
String strConnString = ConfigurationManager.ConnectionStrings["connectionString"].ConnectionString;
SqlConnection con = new SqlConnection(strConnString);
SqlCommand cmd = new SqlCommand();
cmd.CommandType = CommandType.StoredProcedure;
cmd.CommandText = CommandText;
cmd.Parameters.Add("@SheetName", SqlDbType.VarChar).Value =  ddlSheets.SelectedItem.Text;
cmd.Parameters.Add("@FilePath", SqlDbType.VarChar).Value = FolderPath + FileName;
cmd.Parameters.Add("@HDR", SqlDbType.VarChar).Value = rbHDR.SelectedItem.Text;
cmd.Parameters.Add("@TableName", SqlDbType.VarChar).Value = txtTable.Text;
cmd.Connection = con;

try
{
    con.Open();
    object count = cmd.ExecuteNonQuery();
}

catch (Exception ex)
{
    lblMessage.Text = ex.Message;
}
finally
{
    con.Close();
    con.Dispose();
}



我注意到我得到的错误是因为FolderPath不正确。打印FolderPath + FileName时,我得到以下内容:


I noticed that the error I get is because the FolderPath is not correct. When printing the FolderPath + FileName I get the following:

C:Visual StudioTestTestTestAdminFilesExcel07.xlsx

而不是

C:\Visual Studio\Test\Test\Test\Admin\Files\Excel07.xlsx





任何形式的帮助都会非常感谢,谢谢。



我的尝试:



尝试添加路径手册以查看它是否有效



Any kind of help would be appreciated, thank you.

What I have tried:

Tried adding the path manual to see if it works

string FolderPath = "C:\Visual Studio\Test\Test\Test\Admin\Files\Excel07.xlsx";

确实如此。

推荐答案

我相信 Server.MapPath 仅适用于网站的文件夹层次结构。



如果您的客户端正在上载随后导入数据库的文件,则必须在该站点的文件夹层次结构中为该客户端上载的文件提供一个文件夹。此时, MapPath 应该按预期工作。



在您的网站解决方案中,添加一个名为的文件夹上传,然后在该客户端的上传中添加一个文件夹。更改您的代码以使用该文件夹来存储/检索新文件。
I believe Server.MapPath only works within the folder hierarchy of the web site.

If your client is uploading a file that is then imported into a database, you must provide a folder for that clients uploaded files WITHIN the folder hierarchy of the site. At that point, MapPath should work as you expect.

In your web site solution, add a folder called Uploads, and then add a folder in uploads for that client. Change your code to use that folder to store/retrieve new files.


使用Path.Combine方法 [ ^ ]例如

Instead of using folderpath+Filename use Path.Combine Method[^] e.g.
cmd.Parameters.Add("@FilePath", SqlDbType.VarChar).Value = 
                        Path.Combine(FolderPath,FileName);

只有在需要时才会正确放入分隔符

It will put in the separators properly and only if required


这篇关于从server.mathpath获取文件夹路径时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆