c#如何读取规定格式的TXT文件,并存入数据库

c#如何读取规定格式的TXT文件,并存入数据库,第1张

这个很简单啊 你以一行的形式读取 然后 拿到这行以后 判断这行的格式是否有日期 如果有的话 那麼就是 2009/12/7 否则就是 06:35 电视剧 家有儿女(第一部)

如果你要源码的话我可以现在给你写 有问题HI我 !!

//得到节目TXT

StreamReader sr = new StreamReader("D:\\jiangsutxt", EncodingGetEncoding("GBK"));

//读取到末尾

string txt = srReadToEnd();

//电视台

string dianshitai = stringEmpty;

//判断是否为空文本

if (!txtEquals(stringEmpty))

{

//分割文本

string[] oldString = txtReplace("\r\n", "@")Split('@');

//循环数组

foreach (string newTxt in oldString)

{

//是否日期

if (newTxtIndexOf("/") > 0)

{

//得到日期

}

//是否节目

else if (newTxtIndexOf(":"))

{

//得到节目

}

//电视台

else

{

//得到电视台

dianshitai = newTxt;

}

}

//再怎麼 *** 作数据库不用教了吧

有 没用过java 用的是net应该差不多 有个思路具体的编码 你自己实现 吧 即那个txt文件按照顺序 读取到一个 二维数组当中 , 然后用循环

for(i=0;i<数组的长度;i++)

{

if(数组中的时间 在数据库中可以查到)

{

更新数据库;

}

}

using System;

using SystemCollectionsGeneric;

using SystemText;

using SystemConfiguration;

using SystemData;

using SystemDataOleDb;

namespace Excel

{

/// <summary>

/// Excel数据交换类

/// </summary>

public class Excel : IDisposable

{

#region 自定义类型

/// <summary>

/// 是否将第一行作为表头

/// </summary>

public enum HDR

{

/// <summary>

/// 将第一行作为表头

/// </summary>

Yes,

/// <summary>

/// 不用第一行作为表头

/// </summary>

No

};

/// <summary>

/// Excel文件格式

/// </summary>

public enum ExcelFileFormat

{

/// <summary>

/// Excel97/2003格式

/// </summary>

Excel97OR2003,

/// <summary>

/// Excel2007格式

/// </summary>

Excel2007

};

#endregion

#region 变量

private HDR _excelHDR = HDRNo;

private ExcelFileFormat _excelformat = ExcelFileFormatExcel97OR2003;

private string _connectionString2003 = "Provider=MicrosoftJetOLEDB40;Extended Properties=\"Excel 80;HDR={1};IMEX=1\";data source=\"{0}\"";

private string _connectionString2007 = "Provider=MicrosoftACEOLEDB120;Data Source=\"{0}\";Extended Properties=\"Excel 120;HDR={1};IMEX=1\";";

private string _connectionString = "";

private string _filename;

private OleDbConnection _connection;

private OleDbTransaction _tran;

#endregion

#region 属性

#region ConnectionString

/// <summary>

/// 获取系统的连接字符串

/// </summary>

public string ConnectionString

{

get

{

return this_connectionString;

}

}

#endregion

#region 是否将第一行作为表头

/// <summary>

/// 获取或设置是否将第一行作为表头

/// </summary>

public HDR ExcelHDR

{

get

{

return this_excelHDR;

}

set

{

this_excelHDR = value;

}

}

#endregion

#region Excel文件格式

/// <summary>

/// 获取或设置当前Excel文件的格式

/// </summary>

public ExcelFileFormat ExcelFormat

{

get

{

return this_excelformat;

}

set

{

this_excelformat = value;

}

}

#endregion

#endregion

#region 构造函数

/// <summary>

/// 创建一个Excel文件链接对象

/// </summary>

/// <param name="filename">Excel文件完整路径</param>

/// <param name="excelFormat">Excel文件格式</param>

public Excel(string filename, ExcelFileFormat excelFormat)

{

this_filename = filename;

if (excelFormat == ExcelFileFormatExcel97OR2003)

{

this_connectionString = stringFormat(this_connectionString2003, filename, this_excelHDR);

this_connection = new OleDbConnection(this_connectionString);

}

else if (excelFormat == ExcelFileFormatExcel2007)

{

this_connectionString = stringFormat(this_connectionString2007, filename, this_excelHDR);

this_connection = new OleDbConnection(this_connectionString);

}

}

/// <summary>

/// 创建一个Excel文件链接对象

/// </summary>

/// <param name="filename">Excel文件完整路径</param>

/// <param name="excelFormat">Excel文件格式</param>

/// <param name="hdr">是否将第一行作为表头</param>

public Excel(string filename, ExcelFileFormat excelFormat, HDR hdr)

{

this_filename = filename;

this_excelHDR = hdr;

if (excelFormat == ExcelFileFormatExcel97OR2003)

{

this_connectionString = stringFormat(this_connectionString2003, filename, this_excelHDR);

this_connection = new OleDbConnection(this_connectionString);

}

else if (excelFormat == ExcelFileFormatExcel2007)

{

this_connectionString = stringFormat(this_connectionString2007, filename, this_excelHDR);

this_connection = new OleDbConnection(this_connectionString);

}

}

~Excel()

{

thisDispose();

}

#endregion

#region 方法

#region 事务

#region 开始一个Excel文件事务

/// <summary>

/// 开始一个Excel文件事务

/// </summary>

public void BeginTransaction()

{

if (this_connectionState != ConnectionStateOpen && this_connectionState != ConnectionStateConnecting)

{

this_connectionOpen();

}

this_tran = this_connectionBeginTransaction();

}

#endregion

#region 提交一个Excel文件事务

/// <summary>

/// 提交一个Excel文件事务

/// </summary>

public void CommitTransaction()

{

if (this_tran != null)

{

this_tranCommit();

}

thisDispose();

}

#endregion

#region 回滚一个Excel文件事务

/// <summary>

/// 回滚一个Excel文件事务

/// </summary>

public void RollbackTransaction()

{

if (this_tran != null)

{

this_tranRollback();

}

thisDispose();

}

#endregion

#region 关联一个事务

/// <summary>

/// 关联一个事务

/// </summary>

/// <param name="tran">事务对象</param>

/// <param name="comm">命令对象</param>

private void AddTransactionToCommand(OleDbTransaction tran, OleDbCommand comm)

{

if (tran != null)

{

commTransaction = tran;

}

}

#endregion

#endregion

#region 查询分析

#region DataSet

#region DataSet QueryDataSet(string sql)

/// <summary>

/// 通过一个Excel-SQL语句查询

/// </summary>

/// <param name="sql">sql</param>

/// <returns>DataSet结果集</returns>

public DataSet QueryDataSet(string sql)

{

OleDbCommand sc = new OleDbCommand(sql, this_connection);

if (this_connectionState != ConnectionStateOpen && this_connectionState != ConnectionStateConnecting)

{

this_connectionOpen();

}

thisAddTransactionToCommand(this_tran, sc);

OleDbDataAdapter sda = new OleDbDataAdapter(sc);

DataSet ds = new DataSet();

try

{

sdaFill(ds);

sdaDispose();

scDispose();

}

catch (Exception e)

{

thisLogException(e);

}

return ds;

}

#endregion

#region DataSet QueryDataSet(string ProcedureName,string[] Parameters,object[] Values)

/// <summary>

/// 通过存储过程与参数进行查询

/// </summary>

/// <param name="ProcedureName">存储过程名</param>

/// <param name="Paramters">参数数组</param>

/// <param name="Values">值数组</param>

/// <returns>DataSet数据集</returns>

public DataSet QueryDataSet(string ProcedureName, string[] Parameters, object[] Values)

{

OleDbCommand sc = new OleDbCommand();

scConnection = this_connection;

if (this_connectionState != ConnectionStateOpen && this_connectionState != ConnectionStateConnecting)

{

this_connectionOpen();

}

thisAddTransactionToCommand(this_tran, sc);

scCommandText = ProcedureName;

scCommandType = CommandTypeStoredProcedure;

for (int i = 0; i < ParametersLength; i++)

{

scParametersAdd(new OleDbParameter(Parameters[i], Values[i]));

}

OleDbDataAdapter sda = new OleDbDataAdapter(sc);

DataSet ds = new DataSet();

try

{

sdaFill(ds);

sdaDispose();

scDispose();

}

catch (Exception e)

{

thisLogException(e);

}

return ds;

}

#endregion

#endregion

#region DataTable

#region DataTable QueryDataTable(string sql)

/// <summary>

/// 通过一个Excel-SQL语句查询

/// </summary>

/// <param name="sql">sql</param>

/// <returns>DataTable结果集</returns>

public DataTable QueryDataTable(string sql)

{

OleDbCommand sc = new OleDbCommand(sql, this_connection);

if (this_connectionState != ConnectionStateOpen && this_connectionState != ConnectionStateConnecting)

{

this_connectionOpen();

}

thisAddTransactionToCommand(this_tran, sc);

OleDbDataAdapter sda = new OleDbDataAdapter(sc);

DataTable dt = new DataTable();

try

{

sdaFill(dt);

sdaDispose();

scDispose();

}

catch (Exception e)

{

thisLogException(e);

}

return dt;

}

#endregion

#region DataTable QueryDataTable(string ProcedureName,string[] Parameters,object[] Values)

/// <summary>

/// 通过存储过程与参数进行查询

/// </summary>

/// <param name="ProcedureName">存储过程名</param>

/// <param name="Paramters">参数数组</param>

/// <param name="Values">值数组</param>

/// <returns>DataTable数据集</returns>

public DataTable QueryDataTable(string ProcedureName, string[] Parameters, object[] Values)

{

OleDbCommand sc = new OleDbCommand();

scConnection = this_connection;

if (this_connectionState != ConnectionStateOpen && this_connectionState != ConnectionStateConnecting)

{

this_connectionOpen();

}

thisAddTransactionToCommand(this_tran, sc);

scCommandText = ProcedureName;

scCommandType = CommandTypeStoredProcedure;

for (int i = 0; i < ParametersLength; i++)

{

scParametersAdd(new OleDbParameter(Parameters[i], Values[i]));

}

OleDbDataAdapter sda = new OleDbDataAdapter(sc);

DataTable dt = new DataTable();

try

{

sdaFill(dt);

sdaDispose();

scDispose();

}

catch (Exception e)

{

thisLogException(e);

}

return dt;

}

#endregion

#endregion

#region void

#region void Query(string sql)

/// <summary>

/// 通过一个Excel-SQL语句查询

/// </summary>

/// <param name="sql">sql</param>

public void Query(string sql)

{

OleDbCommand sc = new OleDbCommand(sql, this_connection);

if (this_connectionState != ConnectionStateOpen && this_connectionState != ConnectionStateConnecting)

{

this_connectionOpen();

}

thisAddTransactionToCommand(this_tran, sc);

try

{

scExecuteNonQuery();

scDispose();

}

catch (Exception e)

{

thisLogException(e);

}

}

#endregion

#region void Query(string ProcedureName,string[] Parameters,object[] Values)

/// <summary>

/// 通过存储过程与参数进行查询

/// </summary>

/// <param name="ProcedureName">存储过程名</param>

/// <param name="Paramters">参数数组</param>

/// <param name="Values">值数组</param>

/// <returns>DataSet数据集</returns>

public void Query(string ProcedureName, string[] Parameters, object[] Values)

{

OleDbCommand sc = new OleDbCommand();

scConnection = this_connection;

if (this_connectionState != ConnectionStateOpen && this_connectionState != ConnectionStateConnecting)

{

this_connectionOpen();

}

thisAddTransactionToCommand(this_tran, sc);

scCommandText = ProcedureName;

scCommandType = CommandTypeStoredProcedure;

for (int i = 0; i < ParametersLength; i++)

{

scParametersAdd(new OleDbParameter(Parameters[i], Values[i]));

}

try

{

scExecuteNonQuery();

scDispose();

}

catch (Exception e)

{

thisLogException(e);

}

}

#endregion

#endregion

#endregion

#region 附加功能

#region 获取所有表名称

/// <summary>

/// 获取所有表名称

/// </summary>

/// <returns>string[] 表名称</returns>

public string[] GetShemaTableName()

{

//获取数据表

if (this_connectionState != ConnectionStateOpen && this_connectionState != ConnectionStateConnecting)

{

this_connectionOpen();

}

try

{

DataTable shemaTable = this_connectionGetOleDbSchemaTable(OleDbSchemaGuidTables, new object[] { null, null, null, "TABLE" });

int n = shemaTableRowsCount;

string[] strTable = new string[n];

int m = shemaTableColumnsIndexOf("TABLE_NAME");

for (int i = 0; i < n; i++)

{

DataRow m_DataRow = shemaTableRows[i];

strTable[i] = m_DataRowItemArrayGetValue(m)ToString();

}

return strTable;

}

catch (Exception e)

{

thisLogException(e);

return null;

}

}

#endregion

#endregion

#region Excel文件 *** 作异常日志

/// <summary>

/// Excel文件错误日志记录

/// </summary>

/// <param name="e">异常信息对象</param>

private void LogException(Exception e)

{

throw new Exception(eMessage);

}

#endregion

#endregion

#region IDisposable 成员

public void Dispose()

{

#region 注销Excel文件事务

if (this_tran != null)

{

this_tranDispose();

}

#endregion

if (this_connection != null)

{

#region 关闭Excel文件连接

if (this_connectionState != ConnectionStateClosed)

{

this_connectionClose();

this_connectionDispose();

}

}

#endregion

}

#endregion

}

}

android读取数据库可以使用sqlite一些api进行读取,实例如下:

  / 

      查找一条数据 

      @param uid 

     /  

    public User find(Integer uid){  

        SQLiteDatabase db=dbOpenHelpergetReadableDatabase();  //创建数据库辅助类

        Cursor cursor =dbrawQuery("select  from user where uid=", new String[]{uidtoString()});  //创建一个游标

        if(cursormoveToFirst()){  //循环遍历查找数组

            int uid2=cursorgetInt(cursorgetColumnIndex("uid"));  

            String uname=cursorgetString(cursorgetColumnIndex("uname"));  

            String uaddress=cursorgetString(cursorgetColumnIndex("uaddress"));  

            User user=new User();  

            usersetUid(uid2);  

            usersetUname(uname);  

            usersetUaddress(uaddress);  

            return user;  

        }  

        cursorclose();  

        return null;  

    }

以上就是关于c#如何读取规定格式的TXT文件,并存入数据库全部的内容,包括:c#如何读取规定格式的TXT文件,并存入数据库、java按行读取txt文件并与数据库表中的内容进行匹配处理(问题好像比较复杂哈)、怎么用C#语言实现读取EXCEL的表格结构,在把表格中的数据导入到数据库中等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/sjk/10192089.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-06
下一篇 2023-05-06

发表评论

登录后才能评论

评论列表(0条)

保存