Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

Hi all I'm trying to append the current date/time to a table that's dynamically created. In the end i'd like it to be something along the lines of backup_01/01/2004. I'm trying the following, using c# in the codebehind,

DateTime dt = DateTime.Now;
strCommand = "select * into backup_"+dt.Date.ToString()+" from tbl";
sqlComm = new SqlCommand(strCommand, sqlConn);
sqlConn.Open();
sqlComm.ExecuteNonQuery();
sqlConn.Close();

I keep getting errors with the forward slashes in the dt.Date.

 

Has anyone done this before? I know that you can use forward slashes in names for tables but this seems to be giving me trouble.

 

cheers foz

brown m+m, green m+m, it all comes out the same in the end
Posted

sorry disregard this, I found the solution. Just wrap the date in square brackets as follows:

strCommand = "select * into [backup_"+dt.Date.ToString()+"] from tbl";

 

maybe this can help someone in the future.

 

foz

brown m+m, green m+m, it all comes out the same in the end
  • *Experts*
Posted

While you can surround the field name and put almost anything in there for a table (or column) name, the should factor might stop you :)

 

Why not just format the date without slashes? A well formatted date with year first allows for easy sorting as a string (if you ever wanted to show a list of backups, for example):

dt.Date.ToString("yyyyMMdd")

 

-nerseus

"I want to stand as close to the edge as I can without going over. Out on the edge you see all the kinds of things you can't see from the center." - Kurt Vonnegut

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...