2011-05-12 5 views
0

MySQL 데이터베이스에 파일을 저장하는 데 어려움을 겪고 있습니다. MySQL 데이터베이스에 파일 저장

는이 코드

SqlConnection con = new SqlConnection(Properties.Settings.Default.NorthWestConnectionString); 
SqlDataAdapter da = new SqlDataAdapter("Select * From MyImages", con); 
SqlCommandBuilder MyCB = new SqlCommandBuilder(da); 
DataSet ds = new DataSet("MyImages"); 

da.MissingSchemaAction = MissingSchemaAction.AddWithKey; 
FileStream fs = new FileStream(@"C:\Users\Ruan\Downloads\Gone Fishing.BMP", FileMode.OpenOrCreate, FileAccess.Read); 

byte[] MyData = new byte[fs.Length]; 
fs.Read(MyData, 0, System.Convert.ToInt32(fs.Length)); 

fs.Close(); 

da.Fill(ds, "MyImages"); 

DataRow myRow; 
myRow = ds.Tables["MyImages"].NewRow(); 

myRow["Description"] = "This would be description text"; 
myRow["imgField"] = MyData; 
ds.Tables["MyImages"].Rows.Add(myRow); 
da.Update(ds, "MyImages"); 

txtboxPassword.Text = MyData.ToString(); 
con.Close(); 

으로 데이터베이스에 이미지를 저장하기 위해 관리하지만 것은이 새로운 라인을 추가하는 것입니다. 간단히 기존 행에 이미지/문서를 추가하고 싶습니다. (선호 문서)

일반적으로이 코드는 내 db의 필드를 업데이트하는 데 사용됩니다.

SqlConnection conn = new SqlConnection(); 
conn.ConnectionString = Properties.Settings.Default.Studentsdb1ConnectionString; 
SqlCommand cmd = new SqlCommand(); 
cmd.CommandType = CommandType.Text; 
String SQL = String.Format("UPDATE Students SET First_Name = '{0}' , Last_Name = '{1}', Birth_Date = '{2}' WHERE Student_Nr = '{3}'", txtName.Text, txtSurname.Text, Convert.ToDateTime(dateTimePicker1.Text).ToString("d"), SelectedStudentNr); 


cmd.CommandText = SQL; 

cmd.Connection = conn; 

object result = null; 
ConnectionState previousConnectionState = conn.State; 
try 
{ 
if (conn.State == ConnectionState.Closed) 
{ 
conn.Open(); 
} 
result = cmd.ExecuteScalar(); 
} 
catch (SqlException ex) 
{ 
MessageBox.Show(ex.Message); 
} 


finally 
{ 
if (previousConnectionState == ConnectionState.Closed) 
{ 
conn.Close(); 
} 

이제 파일을 저장할 수없는 것 같습니다. 나는 어딘가에서 뭔가를 놓치고있다.

저를 도와주세요.

+1

DB의 새로운 버전의 대부분이 허용하지만, DB에 이진 파일 데이터를 저장하는 것은 좋지 않습니다. DB에서 항상 많은 양의 데이터를 앞뒤로 가져 오면 성능에 영향을 미칠 것입니다. 내 제안은 대신 DB에 서버에있는 삶의 경로를 저장하는 것입니다 .. –

+1

MySql? 코드는 MS SQL – Anuraj

답변

1

SqlParameter type을 사용하고 이미지 용이 아닌 종류가있는 (더 안전하고 "자연스러운"방법으로) SqlCommand에 전달해야합니다.

+0

에있는 것 같다 Okyyy, 나는 그것을 얻는다. 그래서 그 이미지를위한 것이 아니라 ... Petr에게 감사한다. – mrbunyrabit

+0

아, 네가이 질문을 어디서 해결했는지 말해 주시겠습니까? – mrbunyrabit

관련 문제