Who know ? how to Read and Split Textfile by Fixed Length

Read Split Spilte3 use Fixed length to count and insert to spilte by C

Page 1 of 1

3 Replies - 7345 Views - Last Post: 30 April 2009 - 10:53 PM Rate Topic: -----

#1 Ameto  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 29-April 09

Who know ? how to Read and Split Textfile by Fixed Length

Posted 30 April 2009 - 12:15 AM

Now I've open and Read Textfile
But I've still don't know how to count , Read and Split it

I use splite3 to be my database here is code and Form

In this code contain search and load and open textfile but I wanna choose my textfile and bring the data in textfile to be table but I can't do it



Posted Image
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
using Finisar.SQLite;


namespace Gonnabcrazy
{
	public partial class Form1 : Form
	{
		internal ToolStripMenuItem tsmCurrent;
		public Form1()
		{
			InitializeComponent();
		}
		private SQLiteConnection sql_con;
		private SQLiteCommand sql_cmd;
		private SQLiteDataAdapter DB;
		private DataSet DS = new DataSet();
		private DataTable DT = new DataTable();
		private BindingSource searchBindingBook;
	  
		public class DllNotFoundException : TypeLoadException { }


		private void SetConnection()
		{
			sql_con = new SQLiteConnection
				("Data Source=98.db;Version=3;New=False;Compress=True;");
		}
		private void ExecuteQuery(string txtQuery)
		{
			SetConnection();
			sql_con.Open();
			sql_cmd = sql_con.CreateCommand();
			sql_cmd.CommandText = txtQuery;
			sql_cmd.ExecuteNonQuery();
			sql_con.Close();
		}

		private void AeeButtonclick(object sender, EventArgs e)
		{
			ToolStripMenuItem tsmItem = sender as ToolStripMenuItem;
			tsmItem.Checked = true;
			if (tsmCurrent != null)
			{
				tsmCurrent.Checked = false;
			}
			tsmCurrent = tsmItem;
			tsmItem = null;
		}

		private void toolStripButton1_Buttonclick(object sender, EventArgs e)
		{
			SetConnection();
			sql_con.Open();
			sql_cmd = sql_con.CreateCommand();
		}

		private void DateToolStripMenuItem_Click(object sender, EventArgs e)
		{
			AeeButtonclick(sender, e);
		}

		private void TimeToolStripMenuItem_Click(object sender, EventArgs e)
		{
			AeeButtonclick(sender, e);
		}

		private void toolStripMenuItem3_Click(object sender, EventArgs e)
		{
			AeeButtonclick(sender, e);
		}

		private void toolStripMenuItem2_Click(object sender, EventArgs e)
		{
			AeeButtonclick(sender, e);
		}

		private void CallInToolStripMenuItem_Click(object sender, EventArgs e)
		{
			AeeButtonclick(sender, e);
		}

		private void CalloutToolStripMenuItem_Click(object sender, EventArgs e)
		{
			AeeButtonclick(sender, e);
		}

		private void NumToolStripMenuItem_Click(object sender, EventArgs e)
		{
			AeeButtonclick(sender, e);
		}

		private void toolStripTextBox1_Click(object sender, EventArgs e)
		{

		}

		private void toolStripButton2_Click(object sender, EventArgs e)
		{
			SetConnection();
			sql_con.Open();
			sql_cmd = sql_con.CreateCommand();
			string CommandText = "select Date,Time,CallIn,CallOut from Fon";
			DB = new SQLiteDataAdapter(CommandText, sql_con);
			DS.Reset();
			DB.Fill(DS, "IIN");
			searchBindingBook = new BindingSource();
			searchBindingBook.DataSource = DS;
			searchBindingBook.DataMember = "IIN";
			dataGridView1.DataSource = searchBindingBook;
		}

		private void toolStripButton3_Click(object sender, EventArgs e)
		{
			SetConnection();
			sql_con.Open();
			sql_cmd = sql_con.CreateCommand();
			string CommandText = "select Date,Time,CallIn,CallOut from Fon";
			DB = new SQLiteDataAdapter(CommandText, sql_con);
			DS.Reset();
			DB.Fill(DS, "IIN");
			searchBindingBook = new BindingSource();
			searchBindingBook.DataSource = DS;
			searchBindingBook.DataMember = "IIN";

			if ((this.tsmCurrent != null) || (this.toolStripTextBox1.Text != null))
			{
				if(this.searchBindingBook.SupportsSearching != false){
					int found = this.searchBindingBook.Find(this.tsmCurrent.Tag.ToString(), this.toolStripTextBox1.Text);
					if((found > -1) && this.searchBindingBook.SupportsFiltering != false)
					{
						this.searchBindingBook.Filter = String.Format("{0} = '(1)' ", this.tsmCurrent.Tag, this.toolStripTextBox1.Text);
						DT = DS.Tables[0];
		   dataGridView1.DataSource = searchBindingBook;
			 
				sql_con.Close(); 
		}
	}
}
	
}
	}
}

Attached File(s)



Is This A Good Question/Topic? 0
  • +

Replies To: Who know ? how to Read and Split Textfile by Fixed Length

#2 born2c0de  Icon User is offline

  • printf("I'm a %XR",195936478);
  • member icon

Reputation: 180
  • View blog
  • Posts: 4,667
  • Joined: 26-November 04

Re: Who know ? how to Read and Split Textfile by Fixed Length

Posted 30 April 2009 - 09:18 AM

Moved to C# Forum.
Was This Post Helpful? 0
  • +
  • -

#3 InternalStatic  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 11
  • Joined: 28-April 09

Re: Who know ? how to Read and Split Textfile by Fixed Length

Posted 30 April 2009 - 01:35 PM

Well first of all, I'll need you to elaborate on your goals here. Are you reading/splitting a textfile, or programming using a database engine?

Also, instead of having all of those methods invoking the AeeButtonclick method, just assign that method to the click event directly. There's no reason to have an intermediate step like that if you don't need it--just clogs up your code.
Was This Post Helpful? 0
  • +
  • -

#4 rossporter  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 9
  • Joined: 30-April 09

Re: Who know ? how to Read and Split Textfile by Fixed Length

Posted 30 April 2009 - 10:53 PM

The code below should get you started in parsing a a text file into an ADO.Net DataTable.
Let me know if you have questions.


//Before we start breaking out fields.
//lets create a datatable for the reults
//Asuming you know how many 
//columns are in the file:

int colCount = 2; 
// or how many ever columns
// you have in each row
//if you don't know, do this later
// when you do know.

DataTable stringTable = new DataTable("stringData");
DataColumn[] cols = new DataColumn[colCount]; 
cols[0] = new DataColumn("colA");
cols[1] = new DataColumn("colB");
//etc until you have created all the columns;
stringTable.Columns.AddRange(cols);



//So you get your string
String someString = 
  File.OpenText("filename.txt").ReadToEnd();

//Assuming your want to split the 
//text into fields based on a fixed width of 10
int splitAt = 10;

//Assuming also that \r\n terminate
//a row and therefore a record;
string[] rowSplit = {"\r","\n"}

string[] records = 
  someString.Split(rowSplit,StringSplitOptions.RemoveEmptyEntries);

//You now have an array representing each row or record.

foreach(string row in records)
{
	   string[] columnVals = new string[colCount];

	   for(int i =0; i<colCount;i++)
	   {
		  columnVals[i] = row.SubString[i*splitAt,splitAt]
	   }

	   DataTable.Rows.Add(columnVals);
}
//now you have a table with all the rows.

BindableControl1.DataSource = stringTable;


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1