PC Review


Reply
Thread Tools Rate Thread

Cannot add integer or long column to microsoft access table using ado.net

 
 
lado1976@yahoo.com
Guest
Posts: n/a
 
      24th Nov 2005
Hi, I have a problem.

I can't add integer or long columns to ms access table using ado.net.

Could you just take a look at it. I think sql syntax is correct.

The weird thing is that in another program it is working and it is the
same code.

If you comment out these two lines of code:

"IDParent INTEGER, "+
"Position INTEGER, "+

then everything is ok, the table is created.

The problem is I can't add LONG or INTEGER columns.

I don't know what's going on, maybe it's some kind of a bug?

Sample usage:

Test.exe c:\\test.mdb




using System;
using System.Data;
using System.Data.OleDb;

namespace Test
{
public class Test
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main(string[] args)
{
Test t = new Test();

if (args.Length ==1)
{
t.Execute(args[0]);
}
}

private string connectionString = "";
private OleDbConnection connection;

private bool CreateTableWorkTopics()
{
string sqlText = "CREATE TABLE WorkTopics "+
"(IDWorkTopic COUNTER, "+
"IDParent INTEGER, "+
"Position INTEGER, "+
"Title CHAR(255), "+
"CONSTRAINT WorkTopics_PK PRIMARY KEY (IDWorkTopic))";

ExecuteNonQuery(sqlText);
return true;
}

private void ExecuteNonQuery(string sqlText)
{
OleDbCommand cmd = new OleDbCommand(sqlText, connection);
cmd.CommandType = CommandType.Text;

cmd.ExecuteNonQuery();
}

public void Execute(string path)
{
try
{
connectionString = "Provider=Microsoft.Jet.OLEDB.4.0;"+
"Data Source=\""+path+"\";"+
"User ID=Admin;"+
"Password=";

// create mdb file
if (!System.IO.File.Exists(path))
{
ADOX.CatalogClass catalog = new ADOX.CatalogClass();
catalog.Create(connectionString);
catalog = null;
}

connection = new OleDbConnection(connectionString);
connection.Open();

CreateTableWorkTopics();

connection.Close();
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
Console.ReadLine();
}
}
}
}

 
Reply With Quote
 
 
 
 
Brendan Reynolds
Guest
Posts: n/a
 
      24th Nov 2005
It's the field name 'Position' - change that, and your code executes without
error. Must be a reserved word.

--
Brendan Reynolds

