【问题标题】:SQL Server retrieve data only works wehn break Point is placedSQL Server 检索数据仅在放置断点时有效
【发布时间】: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


猜你喜欢
  • 2012-08-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-11-05
  • 1970-01-01
  • 2011-06-28
  • 1970-01-01
相关资源
最近更新 更多