내 코드에서 수동 이탈을 제거하고 싶습니다. 이 대신 SqlParameter 개체를 사용하고 싶습니다.루프에서 SqlParameters를 추가하는 방법?
foreach (ThreadPost post in ThreadPosts)
{
string newMessage = Clean(post.Message);
string oldMessage = post.Message;
// escape ' character for prevent errors in SQL script
// I want to pass newMessage and oldMessage as an SqlParameters to prevent unexpected changes, but how it could be done based on the code bellow???
newMessage = newMessage.Replace("'", "''");
oldMessage = oldMessage.Replace("'", "''");
cmdText += string.Format("INSERT INTO ThreadCleanup VALUES ({0}, {1}, '{2}', '{3}')",
post.ID.ToString(),
"NULL",
oldMessage,
newMessage);
}
}
if (!string.IsNullOrEmpty(cmdText))
{
using (SqlConnection con = new SqlConnection(CONNSTR))
{
con.Open();
SqlTransaction tran = con.BeginTransaction(IsolationLevel.ReadUncommitted);
try
{
using (SqlCommand cmd = new SqlCommand(cmdText, con, tran))
{
cmd.ExecuteNonQuery(); // updated records
}
tran.Commit();
}
catch (SqlException ex)
{
tran.Rollback();
}
con.Close();
}
}
[무엇을 시도해 봤습니까?] (http://whathaveyoutried.com)? 어디서 붙어 있는지 설명하십시오. – Oded
Replace (" '", "' '")를하고 싶지 않습니다. 이것 대신 newMessage와 oldMessage를 추가하기 위해 SqlParameters를 사용하고 싶습니다. – Vytalyi
예, 당신이 무엇을 원하는지 이해합니다. 나는 네가 ** 시도한 ** 물었습니다. – Oded