我有一个asp.net网站表单网站,我想创建一个控件,该控件允许用户浏览图像,然后将图像保存到数据库中。我知道将图像保存到数据库中是不好的做法,但这就是我被告知要做的事情!
有没有人建议最好的方法来做到这一点?
谢谢
最佳答案
从一个样本:
var intDoccumentLength = FileUpload1.PostedFile.ContentLength;
var newDocument = new byte[intDoccumentLength];
var stream = FileUpload1.PostedFile.InputStream;
stream.Read(newDocument, 0, intDoccumentLength);
var sqlConnection = new SqlConnection(sqlConnectionString);
sqlConnection.Open();
var sqlQuery = "INSERT INTO dbo.DocumentData (DocName, DocBytes, DocData, DocCreatedAt) VALUES (@DocName, @DocBytes, @DocData, @DocCreatedAt);"
+ "SELECT CAST(SCOPE_IDENTITY() AS INT)";
sqlCommand = new SqlCommand(sqlQuery, sqlConnection);
sqlCommand.Parameters.Add("@DocName", SqlDbType.NVarChar, 255);
sqlCommand.Parameters.Add("@DocBytes", SqlDbType.Int);
sqlCommand.Parameters.Add("@DocData", SqlDbType.VarBinary);
sqlCommand.Parameters.Add("@DocCreatedAt", SqlDbType.DateTime);
sqlCommand.Parameters["@DocName"].Value = FileUpload1.PostedFile.FileName;
sqlCommand.Parameters["@DocBytes"].Value = intDoccumentLength;
sqlCommand.Parameters["@DocData"].Value = newDocument;
sqlCommand.Parameters["@DocCreatedAt"].Value = DateTime.Now;
var newDocumentId = (Int32) sqlCommand.ExecuteScalar();
sqlConnection.Close();
关于c# - asp.net上传图片,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5432115/