【发布时间】:2022-06-16 00:57:22
【问题描述】:
当我使用断点时,从 sqlserver 检索 SQL 值的以下代码返回数据,当它自己运行时,它返回 null
public string GetURLFromSQL(string ClaimNotificationID,string urlColumn)
{
string URLFromSQL = "";
try
{
SqlConnection con = new SqlConnection(ConnnectionString);
con.Open();
SqlCommand cmd2 = new SqlCommand(@"SELECT " + urlColumn +" "+" FROM[dbo].[Notification_Files] where Notification_ID='" + ClaimNotificationID + "'", con);
using (SqlDataReader reader = cmd2.ExecuteReader())
{
if (reader.Read())
{
URLFromSQL = reader[urlColumn].ToString();
}
}
con.Close();
}
catch (Exception ex)
{ }
return URLFromSQL;
}
【问题讨论】:
-
请使用参数化查询 - 通过连接等方式构建 SQL 查询是灾难的根源。它不仅是许多难以调试的语法错误的来源,而且还是 SQL Injection attacks 的大门。
标签: c# sql-server