0
C# 응용 프로그램을 사용하여 SQL에 이미지를 저장하고 싶습니다. 그래서이 코드를 가지고 다중 선택이 사실입니다. 필자는 배열 파일 이름을 사용해야 함을 알고 있습니다. 그것을 깨닫도록 도와 줄 수 있습니까? 난 폴더에 하나 이상의 파일을 선택하고 SQL 데이터베이스에 저장해야합니다.C# SQL 다중 선택 이미지 저장
찾아보기 이미지 : SQL에서 그들을 저장 바이너리
byte[] ReadImageToBytes(string sPath)
{
//Initialize byte array with a null value initially.
byte[] data = null;
//Use FileInfo object to get file size.
FileInfo fInfo = new FileInfo(sPath);
long numBytes = fInfo.Length;
//Open FileStream to read file
FileStream fStream = new FileStream(sPath, FileMode.Open,
FileAccess.Read);
//Use BinaryReader to read file stream into byte array.
BinaryReader br = new BinaryReader(fStream);
data = br.ReadBytes((int)numBytes);
return data;
}
에
private void btnBrowseImage_Click(object sender, EventArgs e)
{
OpenFileDialog openFileDialog1 = new OpenFileDialog();
openFileDialog1.Multiselect = true;
openFileDialog1.Title = "Sekili sec";
openFileDialog1.InitialDirectory = "E:\\IKA";
openFileDialog1.Filter = "All files (*.jpg)|*.jpg";
// openFileDialog1.FilterIndex = 10;
openFileDialog1.RestoreDirectory = true;
if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
try
{
txtImagePath.Text = openFileDialog1.FileName;
}
catch (Exception ex)
{
MessageBox.Show("Error: Could not read file from disk. Original error: " + ex.Message);
}
}
}
파일 :
private void btnSave_Click(object sender, EventArgs e)
{
try
{
//Read Image Bytes into a byte array
byte[] imageSampleData = ReadImageToBytes(txtImagePath.Text);
//Initialize SQL Server Connection
// SqlConnection con = new SqlConnection(txtConnectionString.Text);
String strConnString = "Data Source=1.1.1.1;Initial Catalog=Test1;User Id=dffr;Password=dffr;";
SqlConnection con = new SqlConnection(strConnString);
//Set insert query
string query = "INSERT INTO tblImages (FullPath,MyImageSample) values(@FullPath, @MyImageSample)";
//Initialize SqlCommand object for insert.
SqlCommand cmd = new SqlCommand(query, con);
cmd.Parameters.Add(new SqlParameter("@FullPath",
(object)txtImagePath.Text));
cmd.Parameters.Add(new SqlParameter("@MyImageSample",
(object)imageSampleData));
//Open connection and execute insert query.
con.Open();
cmd.ExecuteNonQuery();
con.Close();
}
catch
{
MessageBox.Show("Error while saving image.", "Error");
}
}
루프의 본문에는 무엇이 있어야합니까? – Delphi
"openFileDialog1 '이 (가) 현재 컨텍스트에 존재하지 않습니다."라는 오류 메시지가 나타납니다. – Delphi