<(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> Hi, I have a problem.
>
> I can't add integer or long columns to ms access table using ado.net.
>
> Could you just take a look at it. I think sql syntax is correct.
>
> The weird thing is that in another program it is working and it is the
> same code.
>
> If you comment out these two lines of code:
>
> "IDParent INTEGER, "+
> "Position INTEGER, "+
>
> then everything is ok, the table is created.
>
> The problem is I can't add LONG or INTEGER columns.
>
> I don't know what's going on, maybe it's some kind of a bug?
>
> Sample usage:
>
> Test.exe c:\\test.mdb
>
>
>
>
> using System;
> using System.Data;
> using System.Data.OleDb;
>
> namespace Test
> {
> public class Test
> {
> /// <summary>
> /// The main entry point for the application.
> /// </summary>
> [STAThread]
> static void Main(string[] args)
> {
> Test t = new Test();
>
> if (args.Length ==1)
> {
> t.Execute(args[0]);
> }
> }
>
> private string connectionString = "";
> private OleDbConnection connection;
>
> private bool CreateTableWorkTopics()
> {
> string sqlText = "CREATE TABLE WorkTopics "+
> "(IDWorkTopic COUNTER, "+
> "IDParent INTEGER, "+
> "Position INTEGER, "+
> "Title CHAR(255), "+
> "CONSTRAINT WorkTopics_PK PRIMARY KEY (IDWorkTopic))";
>
> ExecuteNonQuery(sqlText);
> return true;
> }
>
> private void ExecuteNonQuery(string sqlText)
> {
> OleDbCommand cmd = new OleDbCommand(sqlText, connection);
> cmd.CommandType = CommandType.Text;
>
> cmd.ExecuteNonQuery();
> }
>
> public void Execute(string path)
> {
> try
> {
> connectionString = "Provider=Microsoft.Jet.OLEDB.4.0;"+
> "Data Source=\""+path+"\";"+
> "User ID=Admin;"+
> "Password=";
>
> // create mdb file
> if (!System.IO.File.Exists(path))
> {
> ADOX.CatalogClass catalog = new ADOX.CatalogClass();
> catalog.Create(connectionString);
> catalog = null;
> }
>
> connection = new OleDbConnection(connectionString);
> connection.Open();
>
> CreateTableWorkTopics();
>
> connection.Close();
> }
> catch (Exception ex)
> {
> Console.WriteLine(ex.ToString());
> Console.ReadLine();
> }
> }
> }
> }
>



 
Reply With Quote
 
Brendan Reynolds
Guest
Posts: n/a
 
      24th Nov 2005
BTW: If you really need to use the field name 'Position', the following also
works ...

"[Position] INTEGER, "+

Note the square brackets.

--
Brendan Reynolds

"Brendan Reynolds" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> It's the field name 'Position' - change that, and your code executes
> without error. Must be a reserved word.
>
> --
> Brendan Reynolds
>
> <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed)...
>> Hi, I have a problem.
>>
>> I can't add integer or long columns to ms access table using ado.net.
>>
>> Could you just take a look at it. I think sql syntax is correct.
>>
>> The weird thing is that in another program it is working and it is the
>> same code.
>>
>> If you comment out these two lines of code:
>>
>> "IDParent INTEGER, "+
>> "Position INTEGER, "+
>>
>> then everything is ok, the table is created.
>>
>> The problem is I can't add LONG or INTEGER columns.
>>
>> I don't know what's going on, maybe it's some kind of a bug?
>>
>> Sample usage:
>>
>> Test.exe c:\\test.mdb
>>
>>
>>
>>
>> using System;
>> using System.Data;
>> using System.Data.OleDb;
>>
>> namespace Test
>> {
>> public class Test
>> {
>> /// <summary>
>> /// The main entry point for the application.
>> /// </summary>
>> [STAThread]
>> static void Main(string[] args)
>> {
>> Test t = new Test();
>>
>> if (args.Length ==1)
>> {
>> t.Execute(args[0]);
>> }
>> }
>>
>> private string connectionString = "";
>> private OleDbConnection connection;
>>
>> private bool CreateTableWorkTopics()
>> {
>> string sqlText = "CREATE TABLE WorkTopics "+
>> "(IDWorkTopic COUNTER, "+
>> "IDParent INTEGER, "+
>> "Position INTEGER, "+
>> "Title CHAR(255), "+
>> "CONSTRAINT WorkTopics_PK PRIMARY KEY (IDWorkTopic))";
>>
>> ExecuteNonQuery(sqlText);
>> return true;
>> }
>>
>> private void ExecuteNonQuery(string sqlText)
>> {
>> OleDbCommand cmd = new OleDbCommand(sqlText, connection);
>> cmd.CommandType = CommandType.Text;
>>
>> cmd.ExecuteNonQuery();
>> }
>>
>> public void Execute(string path)
>> {
>> try
>> {
>> connectionString = "Provider=Microsoft.Jet.OLEDB.4.0;"+
>> "Data Source=\""+path+"\";"+
>> "User ID=Admin;"+
>> "Password=";
>>
>> // create mdb file
>> if (!System.IO.File.Exists(path))
>> {
>> ADOX.CatalogClass catalog = new ADOX.CatalogClass();
>> catalog.Create(connectionString);
>> catalog = null;
>> }
>>
>> connection = new OleDbConnection(connectionString);
>> connection.Open();
>>
>> CreateTableWorkTopics();
>>
>> connection.Close();
>> }
>> catch (Exception ex)
>> {
>> Console.WriteLine(ex.ToString());
>> Console.ReadLine();
>> }
>> }
>> }
>> }
>>

>
>



 
Reply With Quote
 
lado1976@yahoo.com
Guest
Posts: n/a
 
      25th Nov 2005
Thank you very much. You really helped me.

BTW: I have found list of microsoft access reserverd words. If someone
is curious, here it is:

http://support.microsoft.com/?id=321266

 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
SQL server link Access converting decimal to long integer Hoardling via AccessMonster.com Microsoft Access ADP SQL Server 8 1st Feb 2008 09:43 AM
OleDbDataReader Cant read MS Access Long Integer hmcclungiii Microsoft Dot NET 0 6th Apr 2006 09:14 PM
Exporting Access table to dBase - field type Long Integer =?Utf-8?B?YmVnaW5uZXI=?= Microsoft Access External Data 0 10th Jan 2006 10:01 AM
Increasing range of long integer in Access =?Utf-8?B?RmlvbmFDaw==?= Microsoft Access External Data 1 22nd Jul 2005 05:11 PM
Can't create Integer field, a Long Integer is created instead? cbu Microsoft Access Database Table Design 1 2nd Jan 2004 04:58 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 02:43 AM.