[英]SQL Server query that takes image from wcf service and puts it into table
How is a programmer able to add an image to a SQL server database from a wcf service? 程序员如何从wcf服务向SQL Server数据库添加映像?
This is the code that implements adding the image from the wcf service: 这是实现从wcf服务添加图像的代码:
public static void StoreImageIntoDB(string imagePath)
{
string connectionString = "server=XXXX.XXXX.XXXX.XXXX;uid=sa;password=XXXXXX;database=XXXXXX;";
using (SqlConnection conn = new SqlConnection(connectionString))
{
conn.Open();
string sql = "insert into ImageTable values (@img)";
using (SqlCommand cmd = new SqlCommand(sql, conn))
{
FileStream fs = new FileStream(imagePath, FileMode.Open, FileAccess.ReadWrite);
byte[] imageBytes = new byte[fs.Length];
fs.Read(imageBytes, 0, Convert.ToInt32(fs.Length));
fs.Dispose();
cmd.Parameters.Add("@img", SqlDbType.Image).Value = imageBytes;
cmd.ExecuteNonQuery();
}
}
}
public static void RetrieveImageFromDB()
{
string connectionString = "server=XXXX.XXXX.XXXX.XXXX;uid=sa;password=XXXXXX;database=XXXXXX;";
using (SqlConnection conn = new SqlConnection(connectionString))
{
conn.Open();
string sql = "select * from ImageTable";
using (SqlCommand cmd = new SqlCommand(sql, conn))
{
SqlDataAdapter adapter = new SqlDataAdapter(cmd);
DataSet set = new DataSet();
adapter.Fill(set);
foreach (DataRow row in set.Tables[0].Rows)
{
byte[] imageBytes = row[1] as byte[];
MemoryStream ms = new MemoryStream(imageBytes);
Image image = Image.FromStream(ms);
image.Save(string.Format(@"D:\{0}.jpg", row[0].ToString()));//Save the image to disk
image.Dispose();
ms.Dispose();
}
}
}
}
Now I am in need of knowing how to implement this in the SQL server. 现在,我需要知道如何在SQL Server中实现它。 I'm assuming it's a query, but do not know where to begin.
我假设这是一个查询,但不知道从哪里开始。
I object on several levels - the table needs a primary key and other control data. 我在几个级别上提出反对-该表需要一个主键和其他控制数据。 This design is seriously flawed, but the answer to the question as asked given the code that we have been told works as intended, would be
这种设计存在严重缺陷,但是鉴于我们被告知可以按预期工作的代码,对于该问题的答案将是
CREATE TABLE ImageTable ( ImageBinary VARBINARY(MAX) )
I suggest you read up on TSQL DDL (ref https://msdn.microsoft.com/en-us/library/ff848799.aspx ) and perhaps get a book on database design. 我建议您阅读TSQL DDL(请参阅https://msdn.microsoft.com/zh-cn/library/ff848799.aspx ),并可能会获得有关数据库设计的书。
Much better would be something more like 更好的是更像
CREATE TABLE ImageTable ( ImageID INT IDENTITY PRIMARY KEY
, ImageBinary VARBINARY(MAX)
, Added DATETIME DEFAULT GETDATE()
)
which would change your insert to 这会将您的插入内容更改为
string sql = "insert into dbo.ImageTable( ImageBinary ) values (@img)";
and your retrieval to 和您的检索到
string sql = "select ImageBinary from dbo.ImageTable";
But now you have a primary key if you wanted one image at a time or the ability to remove an image, and you could limit the retrieval to a date range or go in ascending or descending order. 但是现在,如果您一次想要一张图像或具有删除图像的功能,则有了主键,并且可以将检索限制在日期范围内,或者以升序或降序排列。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.