Click here to Skip to main content
15,894,180 members
Articles / Mobile Apps

Generate PDB files in C#

Rate me:
Please Sign up or sign in to vote.
4.20/5 (13 votes)
10 Jan 2006GPL32 min read 154.2K   1.6K   30  
This article shows how to create a .pdb file to use as database on Palm devices.
/* Pilot Database Creator Classes
 * Developed By: Muhammad Rizwan Sumra
 * Date: December 20,2005
 * Feel free to contact for any Bug or any problem at rsumra@hotmail.com
 */
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.IO;
using System.Text.RegularExpressions;
using System.Data;
namespace WritePDBs
{
	/// <summary>
	/// Summary description for PDBCreator.
	/// </summary>
	public class PDBCreator : System.Windows.Forms.Form
	{
		#region Declarations
		private System.Windows.Forms.GroupBox groupBox1;
		private System.Windows.Forms.Label label1;
		private System.Windows.Forms.Button button1;
		private System.Windows.Forms.Label label2;
		private System.Windows.Forms.Label label3;
		private System.Windows.Forms.Label label4;
		private System.Windows.Forms.Button button2;
		private System.Windows.Forms.Button button3;
		private System.Windows.Forms.Button button4;
		private System.Windows.Forms.TextBox txtCSV;
		private System.Windows.Forms.TextBox txtDestination;
		private System.Windows.Forms.TextBox txtDBName;
		private System.Windows.Forms.TextBox txtCreatorID;
		private System.Windows.Forms.DataGrid dataGrid1;
		private System.Windows.Forms.Label label5;
		private System.Windows.Forms.ComboBox cmbTableNames;
		private DataSet recordsDataSet=new DataSet();
		#endregion 

		#region Auto Generated Code
		/// <summary>
		/// Required designer variable.
		/// </summary>
		private System.ComponentModel.Container components = null;

		public PDBCreator()
		{
			//
			// Required for Windows Form Designer support
			//
			InitializeComponent();

			//
			// TODO: Add any constructor code after InitializeComponent call
			//
		}

		/// <summary>
		/// Clean up any resources being used.
		/// </summary>
		protected override void Dispose( bool disposing )
		{
			if( disposing )
			{
				if(components != null)
				{
					components.Dispose();
				}
			}
			base.Dispose( disposing );
		}

