我有一個T-SQL和查詢:string queryString = @"SELECT AGENT.Number, PERSON.LoginName, AGENT.EnterpriseName FROM Agent AGENT INNER JOIN Person PERSON ON AGENT.PersonID = PERSON.PersonID WHERE LOWER(EnterpriseName) LIKE @entname";string connStr = null;try{ connStr = ConfigurationManager.ConnectionStrings["DB"].ConnectionString + ToInsecureString(Decrypt(ConfigurationManager.AppSettings["admin"])) + ";";}catch (Exception ex){ Logs.WriteMessage("Error while making connStr " + ex.TargetSite + ex.StackTrace + ex.ToString());}try{ using (SqlConnection connection = new SqlConnection(connStr)) { connection.Open(); using (SqlCommand command = new SqlCommand(queryString, connection)) { SqlParameter param = new SqlParameter { ParameterName = "@entname", Value = "'%" + agentName + "%'" }; command.Parameters.Add(param); using (SqlDataReader reader = command.ExecuteReader()) { while (reader.Read()) { login = (string)reader[1]; userID = (string)reader[0]; } } } connection.Close(); }}這行不通。我沒有得到任何結(jié)果,但是當(dāng)我在查詢中使用值時,我得到了正確的結(jié)果。@parameter參數(shù)未替換為值,因此查詢失敗。請給我一個提示。@entname當(dāng)我停在斷點處并看到查詢時,它看起來是:SELECT AGENT.Number, PERSON.LoginName, AGENT.EnterpriseName FROM Agent AGENT INNER JOIN Person PERSON ON AGENT.PersonID = PERSON.PersonID WHERE LOWER(EnterpriseName) LIKE @entname所以什么都沒有改變。
C# ADO.NET 參數(shù)化查詢
呼喚遠(yuǎn)方
2022-08-20 14:51:02