博客园 首页 新随笔 联系 订阅 管理
  2 Posts :: 1 Stories :: 0 Comments :: 8 Trackbacks

   Stream sDocStream;
   int docLen;
   string contentType;
   string uploadDocName;
   string loc = "test";

     sDocStream  = uploadfile.PostedFile.InputStream;
     docLen =  uploadfile.PostedFile.ContentLength;
     uploadDocName = uploadfile.PostedFile.FileName;

     byte[] docBinaryData=new byte[docLen];

     sDocStream.Read(docBinaryData, 0, docLen);
     contentType = "doc";
   
   String connectionString = ConfigurationSettings.AppSettings["dbconn"].ToString();
   System.Data.SqlClient.SqlConnection sqlConnection = new System.Data.SqlClient.SqlConnection(connectionString);

      string queryString = "INSERT INTO [test] ([Title], [Loc], [contentType], [docLen], [docData]) VALUES (@Title, @Loc, @contentType, @docLen, @docData)";
     
   System.Data.SqlClient.SqlCommand sqlCommand = new System.Data.SqlClient.SqlCommand(queryString, sqlConnection);


   uploadDocName = uploadDocName.Substring(uploadDocName.LastIndexOf(@"\")+1);

     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;

     try
     {
       sqlConnection.Open();
       sqlCommand.ExecuteNonQuery();

     }
     catch (Exception ex)
     {
       Response.Write(ex.Message + "<HR>");
     }
     finally
     {
       sqlConnection.Close();
     }

posted on 2004-04-13 16:20  oliverlu  阅读(...)  评论(...编辑  收藏