		#region Windows Form Designer generated code
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
			this.groupBox1 = new System.Windows.Forms.GroupBox();
			this.cmbTableNames = new System.Windows.Forms.ComboBox();
			this.label2 = new System.Windows.Forms.Label();
			this.button1 = new System.Windows.Forms.Button();
			this.label1 = new System.Windows.Forms.Label();
			this.txtCSV = new System.Windows.Forms.TextBox();
			this.label3 = new System.Windows.Forms.Label();
			this.label4 = new System.Windows.Forms.Label();
			this.txtDestination = new System.Windows.Forms.TextBox();
			this.txtDBName = new System.Windows.Forms.TextBox();
			this.txtCreatorID = new System.Windows.Forms.TextBox();
			this.button2 = new System.Windows.Forms.Button();
			this.label5 = new System.Windows.Forms.Label();
			this.button3 = new System.Windows.Forms.Button();
			this.button4 = new System.Windows.Forms.Button();
			this.dataGrid1 = new System.Windows.Forms.DataGrid();
			this.groupBox1.SuspendLayout();
			((System.ComponentModel.ISupportInitialize)(this.dataGrid1)).BeginInit();
			this.SuspendLayout();
			// 
			// groupBox1
			// 
			this.groupBox1.Controls.Add(this.cmbTableNames);
			this.groupBox1.Controls.Add(this.label2);
			this.groupBox1.Controls.Add(this.button1);
			this.groupBox1.Controls.Add(this.label1);
			this.groupBox1.Controls.Add(this.txtCSV);
			this.groupBox1.Controls.Add(this.label3);
			this.groupBox1.Controls.Add(this.label4);
			this.groupBox1.Controls.Add(this.txtDestination);
			this.groupBox1.Controls.Add(this.txtDBName);
			this.groupBox1.Controls.Add(this.txtCreatorID);
			this.groupBox1.Controls.Add(this.button2);
			this.groupBox1.Controls.Add(this.label5);
			this.groupBox1.Location = new System.Drawing.Point(0, 0);
			this.groupBox1.Name = "groupBox1";
			this.groupBox1.Size = new System.Drawing.Size(440, 128);
			this.groupBox1.TabIndex = 0;
			this.groupBox1.TabStop = false;
			// 
			// cmbTableNames
			// 
			this.cmbTableNames.Enabled = false;
			this.cmbTableNames.Location = new System.Drawing.Point(248, 94);
			this.cmbTableNames.Name = "cmbTableNames";
			this.cmbTableNames.Size = new System.Drawing.Size(120, 21);
			this.cmbTableNames.TabIndex = 4;
			this.cmbTableNames.SelectedIndexChanged += new System.EventHandler(this.cmbTableNames_SelectedIndexChanged);
			// 
			// label2
			// 
			this.label2.AutoSize = true;
			this.label2.Location = new System.Drawing.Point(16, 48);
			this.label2.Name = "label2";
			this.label2.Size = new System.Drawing.Size(90, 16);
			this.label2.TabIndex = 3;
			this.label2.Text = "Destination Path:";
			this.label2.TextAlign = System.Drawing.ContentAlignment.MiddleLeft;
			// 
			// button1
			// 
			this.button1.Location = new System.Drawing.Point(376, 20);
			this.button1.Name = "button1";
			this.button1.Size = new System.Drawing.Size(56, 24);
			this.button1.TabIndex = 2;
			this.button1.Text = "Browse";
			this.button1.Click += new System.EventHandler(this.button1_Click);
			// 
			// label1
			// 
			this.label1.AutoSize = true;
			this.label1.Location = new System.Drawing.Point(16, 24);
			this.label1.Name = "label1";
			this.label1.Size = new System.Drawing.Size(78, 16);
			this.label1.TabIndex = 1;
			this.label1.Text = "CSV/XML File:";
			this.label1.TextAlign = System.Drawing.ContentAlignment.MiddleLeft;
			// 
			// txtCSV
			// 
			this.txtCSV.Location = new System.Drawing.Point(112, 22);
			this.txtCSV.Name = "txtCSV";
			this.txtCSV.ReadOnly = true;
			this.txtCSV.Size = new System.Drawing.Size(256, 20);
			this.txtCSV.TabIndex = 0;
			this.txtCSV.Text = "";
			// 
			// label3
			// 
			this.label3.AutoSize = true;
			this.label3.Location = new System.Drawing.Point(16, 72);
			this.label3.Name = "label3";
			this.label3.Size = new System.Drawing.Size(89, 16);
			this.label3.TabIndex = 3;
			this.label3.Text = "Database Name:";
			this.label3.TextAlign = System.Drawing.ContentAlignment.MiddleLeft;
			// 
			// label4
			// 
			this.label4.AutoSize = true;
			this.label4.Location = new System.Drawing.Point(16, 96);
			this.label4.Name = "label4";
			this.label4.Size = new System.Drawing.Size(59, 16);
			this.label4.TabIndex = 3;
			this.label4.Text = "Creator ID:";
			this.label4.TextAlign = System.Drawing.ContentAlignment.MiddleLeft;
			// 
			// txtDestination
			// 
			this.txtDestination.Location = new System.Drawing.Point(112, 46);
			this.txtDestination.Name = "txtDestination";
			this.txtDestination.ReadOnly = true;
			this.txtDestination.Size = new System.Drawing.Size(256, 20);
			this.txtDestination.TabIndex = 0;
			this.txtDestination.Text = "";
			// 
			// txtDBName
			// 
			this.txtDBName.Location = new System.Drawing.Point(112, 70);
			this.txtDBName.MaxLength = 32;
			this.txtDBName.Name = "txtDBName";
			this.txtDBName.Size = new System.Drawing.Size(256, 20);
			this.txtDBName.TabIndex = 0;
			this.txtDBName.Text = "";
			// 
			// txtCreatorID
			// 
			this.txtCreatorID.Location = new System.Drawing.Point(112, 94);
			this.txtCreatorID.MaxLength = 4;
			this.txtCreatorID.Name = "txtCreatorID";
			this.txtCreatorID.Size = new System.Drawing.Size(48, 20);
			this.txtCreatorID.TabIndex = 0;
			this.txtCreatorID.Text = "";
			// 
			// button2
			// 
			this.button2.Location = new System.Drawing.Point(376, 44);
			this.button2.Name = "button2";
			this.button2.Size = new System.Drawing.Size(56, 24);
			this.button2.TabIndex = 2;
			this.button2.Text = "Browse";
			this.button2.Click += new System.EventHandler(this.button2_Click);
			// 
			// label5
			// 
			this.label5.AutoSize = true;
			this.label5.Location = new System.Drawing.Point(168, 96);
			this.label5.Name = "label5";
			this.label5.Size = new System.Drawing.Size(70, 16);
			this.label5.TabIndex = 3;
			this.label5.Text = "Select Table:";
			this.label5.TextAlign = System.Drawing.ContentAlignment.MiddleLeft;
			// 
			// button3
			// 
			this.button3.Location = new System.Drawing.Point(256, 376);
			this.button3.Name = "button3";
			this.button3.Size = new System.Drawing.Size(88, 24);
			this.button3.TabIndex = 1;
			this.button3.Text = "Generate PDB";
			this.button3.Click += new System.EventHandler(this.button3_Click);
			// 
			// button4
			// 
			this.button4.Location = new System.Drawing.Point(352, 376);
			this.button4.Name = "button4";
			this.button4.Size = new System.Drawing.Size(88, 24);
			this.button4.TabIndex = 1;
			this.button4.Text = "Exit";
			this.button4.Click += new System.EventHandler(this.button4_Click);
			// 
			// dataGrid1
			// 
			this.dataGrid1.DataMember = "";
			this.dataGrid1.HeaderForeColor = System.Drawing.SystemColors.ControlText;
			this.dataGrid1.Location = new System.Drawing.Point(0, 136);
			this.dataGrid1.Name = "dataGrid1";
			this.dataGrid1.Size = new System.Drawing.Size(440, 232);
			this.dataGrid1.TabIndex = 2;
			// 
			// PDBCreator
			// 
			this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
			this.ClientSize = new System.Drawing.Size(448, 405);
			this.Controls.Add(this.dataGrid1);
			this.Controls.Add(this.button3);
			this.Controls.Add(this.groupBox1);
			this.Controls.Add(this.button4);
			this.Name = "PDBCreator";
			this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
			this.Text = "PDB Creator";
			this.groupBox1.ResumeLayout(false);
			((System.ComponentModel.ISupportInitialize)(this.dataGrid1)).EndInit();
			this.ResumeLayout(false);

		}
		#endregion

