gpt4 book ai didi

c# - 空引用异常(从 mysql 下载 BLOB 文件)

转载 作者:行者123 更新时间:2023-11-29 23:13:09 25 4
gpt4 key购买 nike

当单击链接按钮时,我尝试从 mysql 数据库下载 blob 文件。以下是我的代码:

Response.Buffer = true;
Response.Charset = "";
if (context.Request.QueryString["download"] == "1")
{
context.Response.AppendHeader("Content-Disposition", "attachment; filename=" + filename);
}

try
{
Response.BinaryWrite(bytes);
}
catch (NullReferenceException ex) {
LinkButton2.Text = "No file was uploaded";
}

但是它给出了“bytes”上的空引用异常。字节在以下语句中初始化

if (!Convert.IsDBNull(r["proposalDoc"]))
{
this.bytes = (byte[])r["proposalDoc"];
}

数据检索代码:

protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{

this.Time = String.Format("{0:HH:mm:ss}", DropDownList1.SelectedValue);
String query = "Select * from event where time='" + this.Time + "'";
MySqlConnection conn = new MySqlConnection(connection);

MySqlCommand cmd = new MySqlCommand(query, conn);

DateTime time = DateTime.Today;

conn.Open();

MySqlDataReader r = cmd.ExecuteReader();

while (r.Read())
{
TextBox1.Text = r["name"].ToString();
TextBox2.Text = r["Proposedby"].ToString();
if (!Convert.IsDBNull(r["proposalDoc"]))
{
this.bytes = (byte[])r["proposalDoc"];
}

TextBox5.Text = Calendar1.SelectedDate.ToString("d");
TextBox6.Text = r["time"].ToString();
TextBox7.Text = r["Society"].ToString();
TextBox8.Text = r["venue"].ToString();

}

此外,该记录存在于proposalDoc的数据库中

最佳答案

如果您的目的是获取proposalDoc的第一个非空值,请注意( per the MSDN ):

DBNull.Value is used to indicate a value that is missing. It is not equivalent to null or to String.Empty. Therefore, code such as Convert.IsDBNull(null) in C# or Convert.IsDBNull(Nothing) in Visual Basic returns false.

因此,如果 r["proposalDoc"]null,您将把 null 分配给 this.bytes.

克服这个问题的最快方法是对分配进行空合并:

this.bytes = this.bytes ?? (byte[])r["proposalDoc"];

关于c# - 空引用异常(从 mysql 下载 BLOB 文件),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27984998/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com