C# 保存byte[]到数据库和读取,把byte[]写进文件文件中

只能用参数传值的方式才能把byte[]的内容保存到数据
数据库中的字段类型为: varbinary 
 

 

/// <summary>
/// 保存模板
/// </summary>
/// <param name="t"></param>
/// <returns></returns>
public static bool TemplateSave(TemplateEntity t)
{
bool boo = false;
try
{
if (BaseDataAccess.ExecuteQuery("SELECT * FROM TemplateEntity WHERE Name=N'" + t.Name + "'").Rows.Count > 0)
throw new Exception("模板 [" + t.Name + "] 已存在");
 
SqlParameter[] par = { new SqlParameter("@Description",t.Description) ,
new SqlParameter("@TemplateFile",t.TemplateFile)//byte[]数组内容
};
string sql = @"
INSERT INTO dbo.TemplateEntity
( Id ,
Memo ,
CreateDate ,
LastEditDate ,
Printer ,
Name ,
[FileName] ,
[Description],
TemplateFile
)
VALUES ( NEWID(),
N'" + t.Memo + @"' ,
GETDATE() ,
GETDATE() ,
N'" + t.Printer + @"' ,
N'" + t.Name + @"' ,
N'" + t.FileName + @"' ,
@Description ,
@TemplateFile)";
 
if (BaseDataAccess.ExcuteNonQuery(sql, par) > 0)
boo = true;
}
catch (Exception ex)
{
throw ex;
}
return boo;
}
 
 
 
/// <summary>
/// 根据ID查询打印模板
/// </summary>
/// <param name="id"></param>
/// <returns></returns>
public static TemplateEntity GetObjectsById(string id)
{
//return BaseNhibernate<TemplateEntity>.GetObjectsByFrom("from TemplateEntity o where o.Name='" + id + "'").FirstOrDefault();
TemplateEntity t = null;
try
{
DataTable dt = BaseDataAccess.ExecuteQuery("SELECT TOP 1 * FROM TemplateEntity WHERE Name=N'" + id + "'");
if (dt.Rows.Count > 0)
{
t = new TemplateEntity();
t.Id = new Guid(dt.Rows[0]["Id"].ToString());
t.FileName = dt.Rows[0]["FileName"].ToString();
t.Name = dt.Rows[0]["Name"].ToString();
t.Description = dt.Rows[0]["Description"].ToString();
t.Printer = dt.Rows[0]["Printer"].ToString();
if (string.IsNullOrEmpty(dt.Rows[0]["TemplateFile"].ToString()))
throw new Exception("请重新上传打印模板 [ " + t.Name + " ]");
t.TemplateFile = (byte[])dt.Rows[0]["TemplateFile"];
}
}
catch (Exception ex)
{
throw ex;
}
return t;
}
 
 
 
public static string TagsTemplateDir = AppDomain.CurrentDomain.BaseDirectory + @"TagsTemplate";
/// <summary>
/// 下载模板到本地文件夹中
/// </summary>
/// <param name="t"></param>
public static void GetTemplateFile(TemplateEntity t)
{
try
{
WebClient myWebClient = new WebClient();
myWebClient.Credentials = CredentialCache.DefaultCredentials;
Stream postStream = myWebClient.OpenWrite(TagsTemplateDir + "\\" + t.FileName, "PUT");
postStream.Write(t.TemplateFile, 0, t.TemplateFile.Length);
postStream.Close();
 
}
catch (Exception ex)
{
throw ex;
}
}
 

转载于:https://www.cnblogs.com/zishen/p/5896188.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,我可以帮你解决这个问题。 首先,让我们来看一下如何将图片保存数据库。假设你有一个名为`image`的`byte[]`数组,它包含了你要保存的图片数据。以下是一个例子,用于将这个数组保存到 SQL Server 数据库的一个名为`image_table`的表: ```csharp using (SqlConnection connection = new SqlConnection(connectionString)) { connection.Open(); string sql = "INSERT INTO image_table (image_data) VALUES (@ImageData)"; SqlCommand command = new SqlCommand(sql, connection); SqlParameter imageDataParam = new SqlParameter("@ImageData", SqlDbType.VarBinary, -1); imageDataParam.Value = image; command.Parameters.Add(imageDataParam); command.ExecuteNonQuery(); } ``` 上面的代码,`connectionString`是你的数据库连接字符串,`image_table`是你要保存图片的表名,`image_data`是你要保存图片的列名。 接下来是如何从数据库读取图片并显示到窗体上。假设你有一个名为`pictureBox1`的`PictureBox`控件,用于显示图片。以下是一个例子,用于从 SQL Server 数据库的`image_table`表读取图片数据,并将其显示到`pictureBox1`上: ```csharp using (SqlConnection connection = new SqlConnection(connectionString)) { connection.Open(); string sql = "SELECT image_data FROM image_table WHERE id = @Id"; SqlCommand command = new SqlCommand(sql, connection); SqlParameter idParam = new SqlParameter("@Id", SqlDbType.Int); idParam.Value = id; command.Parameters.Add(idParam); SqlDataReader reader = command.ExecuteReader(); if (reader.Read()) { byte[] image = (byte[])reader["image_data"]; MemoryStream stream = new MemoryStream(image); Image img = Image.FromStream(stream); pictureBox1.Image = img; } } ``` 上面的代码,`id`是你要读取图片的行的 ID。`pictureBox1`是你要显示图片的控件。 希望这些代码可以帮助你实现图片上传和显示的功能。如果你有其他问题,请随时问我。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值