		[STAThread]
		static void Main(string[] args)
		{
			Application.Run(new PDBCreator());
		}

		#endregion 

		#region Events
		private void button1_Click(object sender, System.EventArgs e)
		{
			OpenFileDialog ofd=new OpenFileDialog();
			if (ofd.ShowDialog()==DialogResult.OK)
			{
				recordsDataSet=new DataSet();
				this.dataGrid1.DataSource=null;
				this.txtCSV.Text=ofd.FileName;
				if (Path.GetExtension(ofd.FileName)==".csv")
				{
					try
					{
						Regex r = new Regex(",(?=(?:[^\"]*\"[^\"]*\")*(?![^\"]*\"))");
						StreamReader sr=new StreamReader(ofd.FileName);
						DataTable recordsTable=new DataTable(Path.GetFileName(ofd.FileName));
						recordsTable.Clear();
						string wholeFile=sr.ReadToEnd();
						sr.Close();

						string[] rows=wholeFile.Split('\n');		
						string[] cols;
						int columns=0;

						for (int i=0; i<rows.Length; i++)
						{
							cols=r.Split(rows[i]);
							if (cols.Length==0)
								continue;
							if (columns<cols.Length)
							{
								columns=cols.Length;
								for (int j=0; j<columns; j++)
									recordsTable.Columns.Add(new DataColumn(""+(char)(65+j)));
							}
							if (rows[i].Trim()!="")
							{
								recordsTable.Rows.Add(cols);
							}
						}
						recordsDataSet.Tables.Add(recordsTable);
						this.dataGrid1.DataSource=recordsDataSet.Tables[recordsTable.TableName];
						AddValuesInCombo();
					}
					catch(Exception exc)
					{
						MessageBox.Show("Error in Reading CSV: "+exc.Message,"PDB Generator",MessageBoxButtons.OK,MessageBoxIcon.Information);
					}
				}
				else if (Path.GetExtension(ofd.FileName)==".xml")
				{

					try
					{
						this.recordsDataSet.ReadXml(ofd.FileName);
						AddValuesInCombo();
					}
					catch(Exception exc)
					{
						MessageBox.Show("Error in Reading XML: "+exc.Message,"PDB Generator",MessageBoxButtons.OK,MessageBoxIcon.Information);
					}
				}
			}
		}
		private void AddValuesInCombo()
		{
			cmbTableNames.Items.Clear();
			for (int i=0; i<recordsDataSet.Tables.Count; i++)
			{
				if (recordsDataSet.Tables[i].Rows.Count>0 && recordsDataSet.Tables[i].Columns.Count>0 &&recordsDataSet.Tables[i].Columns[0].ColumnMapping!=MappingType.Hidden)
					cmbTableNames.Items.Add(recordsDataSet.Tables[i].TableName);
			}
			if (cmbTableNames.Items.Count>0)
			{
				cmbTableNames.Enabled=true;
				cmbTableNames.SelectedIndex=0;
			}
		}

