Showing posts with label character. Show all posts
Showing posts with label character. Show all posts

Monday, March 26, 2012

fetching a string containging character " ' " from Datatable

Hello! I have a problem with selecting some string from a datatable containing character " ' " under the values of some attributes. For example I have a table called Table and attribute type of NVCHAR(255) called SomeString. This attribute contains strings with a charcter " ' " and I am unable to pull out the value using SELECT statement.

For example:

SELECT SomeAttribute FROM Table WHERE SomeString =' He's riding a bike ';

Because character " ' " is reserved for borders of the string and is also a part of my string. Is there any possibility to solve such a "problem"?

Thanx

Just escape the quote char.


SELECT SomeAttribute FROM Table WHERE SomeString =' He''s riding a bike ';

A better thing is using paramitrimized queries. Then you never have to worry about format's or SQL Injection.

It's olso better for the preformance, because you don't need to have to concatenate a string for example:


string query = "SELECT * FROM Table1 WHERE ID = " + txtId.Text + " AND Name = \"" + "txtName.Text + "\"";

No escape characters needed, you doesn't have to think about using a " or not etc.

Parameters are like placeholders, you use them in Stored Procedures as well.

A little example:


// TODO: Set date variable.
DateTime date = DateTime.Now;
// Set query and parameters.
const string query = "SELECT * FROM Table1 WHERE MyDate = @.MyDate";
SqlParameter pMyDate = new SqlParameter("@.MyDate", SqlDbType.DateTime);
pMyDate.Value = date;
// Create connection and open it.
SqlConnection dbConn = new SqlConnection("ConnectingString");
dbConn.Open();
try
{
using(SqlCommand dbCommand = new SqlCommand(query, dbConn))
{
// Add paramter to Command.
dbCommand.Parameters.Add( pMyDate );
// Execute the query and get results.
SqlDataReader reader = dbCommand.ExecuteReader();
try
{
// Walkthrough results.
while(reader.Read())
{
// TODO: Do something with the data.
}
}
finally
{
// Close reader.
reader.Close();
}
}
}
finally
{
// Close connection.
dbConn.Close();
}

|||Thank you!

Monday, March 12, 2012

FAT Fingered my DB name with a space character...

This wa an oops on my part. I created a new DB las week and I fatfingered the space character in it so now it's called: "DUMMY_FATFINGER " (Notice the space)

Other than recreating it how can I rename it correctly? Just clicking on it to rename it does not work. I get the Database already exists error.

Thankssp_rename?|||I forgot to mention it is on a SQL 2005 Server. Is sp_rename SQL 2005? I recall, I think I recall, sp_renamedb was SQL 2K?

Thanks for the quick reply.|||sp_renamedb should still work. everyone has to be logged out first though.|||I get the following error...
Msg 15032, Level 16, State 1, Procedure sp_renamedb, Line 36
The database 'HR_TRACKING' already exists. Specify a unique database name.

Obviously SQL knows the correct name, users are connecting to the correct name that does not have the space at the end. The problem is, when the backup job runs, it is getting the name with the space at the end. When I run the sp_renameDB the system knows already it has the correct name hence the error message above. It's a cosmeting naming thing somewhere in SQL.

Thanks|||Damn I get here BEFORE I get the email

OR

You could dump the db and rename it

Why do you care BTW?|||What Backup Job?

And let me ask this silly question

does the other bd actually exist?|||I care becaus when the backups kick off to backup to a disk device to a file, the filename to backup to comes out to be 'DBNAME_FATFINGER _backup_200710120945.bak'. It can't handle the space in the name and the backup job errors out.

Thanks|||BINGO!!!!

Renaming it twice did the trick!

I actually used the GUI to do it.