当前位置: 代码迷 >> .NET Framework >> C# 将txt文本中得数据写进数据库
  详细解决方案

C# 将txt文本中得数据写进数据库

热度:25   发布时间:2016-05-02 00:29:43.0
C# 将txt文本中得数据写进数据库,求高手指教
我现在在做一个winform窗体,已经将机器终端产生的交易数据传输到我这边的文本中,我现在需要将这个文本框中得数据插入到数据库中,求解
文本框中这样显示:
交易流水号 交易时间 兑换方式 流水清单
20111128 2011-11-28 纸币 111
……
……
……
如何把这些数据一一读取出来,根据不同的字段插入到数据库对应的字段中

------解决方案--------------------
C# code
        public static List<String[]> ReadTxt(string filePathName)        {            List<String[]> ls = new List<String[]>();            StreamReader fileReader=new   StreamReader(filePathName);             string strLine="";            while (strLine != null)            {                strLine = fileReader.ReadLine();                if (strLine != null && strLine.Length>0)                {                    ls.Add(strLine.Split(','));                    //Debug.WriteLine(strLine);                }            }            fileReader.Close();            return ls;        }
------解决方案--------------------
stringbuild sb=new stringbuild(); 
StreamReader fileReader=new StreamReader(filePathName); 
string strLine="";
while (strLine != null)
{
strLine = fileReader.ReadLine();
if (strLine != null && strLine.Length>0)
{
string[] Values = strLine .Split(',');
//数据库sql语句
sb.append("insert into table(....)values(values[0],values[1]..)"
}
}
//执行数据库操作
exSQL(sb.tostring());
  相关解决方案