gpt4 book ai didi

c# - 使用c#从sql server下载文件

转载 作者:太空宇宙 更新时间:2023-11-03 10:53:58 27 4
gpt4 key购买 nike

我开发了一个应用程序,一个 Web 应用程序和一个 C#,文件上传是在 Web 应用程序中完成的,这是我的代码:

 protected void ImageButton2_Click(object sender, ImageClickEventArgs e) // upload documents to database 
{
foreach (HttpPostedFile upFile in FileUpload1.PostedFiles)
{

// Read the file and convert it to Byte Array
string filename = Path.GetFileName(upFile.FileName);
string ext = Path.GetExtension(filename);
string contenttype = String.Empty;


//Set the contenttype based on File Extension
switch (ext)
{
case ".doc":
contenttype = "application/vnd.ms-word";
break;
case ".docx":
contenttype = "application/vnd.ms-word";
break;
case ".xls":
contenttype = "application/vnd.ms-excel";
break;
case ".xlsx":
contenttype = "application/vnd.ms-excel";
break;
case ".jpg":
contenttype = "image/jpg";
break;
case ".png":
contenttype = "image/png";
break;
case ".gif":
contenttype = "image/gif";
break;
case ".pdf":
contenttype = "application/pdf";
break;
case ".rar":
contenttype = "application/rar";
break;
}

if (contenttype != String.Empty)
{

Stream fs = FileUpload1.PostedFile.InputStream;
BinaryReader br = new BinaryReader(fs);
Byte[] bytes = br.ReadBytes((Int32)fs.Length);

//insert the file into database
string strQuery = "insert into UploadFile(referencenumber, Name, ContentType, Data, id)" +
" values (@referencenumber, @Name, @ContentType, @Data, @id)";
SqlCommand cmd = new SqlCommand(strQuery);
cmd.Parameters.Add("@referencenumber", SqlDbType.VarChar).Value = disprefno.Text;
cmd.Parameters.Add("@id", SqlDbType.VarChar).Value = 1;
cmd.Parameters.Add("@Name", SqlDbType.VarChar).Value = filename;
cmd.Parameters.Add("@ContentType", SqlDbType.VarChar).Value
= contenttype;
cmd.Parameters.Add("@Data", SqlDbType.Binary).Value = bytes;
InsertUpdateData(cmd);
}
}
}
private Boolean InsertUpdateData(SqlCommand cmd)
{
//upload class
String strConnString = System.Configuration.ConfigurationManager
.ConnectionStrings["AIConnectionString"].ConnectionString;
SqlConnection con = new SqlConnection(strConnString);
cmd.CommandType = CommandType.Text;
cmd.Connection = con;
try
{
lblUploadStat.Visible = true;
con.Open();
cmd.ExecuteNonQuery();
return true;
}
catch (Exception ex)
{
Response.Write(ex.Message);
return false;
}
finally
{
string myStringVariable1 = string.Empty;
myStringVariable1 = "Documents Was Successfully Sent ";
ClientScript.RegisterStartupScript(this.GetType(), "myalert", "alert('" + myStringVariable1 + "');", true);
lblUploadStat.Visible = false;
con.Close();
con.Dispose();
}
}

现在,我想通过我的 c# 应用程序从 sql 数据库中检索上传文件,我该如何实现这一点

最佳答案

只是为了让我知道,我添加了一个非常非常简单的代码版本:

using (SqlCommand cmd = new SqlCommand("select ContentType from UploadFile WHERE Id = 1"))
{
con.Open();
SqlDataReader obj = cmd.ExecuteReader();
byte[] bytes = (byte [])obj.GetValue(0);
}

关于c# - 使用c#从sql server下载文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20195292/

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