الفريق العربي للبرمجةأرشيف المنتديات · 2000 – 2023
نسخة أرشيفية للقراءة فقط — التسجيل والمشاركة مغلقان، والمحتوى محفوظ كما كان.

تخزين الصور داخل sqL server

مغلق
بدأه AbuZakaria في 13 يوليو 2005 · 6 رد · 1,012 مشاهدة · في Microsoft Visual C#.NET
مشاركة: واتساب X فيسبوك تيليجرام
#1 صاحب الموضوع

كيف يمكن ربط field من نوع image الموجود في الSQL serverمع الimage object الموجود في الC# و اعمل ادخال و حذف و تعديل

#3
Save the file as SaveImage.vb

Imports System
Imports System.IO
Imports System.Data

Public Class SaveImage
  Shared Sub main()
  'Delaclare a file stream object 
  Dim o As System.IO.FileStream
  'Declare a stream reader object
  Dim r As StreamReader
  Dim jpgFile As String
  Console.Write("Enter a Valid .JPG file path")
  jpgFile = Console.ReadLine
  If Dir(jpgFile) = "" Then
   Console.Write("Invalid File Path")
   Exit Sub
  End If
  'Open the file 
  o = New FileStream(jpgFile, FileMode.Open, FileAccess.Read, FileShare.Read)
  'Read the output in a stream reader
  r = New StreamReader(o)
  Try
    'Declare an Byte array to save the content of the file to be saved
    Dim FileByteArray(o.Length - 1) As Byte
    o.Read(FileByteArray, 0, o.Length)
'Open the DataBase Connection, Please map the datasource name to match the 'Database path
    Dim Con As New System.Data.OleDb.OleDbConnection("Provider=Microsoft.Jet.OLEDB.3.51;Persist Security Info=False;Data Source=DbImages.mdb")
    Dim Sql As String = "INSERT INTO DbImages (Pic,FileSize) VALUES (?,?)"
    'Declare a OleDbCommand Object 
    Dim CmdObj As New System.Data.OleDb.OleDbCommand(Sql, Con)
    'Add the parameters
    CmdObj.Parameters.Add("@Pic", System.Data.OleDb.OleDbType.Binary, o.Length).Value = FileByteArray
   CmdObj.Parameters.Add("@FileSize", System.Data.OleDb.OleDbType.VarChar, 100).Value = o.Length
  Con.Open()
  CmdObj.ExecuteNonQuery()
  Con.Close()
  Catch ex As Exception
    Console.Write(ex.ToString)
  End Try
 End Sub
End Class
#4

هذا المثال يوضح لك ما تريد

لكن كي يعمل عليك إنشاء قاعدة بيانات إسمها pictures فيها جدول وحيد له نفس الإسم ويحتوي عمودين الأول إسمه file name ونوعه varchar طوله250 والثاني إسمه picture نوعه image وأيضا يجب أن تغير data source=alali في سلسلة الإتصال إلى إسم الجهاز لديك

my sql pic.rarمثالundefined

#5
insert into  pictures (picture,filename) values (@pic,@ff)

لية انا رميت أو حطيت الvalues بتاعت الصورة والاسم ب@pic,@ff

انا لما جيت اعمل كدة بيدى exception بسبب عدم تعرفة علىذلك المتغيرين ومحتاج منى تعريفهما؟؟؟؟

طريقة كتبتى للتعبير لا تختلف أنظر الى ذلك الكود وياريت لو فية اخطأ ممكن أى حد يصححها

SQLstring = "insert into Staff ";
    SQLstring += " '" + e.Code + "',";
    SQLstring += " '" + e.Name + "',";
    SQLstring +=   e.Degree+",";
    SQLstring += "'" + @pic + "'," +
    SQLstring += "'" + @ff + "')";

ثم بعد كدة أرمى الsqlString الى

//Global.Command.CommandText = SQLstring;
 	 Global.Command.ExecuteNonQuery();

ياريت الرد يكون بسرعة

وشكرا لكم

Muhammad Allam

Computer Science

@Resource(MappedURL="My Blog" )

#6

السلام على من اتبع الهدى ورحمة الله وبركاته

هذا الكود لحفظ الصورة بقاعدة البيانات

// Source Code for Save the image file into the database

public void OnUpload(Object sender, EventArgs e)
{
    // Create a byte[] from the input file
    int len = Upload.PostedFile.ContentLength;
    byte[] pic = new byte[len];
    Upload.PostedFile.InputStream.Read (pic, 0, len);
    // Insert the image and comment into the database
    SqlConnection connection = new 
      SqlConnection (@"server=INDIA\INDIA;database=iSense;uid=sa;pwd=india");
    try
    {
        connection.Open ();
        SqlCommand cmd = new SqlCommand ("insert into Image " 
          + "(Picture, Comment) values (@pic, @text)", connection);
        cmd.Parameters.Add ("@pic", pic);
        cmd.Parameters.Add ("@text", Comment.Text);
        cmd.ExecuteNonQuery ();
    }
    finally 
    {
        connection.Close ();
    }
}

وهذا الكود لقراءة الصورة من قاعدة البيانات

private void Page_Load(object sender, System.EventArgs e)
{
    // Put user code to initialize the page here
    MemoryStream stream = new MemoryStream ();
    SqlConnection connection = new 
      SqlConnection (@"server=INDIA\INDIA;database=iSense;uid=sa;pwd=india");
    try
    {
        connection.Open ();
        SqlCommand command = new 
          SqlCommand ("select Picture from Image", connection);
        byte[] image = (byte[]) command.ExecuteScalar ();   
        stream.Write (image, 0, image.Length);
        Bitmap bitmap = new Bitmap (stream);
        Response.ContentType = "image/gif";
        bitmap.Save (Response.OutputStream, ImageFormat.Gif);
    } 
    finally
    {
        connection.Close ();
        stream.Close ();
    }
}

Name : Ahmed H. Alawady

Web Site : Alawady.info

Email : alawady_ahmed@hotmail.com

Tel : +2 012 345 6808

#7

شكرا لك ياأخ أحمد جزاك الله كل خير

Muhammad Allam

Computer Science

@Resource(MappedURL="My Blog" )

هذا الموضوع مغلق.

مواضيع مشابهة