C# Save byte[] into a SQL Server database

Programming/C# 2013. 9. 26. 12:59 Posted by 생각하는로뎅
반응형
save
  1. string queryStmt = "INSERT INTO dbo.YourTable(Content) VALUES(@Content)";  
  2.   
  3. using(SqlConnection _con = new SqlConnection(--your-connection-string-here--))  
  4. using(SqlCommand _cmd = new SqlCommand(query, _con))  
  5. {  
  6.    SqlParameter param = _cmd.Parameters.Add("@Content", SqlDbType.VarBinary);  
  7.    param.Value = YourByteArrayVariableHere;  
  8.   
  9.    _con.Open();  
  10.    _cmd.ExecuteNonQuery();  
  11.    _con.Close();  
  12. }  

load
  1. SqlCommand test_scmd = new SqlCommand("SELECT dbo.YourTable FROM Content");  
  2. test_scmd.Connection = SQL_Connection;  
  3. SqlDataReader test_sdr = test_scmd.ExecuteReader();  
  4.   
  5. if (test_sdr.Read())  
  6. {  
  7.     byte[] test_object = test_sdr["Content"as byte[];  
  8. }  
  9. test_sdr.Close();  
반응형

'Programming > C#' 카테고리의 다른 글

C# byte array to Array.ConvertAll:  (0) 2013.10.03
C# Convert hex string to byte array  (0) 2013.09.26
C# SqlDataReade  (0) 2013.09.25
C# write text file  (0) 2013.09.25
c# 16진수 -> 10진수  (0) 2013.09.24