SQL 从 wcf 服务获取图像并将其放入 table 的服务器查询
SQL Server query that takes image from wcf service and puts it into table
程序员如何从 wcf 服务向 SQL 服务器数据库添加图像?
这是实现从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();
}
}
}
}
现在我需要知道如何在 SQL 服务器中实现它。我假设这是一个查询,但不知道从哪里开始。
我在几个层面上反对 - table 需要主键和其他控制数据。这种设计存在严重缺陷,但鉴于我们被告知的代码按预期工作,问题的答案将是
CREATE TABLE ImageTable ( ImageBinary VARBINARY(MAX) )
我建议您阅读 TSQL DDL(参考 https://msdn.microsoft.com/en-us/library/ff848799.aspx),也许还可以读一本关于数据库设计的书。
更好的是更像
CREATE TABLE ImageTable ( ImageID INT IDENTITY PRIMARY KEY
, ImageBinary VARBINARY(MAX)
, Added DATETIME DEFAULT GETDATE()
)
这会将您的插入内容更改为
string sql = "insert into dbo.ImageTable( ImageBinary ) values (@img)";
你的检索到
string sql = "select ImageBinary from dbo.ImageTable";
但现在如果您想要一次一张图像或删除图像的能力,您就有了一个主键,并且您可以将检索限制在一个日期范围内或按升序或降序进行。
程序员如何从 wcf 服务向 SQL 服务器数据库添加图像?
这是实现从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();
}
}
}
}
现在我需要知道如何在 SQL 服务器中实现它。我假设这是一个查询,但不知道从哪里开始。
我在几个层面上反对 - table 需要主键和其他控制数据。这种设计存在严重缺陷,但鉴于我们被告知的代码按预期工作,问题的答案将是
CREATE TABLE ImageTable ( ImageBinary VARBINARY(MAX) )
我建议您阅读 TSQL DDL(参考 https://msdn.microsoft.com/en-us/library/ff848799.aspx),也许还可以读一本关于数据库设计的书。
更好的是更像
CREATE TABLE ImageTable ( ImageID INT IDENTITY PRIMARY KEY
, ImageBinary VARBINARY(MAX)
, Added DATETIME DEFAULT GETDATE()
)
这会将您的插入内容更改为
string sql = "insert into dbo.ImageTable( ImageBinary ) values (@img)";
你的检索到
string sql = "select ImageBinary from dbo.ImageTable";
但现在如果您想要一次一张图像或删除图像的能力,您就有了一个主键,并且您可以将检索限制在一个日期范围内或按升序或降序进行。