.net大字段操作

超長文本存儲  

 SqlCommand myCommand;
   myCommand.CommandText = "INSERT table (Field1,Field2...)VALUES(@Field1,@Field2)";
   SqlParameter myParameter = new SqlParameter("@Field1", SqlDbType.Text);
   myCommand.Parameters.Add(myParamArray);
   SqlParameter myParameter = new SqlParameter("@Field2", SqlDbType.Text);
   myCommand.Parameters.Add(myParamArray);
   myCommand.ExecuteNonQuery;

 

 

圖片存儲

void Button_Submit(System.Object sender, System.EventArgs e)
   {
     // HttpPostedFile對象,用於讀取圖象文件屬性
     HttpPostedFile UpFile = UP_FILE.PostedFile;

     // FileLength 變量存儲圖片的字節大小
     int FileLength = UpFile.ContentLength;

     try
     {
    if (FileLength == 0)
    {
     txtMessage.Text = "<b>您未選擇上傳的文件</b>";
    }
    else
    {
     // 創建存儲圖片文件的臨時 Byte 數組
     Byte[] FileByteArray = new Byte[FileLength];

     // 建立數據流對象
     Stream StreamObject = UpFile.InputStream; 
   
     // 讀取圖象文件數據,FileByteArray爲數據儲存體,0爲數據指針位置、FileLnegth爲數據長度
     StreamObject.Read(FileByteArray,0,FileLength);  

     // 數據庫操作
     string ConnStr = System.Configuration.ConfigurationSettings.AppSettings["ConnectionSqlServer"];
     string query = "INSERT INTO ImageTable (ImageData, ImageContentType, ImageDescription, ImageSize) VALUES (@ImageData, @ImageContentType, @ImageDescription, @ImageSize)";
     SqlCommand myCommand = new SqlCommand(query, new SqlConnection(ConnStr));

     // 添加各項參數並賦值
     myCommand.Parameters.Add("@ImageData", SqlDbType.Image);
     myCommand.Parameters.Add("@ImageContentType", SqlDbType.VarChar, 50);
     myCommand.Parameters.Add("@ImageDescription", SqlDbType.VarChar, 200);
     myCommand.Parameters.Add("@ImageSize", SqlDbType.BigInt);
     myCommand.Parameters["@ImageData"].Value = FileByteArray;
     myCommand.Parameters["@ImageContentType"].Value = UpFile.ContentType;
     myCommand.Parameters["@ImageDescription"].Value = txtDescription.Text;
     myCommand.Parameters["@ImageSize"].Value = FileLength;
     
     // 執行數據庫操作
     myCommand.Connection.Open();
     myCommand.ExecuteNonQuery();
     myCommand.Connection.Close();

     // 提示上傳成功
     txtMessage.Text = "<b>上傳文件成功</b>";
    }
   }
   catch (Exception ex)
   {
    // 使用 Label 標籤顯示異常
    txtMessage.Text = ex.Message.ToString();
   }
  }

發佈了36 篇原創文章 · 獲贊 0 · 訪問量 10萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章