asp.net read the text file and insert the implementation code of the database _ practical Tips

Source: Internet
Author: User
Tags datetime reflection
As a result, you need to manipulate the following steps
1, upload txt file to the company system
2, read the required content
3, insert the content into the database (need to judge the duplicate)
4, compared with the existing order data
This program only studies what needs to be read and insert the database
Copy Code code 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? _returnfee;
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? Returnfee
{
set {_returnfee = value;}
get {return _returnfee;}
}
<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 () {}
#region Add Data
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. Returnfee!= null)
{
Strsql1.append ("Returnfee,");
Strsql2.append ("" + model. Returnfee + ",");
}
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["Returnfee"]. ToString ()!= "")
{
Model. Returnfee = 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 ()
{
Method of reflection Gets the field
Settlementcmbmodel model = new Settlementcmbmodel ();
propertyinfo[] obj = model. GetType (). GetProperties ();
DataTable dt = new DataTable ();
foreach (PropertyInfo p in obj)
{
Dt. Columns.Add (P.name);
}
Structural 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 ();
Perform Insert database action
int j = 0;
foreach (DataRow dr in Dt. Rows)
{
Settlementcmbmodel _model = Settlementcmbdal.getmodel (DR);
When processing a transaction time import occurs in a problem
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";
Order number plus a 0
_model. OrderNo = "0" + _model. OrderNo;
if (Sqlhelper.getrecordcount ("CRM_SETTLEMENT_CMB", "OrderNo like") + _model. OrderNo + "'") ==0)
{
J + + Settlementcmbdal.add (_model);
}
}
This. Literal1.text = "Successful Update" + j + "line";
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 txt, and then construct a DataTable, and then by reflection to get model, and then insert the database, this example for the convenience of the description, a few classes on a page, there are more cattle than the method please advise, 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.