System.NullReferenceException - 我错过了什么?

时间:2018-05-08 14:18:12

标签: c# sql-server

早上好! 我有一个“继承”的C#/ SQL程序集 - 我绝对不是C#程序员,但我正在修复这些代码:

using System;
using System.Data;
using System.Data.SqlClient;
using System.Data.SqlTypes;
using System.Collections;
using System.Globalization;
using Microsoft.SqlServer.Server;
using System.Net;
using System.Text;
using System.IO;

public partial class UserDefinedFunctions
{

// Function to return a web URL as a string value.
[Microsoft.SqlServer.Server.SqlFunction(DataAccess = DataAccessKind.Read)]

public static SqlChars GET(SqlString uri, SqlString username, SqlString passwd)
{
    // The SqlPipe is how we send data back to the caller
    SqlPipe pipe = SqlContext.Pipe;
    //Check for an empty uri. We don't always need user/password but the uri is required
    if (uri.IsNull || uri.Value.Trim() == string.Empty)
    {
        return new SqlChars(("Empty uri").ToString());
    }
    SqlChars document;
    String stat;

    // Set up the request, including authentication
    WebRequest req = WebRequest.Create(Convert.ToString(uri));
    if (Convert.ToString(username) != null & Convert.ToString(username) != "")
    {
        req.Credentials = new NetworkCredential(Convert.ToString(username),Convert.ToString(passwd));
    }
    ((HttpWebRequest)req).UserAgent = "CLR web client on SQL Server";
    // Fire off the request and retrieve the response.
    // We'll put the response in the string variable "document".
    SqlContext.Pipe.Send("Making the API call...");
    WebResponse resp;
    Stream dataStream= new MemoryStream();
    StreamReader rdr = new StreamReader("");
    try
    {
        resp = req.GetResponse();
        dataStream = resp.GetResponseStream();
        rdr = new StreamReader(dataStream);
        document = new SqlChars(rdr.ReadToEnd());
        //document = new SqlChars(rdr.ReadToEnd().ToCharArray());
        stat = (((HttpWebResponse)resp).StatusCode).ToString();
        SqlContext.Pipe.Send("Http Status:" +stat);
        // .. and return the output to the caller.
        return (document);
    }
    catch (Exception ex)
    {
        SqlContext.Pipe.Send("Something went wrong!");
        return new SqlChars("ERROR: "+(ex).ToString());
        throw;
    }
    // Close up everything...
    finally
    {
        SqlContext.Pipe.Send("Cleaning up...");
        rdr.Close();
        dataStream.Close();
        //resp.Close();
        SqlContext.Pipe.Send("Exiting the Assembly function...");
    }
}

// Function to submit a HTTP POST and return the resulting output.
[Microsoft.SqlServer.Server.SqlFunction(DataAccess = DataAccessKind.Read)]
public static SqlChars POST(SqlString uri, SqlString postData, SqlString username, SqlString passwd)
{
    SqlPipe pipe = SqlContext.Pipe;
    SqlChars document;
    byte[] postByteArray = Encoding.UTF8.GetBytes(Convert.ToString(postData));
    //Check for an empty uri. We don't always need user/password but the uri is required
    if (uri.IsNull || uri.Value.Trim() == string.Empty)
    {
        return new SqlChars(("Empty uri").ToString());
    }
    String stat;

    // Set up the request, including authentication, 
    // method=POST and encoding:
    WebRequest req = WebRequest.Create(Convert.ToString(uri));
    ((HttpWebRequest)req).UserAgent = "CLR web client on SQL Server";
    if (Convert.ToString(username) != null & Convert.ToString(username) != "")
    {
        req.Credentials = new NetworkCredential(
            Convert.ToString(username),
            Convert.ToString(passwd));
    }
    req.Method = "POST";
    req.ContentType = "application/x-www-form-urlencoded";

    // Submit the POST data
    SqlContext.Pipe.Send("Making the API call...");
    WebResponse resp;
    Stream dataStream= new MemoryStream();
    StreamReader rdr = new StreamReader("");
    try
    {
    dataStream = req.GetRequestStream();
    dataStream.Write(postByteArray, 0, postByteArray.Length);
    dataStream.Close();

    // Collect the response, put it in the string variable "document"
    resp = req.GetResponse();
    dataStream = resp.GetResponseStream();
    rdr = new StreamReader(dataStream);
    document = new SqlChars(rdr.ReadToEnd());
    stat = (((HttpWebResponse)resp).StatusCode).ToString();
    SqlContext.Pipe.Send("Http Status:" + stat);

    return (document);
    }
    catch (Exception ex)
    {
        SqlContext.Pipe.Send("Something went wrong!");
        return new SqlChars("ERROR: "+(ex).ToString());
        throw;
    }
    // Close up everything...
    finally
    {

    // Close up and return
        if (rdr != null)
        {
            rdr.Close();
        }
    dataStream.Close();
   // resp.Close();
    SqlContext.Pipe.Send("Exiting the Assembly function...");
    }
}

}

这段代码编译得很好,但是当我从SQL函数调用程序集时,我收到以下错误:

  

在执行用户定义的例程或>聚合“fn_get_webrequest”期间发生.NET Framework错误:   System.NullReferenceException:未将对象引用设置为对象的实例。

我错过了什么?任何见解将不胜感激......

0 个答案:

没有答案