Implementation Code for asp.net to read text files and insert them into the database

Source: Internet
Author: User

The following steps are required:
1. Upload the txt file to the company system.
2. Read the required content
3. Insert the content into the database (duplicate needs to be determined)
4. Compare with existing order data
This program only studies to read the required content and insert the database
Copy codeThe Code is as follows:
Using System;
Using System. Data;
Using System. Collections. Generic;
Using System. Web;
Using System. Web. UI;
Using System. Web. UI. WebControls;
Using System. IO;
Using System. Text;
Using System. Text. RegularExpressions;
Using service. DAL;
Using System. Reflection;
Namespace service. fin
{
[Serializable]
Public class SettlementCmbModel
{
Public SettlementCmbModel (){}
# Region Model
Private int _ id;
Private string _ terminalno;
Private DateTime? _ Tradetime;
Private string _ batchno;
Private string _ channel;
Private string _ empowerno;
Private string _ orderno;
Private string _ cardno;
Private string _ cardmark;
Private string _ tradetype;
Private string _ businesstype;
Private string _ goodsno;
Private string _ stages;
Private decimal? _ Tradeamount;
Private decimal? _ Commissionamount;
Private decimal? _ Returnworkflow;
Private decimal? _ Memberspoint1;
Private decimal? _ Memberspoint2;
Private decimal? _ Mpcmbpercent;
Private decimal? _ Mpvalue;
Private decimal? _ Realamount;
/// <Summary>
///
/// </Summary>
Public int Id
{
Set {_ id = value ;}
Get {return _ id ;}
}
/// <Summary>
///
/// </Summary>
Public string TerminalNo
{
Set {_ terminalno = value ;}
Get {return _ terminalno ;}
}
/// <Summary>
///
/// </Summary>
Public DateTime? TradeTime
{
Set {_ tradetime = value ;}
Get {return _ tradetime ;}
}
/// <Summary>
///
/// </Summary>
Public string BatchNo
{
Set {_ batchno = value ;}
Get {return _ batchno ;}
}
/// <Summary>
///
/// </Summary>
Public string Channel
{
Set {_ channel = value ;}
Get {return _ channel ;}
}
/// <Summary>
///
/// </Summary>
Public string EmpowerNo
{
Set {_ empowerno = value ;}
Get {return _ empowerno ;}
}
/// <Summary>
///
/// </Summary>
Public string OrderNo
{
Set {_ orderno = value ;}
Get {return _ orderno ;}
}
/// <Summary>
///
/// </Summary>
Public string CardNo
{
Set {_ cardno = value ;}
Get {return _ cardno ;}
}
/// <Summary>
///
/// </Summary>
Public string CardMark
{
Set {_ cardmark = value ;}
Get {return _ cardmark ;}
}
/// <Summary>
///
/// </Summary>
Public string TradeType
{
Set {_ tradetype = value ;}
Get {return _ tradetype ;}
}
/// <Summary>
///
/// </Summary>
Public string BusinessType
{
Set {_ businesstype = value ;}
Get {return _ businesstype ;}
}
/// <Summary>
///
/// </Summary>
Public string GoodsNo
{
Set {_ goodsno = value ;}
Get {return _ goodsno ;}
}
/// <Summary>
///
/// </Summary>
Public string Stages
{
Set {_ stages = value ;}
Get {return _ stages ;}
}
/// <Summary>
///
/// </Summary>
Public decimal? TradeAmount
{
Set {_ tradeamount = value ;}
Get {return _ tradeamount ;}
}
/// <Summary>
///
/// </Summary>
Public decimal? CommissionAmount
{
Set {_ commissionamount = value ;}
Get {return _ commissionamount ;}
}
/// <Summary>
///
/// </Summary>
Public decimal? Returnlogs
{
Set {_ returnworkflow = value ;}
Get {return _ returnworkflow ;}
}
/// <Summary>
///
/// </Summary>
Public decimal? MembersPoint1
{
Set {_ memberspoint1 = value ;}
Get {return _ memberspoint1 ;}
}
/// <Summary>
///
/// </Summary>
Public decimal? MembersPoint2
{
Set {_ memberspoint2 = value ;}
Get {return _ memberspoint2 ;}
}
/// <Summary>
///
/// </Summary>
Public decimal? MPCmbPercent
{
Set {_ mpcmbpercent = value ;}
Get {return _ mpcmbpercent ;}
}
/// <Summary>
///
/// </Summary>
Public decimal? MPValue
{
Set {_ mpvalue = value ;}
Get {return _ mpvalue ;}
}
/// <Summary>
///
/// </Summary>
Public decimal? RealAmount
{
Set {_ realamount = value ;}
Get {return _ realamount ;}
}
# Endregion Model
}
Public class SettlementCmbDal
{
Public SettlementCmbDal (){}
# Add data to region
Public static int Add (SettlementCmbModel model)
{
StringBuilder strSql = new StringBuilder ();
StringBuilder strSql1 = new StringBuilder ();
StringBuilder strSql2 = new StringBuilder ();
If (model. TerminalNo! = Null)
{
StrSql1.Append ("TerminalNo ,");
StrSql2.Append ("'" + model. TerminalNo + "',");
}
If (model. TradeTime! = Null)
{
StrSql1.Append ("TradeTime ,");
StrSql2.Append ("'" + model. TradeTime + "',");
}
If (model. BatchNo! = Null)
{
StrSql1.Append ("BatchNo ,");
StrSql2.Append ("'" + model. BatchNo + "',");
}
If (model. Channel! = Null)
{
StrSql1.Append ("Channel ,");
StrSql2.Append ("'" + model. Channel + "',");
}
If (model. EmpowerNo! = Null)
{
StrSql1.Append ("EmpowerNo ,");
StrSql2.Append ("'" + model. EmpowerNo + "',");
}
If (model. OrderNo! = Null)
{
StrSql1.Append ("OrderNo ,");
StrSql2.Append ("'" + model. OrderNo + "',");
}
If (model. CardNo! = Null)
{
StrSql1.Append ("CardNo ,");
StrSql2.Append ("'" + model. CardNo + "',");
}
If (model. CardMark! = Null)
{
StrSql1.Append ("CardMark ,");
StrSql2.Append ("'" + model. CardMark + "',");
}
If (model. TradeType! = Null)
{
StrSql1.Append ("TradeType ,");
StrSql2.Append ("'" + model. TradeType + "',");
}
If (model. BusinessType! = Null)
{
StrSql1.Append ("BusinessType ,");
StrSql2.Append ("'" + model. BusinessType + "',");
}
If (model. GoodsNo! = Null)
{
StrSql1.Append ("GoodsNo ,");
StrSql2.Append ("'" + model. GoodsNo + "',");
}
If (model. Stages! = Null)
{
StrSql1.Append ("Stages ,");
StrSql2.Append ("'" + model. Stages + "',");
}
If (model. TradeAmount! = Null)
{
StrSql1.Append ("TradeAmount ,");
StrSql2.Append ("" + model. TradeAmount + ",");
}
If (model. CommissionAmount! = Null)
{
StrSql1.Append ("CommissionAmount ,");
StrSql2.Append ("" + model. CommissionAmount + ",");
}
If (model. returnworkflow! = Null)
{
StrSql1.Append ("returnpipeline ,");
StrSql2.Append ("" + model. return#+ ",");
}
If (model. MembersPoint1! = Null)
{
StrSql1.Append ("MembersPoint1 ,");
StrSql2.Append ("" + model. MembersPoint1 + ",");
}
If (model. MembersPoint2! = Null)
{
StrSql1.Append ("MembersPoint2 ,");
StrSql2.Append ("" + model. MembersPoint2 + ",");
}
If (model. MPCmbPercent! = Null)
{
StrSql1.Append ("MPCmbPercent ,");
StrSql2.Append ("" + model. MPCmbPercent + ",");
}
If (model. MPValue! = Null)
{
StrSql1.Append ("MPValue ,");
StrSql2.Append ("" + model. MPValue + ",");
}
If (model. RealAmount! = Null)
{
StrSql1.Append ("RealAmount ,");
StrSql2.Append ("" + model. RealAmount + ",");
}
StrSql. Append ("insert into CRM_Settlement_CMB (");
StrSql. Append (strSql1.ToString (). Remove (strSql1.Length-1 ));
StrSql. Append (")");
StrSql. Append ("values (");
StrSql. Append (strSql2.ToString (). Remove (strSql2.Length-1 ));
StrSql. Append (")");
StrSql. Append ("; select @ IDENTITY ");
Return SqlHelper. ExecuteNonQuery (strSql. ToString ());
}
# Endregion
# Region get model
Public static SettlementCmbModel GetModel (DataRow dr)
{
SettlementCmbModel model = new SettlementCmbModel ();
Model. TerminalNo = dr ["TerminalNo"]. ToString ();
If (dr ["TradeTime"]. ToString ()! = "")
{
Model. TradeTime = DateTime. Parse (dr ["TradeTime"]. ToString ());
}
Model. BatchNo = dr ["BatchNo"]. ToString ();
Model. Channel = dr ["Channel"]. ToString ();
Model. EmpowerNo = dr ["EmpowerNo"]. ToString ();
Model. OrderNo = dr ["OrderNo"]. ToString ();
Model. CardNo = dr ["CardNo"]. ToString ();
Model. CardMark = dr ["CardMark"]. ToString ();
Model. TradeType = dr ["TradeType"]. ToString ();
Model. BusinessType = dr ["BusinessType"]. ToString ();
Model. GoodsNo = dr ["GoodsNo"]. ToString ();
Model. Stages = dr ["Stages"]. ToString ();
If (dr ["TradeAmount"]. ToString ()! = "")
{
Model. TradeAmount = decimal. Parse (dr ["TradeAmount"]. ToString ());
}
If (dr ["CommissionAmount"]. ToString ()! = "")
{
Model. CommissionAmount = decimal. Parse (dr ["CommissionAmount"]. ToString ());
}
If (dr ["returnworkflow"]. ToString ()! = "")
{
Model. returnimal = decimal. Parse (dr ["ReturnFee"]. ToString ());
}
If (dr ["MembersPoint1"]. ToString ()! = "")
{
Model. MembersPoint1 = decimal. Parse (dr ["MembersPoint1"]. ToString ());
}
If (dr ["MembersPoint2"]. ToString ()! = "")
{
Model. MembersPoint2 = decimal. Parse (dr ["MembersPoint2"]. ToString ());
}
If (dr ["MPCmbPercent"]. ToString ()! = "")
{
Model. MPCmbPercent = decimal. Parse (dr ["MPCmbPercent"]. ToString ());
}
If (dr ["MPValue"]. ToString ()! = "")
{
Model. MPValue = decimal. Parse (dr ["MPValue"]. ToString ());
}
If (dr ["RealAmount"]. ToString ()! = "")
{
Model. RealAmount = decimal. Parse (dr ["RealAmount"]. ToString ());
}
Return model;
}
# Endregion
}
Public partial class ReadTxt: System. Web. UI. Page
{
Protected void Page_Load (object sender, EventArgs e)
{
}
Protected void txtInput ()
{
// Obtain fields using the Reflection Method
SettlementCmbModel model = new SettlementCmbModel ();
PropertyInfo [] obj = model. GetType (). GetProperties ();
DataTable dt = new DataTable ();
Foreach (PropertyInfo p in obj)
{
Dt. Columns. Add (p. Name );
}
// Construct DataRow
StreamReader sr = new StreamReader (Server. MapPath ("text.txt"), System. Text. Encoding. GetEncoding ("GB2312 "));
While (! Sr. EndOfStream)
{
String s = sr. ReadLine ();
If (s. StartsWith ("NET "))
{
S = s. Trim ();
S = s. Replace ("-","-");
String [] ss = s. Split ('');
Object [] datarow = new object [dt. Columns. Count];
Int I = 0;
Foreach (string st in ss)
{
If (st! = "")
{
Datarow [I] = st;
I ++;
}
}
Dt. Rows. Add (datarow );
}
}
Sr. Close ();
// Insert a database
Int j = 0;
Foreach (DataRow dr in dt. Rows)
{
SettlementCmbModel _ model = SettlementCmbDal. GetModel (dr );
// The problem occurred when processing the transaction time import.
String s = _ model. TerminalNo;
Int year = int. Parse (s. Substring (0, 4 ));
Int month = int. Parse (s. Substring (4, 2 ));
Int day = int. Parse (s. Substring (6, 2 ));
String st = _ model. TradeTime. ToString ();
String [] str = st. Split ('');
String [] stri = str [1]. Split (':');
Int hour = int. Parse (stri [0]);
Int minute = int. Parse (stri [1]);
Int second = int. Parse (stri [2]);
DateTime de = new DateTime (year, month, day, hour, minute, second );
_ Model. TradeTime = de;
_ Model. TerminalNo = "NET ";
// Add 0 to the order number Header
_ Model. OrderNo = "0" + _ model. OrderNo;
If (SqlHelper. GetRecordCount ("CRM_Settlement_CMB", "OrderNo like '" + _ model. OrderNo + "'") = 0)
{
J + = SettlementCmbDal. Add (_ model );
}
}
This. Literal1.Text = "successfully updated" + j + "row ";
// StringBuilder sb = new StringBuilder ();
// Sb. Append ("<table> ");
// Foreach (DataRow dr in dt. Rows)
//{
// Sb. Append ("<tr> ");
// Foreach (DataColumn dc in dt. Columns)
//{
// Sb. Append ("<td>" + dr [dc. ColumnName]. ToString () + "</td> ");
//}
// Sb. Append ("</tr> ");
//}
// Sb. Append ("</table> ");
// This. Literal1.Text = sb. ToString ();
}
}
}

The idea is to read the txt file, construct the able, obtain the model in the reflection mode, and insert it into the database. In this example, several classes are put on one page for convenience, please advise if you have any better method. Thank you.

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.