Table Names with Spaces

A

AnandVishy

I wrote a program that inserts records into a table in a MS Access
database. For some reason, it works only for tables without spaces in
their names:

There are four tables: "test 1" "test2" "test 3" and "test4" each with
fields word, pos, and meaning.

The following code works only for test2 and test4, the tables whose
names do not contain any spaces.

string tableName="test 1"; //place table name here
OleDbConnection conn=new
OleDbConnection("provider=microsoft.jet.oledb.4.0;data
source=lists.mdb");
OleDbDataAdapter da=new OleDbDataAdapter("select * from
["+tableName+"]",conn);
OleDbCommandBuilder cb=new OleDbCommandBuilder(da);
DataSet ds=new DataSet();
da.Fill(ds);
conn.Close();

DataTable dt=ds.Tables[0];
DataRow dr=dt.NewRow();
dr["word"]="myword";
dr["pos"]="mypos";
dr["meaning"]="mymeaning";
dt.Rows.Add(dr);
try
{
da.Update(ds);
}
catch(OleDbException ole){Console.WriteLine(ole.Message);}
 
B

Bernie Yaeger

Hi,

Refer to tables with spaces by using brackets: test2 but [test 3]

HTH,

Bernie Yaeger
 
W

William \(Bill\) Vaughn

Yes, but do yourself (and the people that inherit your code) get rid of all
embedded spaces in object names. Lots of tools don't work well with them and
you'll be tripping over this problem forever.

--
____________________________________
William (Bill) Vaughn
Author, Mentor, Consultant
MVP, hRD
www.betav.com
Please reply only to the newsgroup so that others can benefit.
This posting is provided "AS IS" with no warranties, and confers no rights.
__________________________________

Bernie Yaeger said:
Hi,

Refer to tables with spaces by using brackets: test2 but [test 3]

HTH,

Bernie Yaeger

AnandVishy said:
I wrote a program that inserts records into a table in a MS Access
database. For some reason, it works only for tables without spaces in
their names:

There are four tables: "test 1" "test2" "test 3" and "test4" each with
fields word, pos, and meaning.

The following code works only for test2 and test4, the tables whose
names do not contain any spaces.

string tableName="test 1"; //place table name here
OleDbConnection conn=new
OleDbConnection("provider=microsoft.jet.oledb.4.0;data
source=lists.mdb");
OleDbDataAdapter da=new OleDbDataAdapter("select * from
["+tableName+"]",conn);
OleDbCommandBuilder cb=new OleDbCommandBuilder(da);
DataSet ds=new DataSet();
da.Fill(ds);
conn.Close();

DataTable dt=ds.Tables[0];
DataRow dr=dt.NewRow();
dr["word"]="myword";
dr["pos"]="mypos";
dr["meaning"]="mymeaning";
dt.Rows.Add(dr);
try
{
da.Update(ds);
}
catch(OleDbException ole){Console.WriteLine(ole.Message);}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top