		private void button2_Click(object sender, System.EventArgs e)
		{
			FolderBrowserDialog sfd=new FolderBrowserDialog();
			if (sfd.ShowDialog()==DialogResult.OK)
			{
				this.txtDestination.Text=sfd.SelectedPath;
			}
		}

		private void button3_Click(object sender, System.EventArgs e)
		{
			if (this.txtCSV.Text=="")
			{
				MessageBox.Show("There is no csv file to genearte PDB","PDB Generator",MessageBoxButtons.OK,MessageBoxIcon.Information);
				return;
			}
			if (this.txtDestination.Text=="")
			{
				MessageBox.Show("Destination Path is not specified to save PDB file","PDB Generator",MessageBoxButtons.OK,MessageBoxIcon.Information);
				return;
			}
			if (this.txtDBName.Text=="")
			{
				MessageBox.Show("Database Name connot be empty","PDB Generator",MessageBoxButtons.OK,MessageBoxIcon.Information);
				return;
			}
			if (this.txtCreatorID.Text=="" || this.txtCreatorID.Text.Length!=4)
			{
				MessageBox.Show("Database Name connot be empty","PDB Generator",MessageBoxButtons.OK,MessageBoxIcon.Information);
				return;
			}

			PDBCreators pdb=new PDBCreators();
			pdb.PDBHeaders.DataBaseName=this.txtDBName.Text;
			pdb.PDBHeaders.CreatorID=this.txtCreatorID.Text;

			DataTable recordsTable;
			if (recordsDataSet.Tables.Count>0 && this.cmbTableNames.Items.Count>0 && recordsDataSet.Tables.Contains(this.cmbTableNames.Text))
				recordsTable=recordsDataSet.Tables[this.cmbTableNames.Text];
			else
				return;
			for (int i=0; i<recordsTable.Rows.Count; i++)
				pdb.AddRecord=recordsTable.Rows[i].ItemArray;
			try
			{
				pdb.GeneratePDB(this.txtDestination.Text+"\\"+this.txtDBName.Text+".pdb");
				MessageBox.Show("PDB Generated Successfully","PDB Generator",MessageBoxButtons.OK,MessageBoxIcon.Information);
			}
			catch(Exception exc)
			{
				MessageBox.Show("Error in PDB Generation"+exc.ToString(),"PDB Generator",MessageBoxButtons.OK,MessageBoxIcon.Information);
			}
		}

		private void button4_Click(object sender, System.EventArgs e)
		{
			Application.Exit();
		}
		private void cmbTableNames_SelectedIndexChanged(object sender, System.EventArgs e)
		{
			try
			{
				if (this.recordsDataSet.Tables.Count>0 && this.recordsDataSet.Tables.Contains(this.cmbTableNames.Text))
					this.dataGrid1.DataSource=this.recordsDataSet.Tables[this.cmbTableNames.Text];	
			}
			catch(Exception exc)
			{
				MessageBox.Show("Invalid Table Name :"+exc.ToString(),"PDB Generator",MessageBoxButtons.OK,MessageBoxIcon.Information);
			}
			
		}
		#endregion 

	}
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The GNU General Public License (GPLv3)


Written By
Web Developer
Pakistan Pakistan
I'm working as Software Engineer at ISF (Islamabad Software Factory) in Pakistan and involves in multiple products having different languages in different phases. My favourite areas are PDA's, Palm's, .Net and webservices......

Comments and Discussions