Click here to Skip to main content
16,013,322 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more: (untagged)
textbox1="xxx";
C#
 Table table = new Table { Name = "main" };
   
table.Columns.Append(textBox1.Text,typeof(string)); 

     foreach (Column column in table.Columns)
      if (column.Name != "ID")
       {
           column.Attributes = ColumnAttributesEnum.adColNullable;
       }
   cat.Tables.Append(table);
   cat = null; 


hello sir,i m new to c#.i want give column name from textbox or any other string variables. is it possible ?

i use vc#2010 and access 2007


this is my full code;

C#
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Diagnostics;

namespace WindowsFormsApplication1
{
    using ADOX;
    public partial class Form1 : Form
    {
        string FilePath = "D:\\Testing1.accdb";
        ADOX.Catalog cat = new ADOX.Catalog();
        public Form1()
        {
            InitializeComponent();
        }
        private void Form1_Load(object sender, EventArgs e)
        {
            CreateAndExportLegacyFile(FilePath);
        }
        private void CreateAndExportLegacyFile(string exportFilePath)
      {
           if(!File.Exists(FilePath))
           {            
           var connectionString = "Provider=Microsoft.Jet.OLEDB.4.0;";
           connectionString += "Data Source=" + exportFilePath + ";Jet OLEDB:Engine Type=5"; 
           cat.Create(connectionString);
         }
          
           //DataTable workTable = new DataTable("Testing11");

           //DataColumn workCol = workTable.Columns.Add("CustID", typeof(Int32));
           //workCol.AllowDBNull = false;
           //workCol.Unique = true;

           //workTable.Columns.Add("CustLName", typeof(String));
           //workTable.Columns.Add("CustFName", typeof(String));
           //workTable.Columns.Add("Purchases", typeof(Double));
    textBox1.Text = "XXX";
    Table table = new Table { Name = "Main" };
    table.Columns.Append(textBox1.Text,typeof(string)); //DataTypeEnum.adVarWChar, 50);
   
      foreach (Column column in table.Columns)
       if (column.Name != "ID")
        {
            column.Attributes = ColumnAttributesEnum.adColNullable;
        }
    cat.Tables.Append(table);
    cat = null;
    //Marshal.FinalReleaseComObject(table);
    //Marshal.FinalReleaseComObject(cat.Tables);
    //Marshal.FinalReleaseComObject(cat.ActiveConnection);
    //Marshal.FinalReleaseComObject(cat);
         

//System.Runtime.InteropServices.Marshal.FinalReleaseComObject(workTable);   
//System.Runtime.InteropServices.Marshal.FinalReleaseComObject(cat.Tables);      
//System.Runtime.InteropServices.Marshal.FinalReleaseComObject(cat.ActiveConnection);   
//System.Runtime.InteropServices.Marshal.FinalReleaseComObject(cat);
Posted
Updated 30-Sep-14 20:58pm
v3

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900