简体   繁体   中英

C# + OLEDB insert varchar with special character?

I have a problem with this code, maybe you can give me a hand. Im inserting file information as rows with name, size, ... they are all varchars The problem is that the "name" has this ' tildes in the namefile so when it tryies to parse the data it crashes.

the filename is : Creedence Clearwater Revival - Lookin' Out My Back Door - 09.mp3

after "lookin" there is a '

my command is: oleDbInsertCommand1.CommandText = "INSERT INTO Dirs-Arcs ( nombre , formato , tamaño , path , tags ) VALUES ('"+name+"', '"+formato+"', '"+tamaño+"', '"+path+"', '"+tags+"')";

this works fine with filenames without this ' characters, how can I parse this better???? thanks.

Use parameters:

OleDbCommand cmd = new OleDbCommand ( "INSERT INTO dirs-arcs (nombre, formato, tamano, path, tags) VALUES (@name, @format, @tamano, @path, @tags)", connection);

cmd.Parameters.Add( "@name", OleDbType.VarChar ).Value = name;
// add all parameters

hth

Mario

You may use AccessDataSource with parameters. If you use parameters, you dont have any problem for special chars.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM