How ASP.net (C #) reads the data from the. txt file in the upload process and writes it to the database

Source: Internet
Author: User
private void ReadFile (string path)
{
if (System.IO.File.Exists (path))//Determine if the file exists for the specified path
{
StreamReader reader=new StreamReader (path,system.text.encoding.getencoding ("GB2312"));
Instantiate a StreamReader, and specify the encoding method, otherwise read Chinese will appear garbled
Reader. ReadLine ()//read one line first (if there is no title or something above the file, do not have this line of code)
String oneline;
while (Oneline =reader. ReadLine ())!= null)
{
Oneline=oneline. Trim ();
if (oneline!= "")
{
string [] ss=oneline. Split (",". ToCharArray ());//The data in each row is separated and stored in an array.
if (!existedname (ss[0). ToString ())) Ss[0] is the first element of the array to determine whether the data in the database exists
{
StringBuilder sb=new StringBuilder (); instantiation StringBuilder
Sb. Append ("INSERT into tab_adminuser values (");
for (int i=0;i<ss. length;i++)
{
Sb. Append ("'" "+ss[i]+" "" + ",");

}
Sb. Append ("' 3, '"); the above sentence is the INSERT statement that constructs SQL
Sb. Remove (sb.) ToString (). LastIndexOf (","), 1);

Sb. Append (")");
Response.Write (sb.) ToString ());
Response.End ();
Dc.exesql (sb.) ToString ());//Execution SQL,DC is one of my classes, the function is to complete the database insert
}


}
}
Reader. Close ()//Remember to turn off the input output stream

}
Else
{
Response.Write (' <script language= ' JavaScript ' >alert (' The specified file does not exist. '); History.go ( -1);</script> ");
Response.End ();
}
}
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.