我尝试从表CODE
加载NAME_IMAGE
和IMAGES
作为最终jpg的名称...但我不知道如何使用每张图片他的名字和代码......你有什么想法吗?
SqlConnection connection = new SqlConnection("*****");
connection.Open();
SqlCommand command = new SqlCommand("SELECT IMAGE,NAME_IMAGE,CODE FROM IMAGES", connection);
byte[] buffer = (byte[]) command.ExecuteScalar ();
connection.Close();
FileStream fs = new FileStream(@"C:\Users\L\Desktop\\" + (CODE) + "_" + (NAME_IMAGE) + ".jpg", FileMode.Create);
fs.Write(buffer, 0, buffer.Length);
fs.Close();
答案 0 :(得分:2)
您正在阅读多个列,并且您没有where
子句,因此这里的第一个问题是ExecuteScalar
- 旨在读取第一行的第一列仅即可。由于这不是这种情况,您应该查看ExecuteReader
。或者包装读者的工具,例如“dapper”:
using(var connection = new SqlConnection("*****"))
{
var data = connection.Query("SELECT IMAGE,NAME_IMAGE,CODE FROM IMAGES");
foreach(dynamic row in data)
{
byte[] data = row.IMAGE; // yes, as simple as that
string name = row.NAME_IMAGE;
//... do something with data / name
}
}
或通过原始ADO.NET:
using(var connection = new SqlConnection("*****"))
using(var cmd = connection.CreateCommand())
{
cmd.CommandText = "SELECT IMAGE,NAME_IMAGE,CODE FROM IMAGES";
connection.Open();
using(var reader = cmd.ExecuteReader())
{
while(reader.Read())
{
byte[] data = (byte[])reader["IMAGE"];
string name = (string)reader["NAME"];
// ...
}
}
}