如何从此代码中删除路径以在服务器中运行它 [英] How Do I Remove Path From This Code To Run It In Server
本文介绍了如何从此代码中删除路径以在服务器中运行它的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
protected void btn_Click(object sender, EventArgs e)
{
try
{
if (FileUpload1.HasFile)
{
string path = string.Concat((Server.MapPath("~/temp/" + FileUpload1.FileName)));
FileUpload1.PostedFile.SaveAs(path);
OleDbConnection OleDbcon = new OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + path + ";Extended Properties=Excel 12.0 ;");
OleDbCommand cmd = new OleDbCommand("SELECT * FROM [Sheet1$]", OleDbcon);
OleDbDataAdapter objAdapter1 = new OleDbDataAdapter(cmd);
OleDbcon.Open();
DbDataReader dr = cmd.ExecuteReader();
SqlBulkCopy bulkInsert = new SqlBulkCopy(con_str);
bulkInsert.DestinationTableName = "table1";
bulkInsert.WriteToServer(dr);
OleDbcon.Close();
Array.ForEach(Directory.GetFiles((Server.MapPath("~/temp/"))), File.Delete);
Label1.ForeColor = Color.Green;
Label1.Text = "successfully inserted";
}
else
{
Label1.ForeColor = Color.Red;
Label1.Text = "Please select the File";
}
}
catch (Exception ex)
{
Response.Write(ex.Message);
}
finally
{
}
}
推荐答案
,OleDbcon);
OleDbDataAdapter objAdapter1 = new OleDbDataAdapter(cmd);
OleDbcon.Open();
DbDataReader dr = cmd.ExecuteReader();
SqlBulkCopy bulkInsert = new SqlBulkCopy(con_str);
bulkInsert.DestinationTableName = table1;
bulkInsert.WriteToServer(dr);
OleDbcon.Close();
Array.ForEach(Directory.GetFiles((Server.MapPath) ( 〜/ temp /))),File.Delete);
Label1。 ForeColor = Color.Green;
Label1.Text = 已成功插入;
}
else
{
Label1.ForeColor = Color 。红色;
Label1.Text = 请选择文件;
}
}
catch (例外情况)
{
Response.Write(ex.Message);
}
最后
{
}
}
", OleDbcon); OleDbDataAdapter objAdapter1 = new OleDbDataAdapter(cmd); OleDbcon.Open(); DbDataReader dr = cmd.ExecuteReader(); SqlBulkCopy bulkInsert = new SqlBulkCopy(con_str); bulkInsert.DestinationTableName = "table1"; bulkInsert.WriteToServer(dr); OleDbcon.Close(); Array.ForEach(Directory.GetFiles((Server.MapPath("~/temp/"))), File.Delete); Label1.ForeColor = Color.Green; Label1.Text = "successfully inserted"; } else { Label1.ForeColor = Color.Red; Label1.Text = "Please select the File"; } } catch (Exception ex) { Response.Write(ex.Message); } finally { } }
从你的问题看来,你似乎是混淆输入excel文件路径设置。所以你可以做的是你可以在你的asp.net web.config文件中创建一个appsettings并将实际的文件夹路径放在该键中,然后在你的代码中访问。
例子
From your question it seems that you are confuse about your input excel file path setting . so what you can do is you can creat a one appsettings in your asp.net web.config file and put the actual folder path in that key and then access in your code.
Example
<appsettings>
<add key="path" value="FOLDERPath" />
</appsettings>
然后使用
then use
string path = ConfigurationManager.Appsettings["path"] + "\" + FileUpload1.FileName;
上面显示的代码中没有定义路径。保存Excel文件的路径可以在< appsettings>下的Web.cong文件中定义。标签。这在运行时是不可访问的,即最终用户不知道该路径。
There is no path defined in the above displayed code. The path where the Excel files are saved can be define din the Web.cong file under the <appsettings> tags. That is not accessible at the run time i.e. the path is not known to the end user.
这篇关于如何从此代码中删除路径以在服务器中运行它的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文