我想在sql表中插入一个可能包含'character的字符串.
我最好的方法是什么?我应该在'之前插入\吗?这是我在ac#代码中的命令:
SqlCommand myCommand = new SqlCommand(
String.Format(
"insert into ACTIVE.dbo.Workspaces_WsToRefile values({0},'{1}',getdate())",
folderId,
NewWorkspaceName),
myConnection);
Run Code Online (Sandbox Code Playgroud)
其中NewWorkspaceName可能包含'字符,因此插入将导致异常.
感谢先进,哈达斯.
你应该使用SqlParameter.http://msdn.microsoft.com/en-us/library/yy6y35y8.aspx
string query = "insert into ACTIVE.dbo.Workspaces_WsToRefile values(@folderID, @newWorkSpace, @createDate)";
using(SqlCommand cmd = new SqlCommand(query, SqlConnection))
{
SqlParameter param = new SqlParameter("@folderID", folderId);
param.SqlDbType = SqlDbType.Int;
cmd.Parameters.Add(param);
.....
}
Run Code Online (Sandbox Code Playgroud)