2011-06-23 98 views

回答

0
// Read image file from a file 
FileStream stream = new FileStream("pathToFile", FileMode.Open); 
// convert to byte array to save in db 
byte[] myImage = new byte[stream.Length]; 
stream.Read(myImage, 0, myImage.Length); 

// sample query with parameters to insert into db 
string sqlQuery = "INSERT INTO [UserProfile] (UserID, Picture) Values (@userId, @picture)"; 
// conn is your db connection 
SqlCommand command = new SqlCommand(sqlQuery, conn); 
// creating parameters 
SqlParameter paramId = new SqlParameter("@userId", SqlDbType.Int, 4); 
paramId.Value = 45; 
// you picture parameter, and assigning its the value 
SqlParameter paramPicture = new SqlParameter("@picture", SqlDbType.Binary, myImage.Length); 
paramPicture.Value = myImage; 
// adding params to command 
command.Parameters.Add(paramId); 
command.Parameters.Add(paramPicture); 
// then execute your command 
command.ExecuteNonQuery(); 
相關問題