Stream sDocStream;
int docLen;
string contentType;
string uploadDocName;
string loc = "test";
sDocStream = uploadfile.PostedFile.InputStream;
docLen = uploadfile.PostedFile.ContentLength;
uploadDocName = uploadfile.PostedFile.FileName;
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
byte[] docBinaryData = new byte[docLen];
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
sDocStream.Read(docBinaryData, 0, docLen);
contentType = "doc";
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
String connectionString = ConfigurationSettings.AppSettings["dbconn"].ToString();
System.Data.SqlClient.SqlConnection sqlConnection = new System.Data.SqlClient.SqlConnection(connectionString);
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
string queryString = "INSERT INTO [test] ([Title], [Loc], [contentType], [docLen], [docData]) VALUES (@Title, @Loc, @contentType, @docLen, @docData)";
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
System.Data.SqlClient.SqlCommand sqlCommand = new System.Data.SqlClient.SqlCommand(queryString, sqlConnection);
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
uploadDocName = uploadDocName.Substring(uploadDocName.LastIndexOf(@"\") + 1);
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
sqlCommand.Parameters.Add("@Title", System.Data.SqlDbType.NVarChar).Value = uploadDocName;
sqlCommand.Parameters.Add("@Loc", System.Data.SqlDbType.NVarChar).Value = loc;
sqlCommand.Parameters.Add("@contentType", System.Data.SqlDbType.VarChar).Value = contentType;
sqlCommand.Parameters.Add("@docLen", System.Data.SqlDbType.BigInt).Value = docBinaryData.Length;
sqlCommand.Parameters.Add("@docData", System.Data.SqlDbType.Image).Value = docBinaryData;
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
try
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
sqlConnection.Open();
sqlCommand.ExecuteNonQuery();
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
}
catch (Exception ex)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
Response.Write(ex.Message + "<HR>");
}
finally
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
sqlConnection.Close();
}
posted on
2007-01-08 21:42
Caviare
阅读(
251)
评论()
编辑
收藏
举报