Protected void shang_click (Object sender, eventargs E)
{
String filename = This. fileupload1.filename. Trim ();
If (! Filename. endswith (". txt "))
{
Page. clientscript. registerstartupscript (GetType (), "", "<SCRIPT> alert ('only TXT docs' can be imported) </SCRIPT> ");
Return;
}
String Path = server. mappath ("./");
If (! Directory. exists (path + "wenj "))
{
Directory. createdirectory (path + "wenj ");
}
String paths = path + "wenj \" + filename;
Fileupload1.postedfile. saveas (paths );
// Fileupload1.saveas (paths );
Dataset DS = sqldatasettxt (dataconnectiontxt (path + "wenj"), "select * from" + fileupload1.filename. Trim ());
This. gridview1.datasource = Ds. Tables [0];
This. gridview1.databind ();
}
Public oledbconnection dataconnectiontxt (string txtfolder)
{
String _ dbconnstring = "provider = Microsoft. Jet. oledb.4.0; Data Source =" + txtfolder + "; extended properties = 'text; HDR = no; FMt = delimited ';";
VaR txtconnection = new oledbconnection (_ dbconnstring );
Try
{
Txtconnection. open ();
Return txtconnection;
}
Catch (exception)
{
Throw new exception ("the error message returned by the attacker cannot be returned. Please confirm the correctness of the path again! ");
}
}
Public dataset sqldatasettxt (oledbconnection, string sqlstr)
{
VaR tempdateset = new dataset ();
Try
{
VaR oledbcommand = new oledbcommand (sqlstr, oledbconnection );
VaR oledbdataadapter = new oledbdataadapter (oledbcommand );
Oledbdataadapter. Fill (tempdateset );
Return tempdateset;
}
Catch (exception)
{
If (oledbconnection. State! = Connectionstate. Closed)
{
Oledbconnection. Close ();
}
Throw new exception (exception. Message );
}
Finally
{
Oledbconnection. Close ();
}
}