如,我的d:\1.jpg,如何存放到表的某一列?我希望先用sql server mse实现;如果有可能,再用语句实现。

解决方案 »

  1.   

    这个用语句好像不行用SqlCommand,把文件读成byte[],赋给SqlCommand
      

  2.   

    该示例按块从提供的文件路径中检索员工照片。 它根据指定的缓冲区大小,将每个块读入一个字节数组。 然后将该字节数组设置为 SqlCommand 的 @Bytes 输入参数的值。 更新 @Offset 参数值并执行 SqlCommand,它会向该员工记录的 Photo 字段追加当前的字节块区。
    using System;
    using System.Data;
    using System.Data.SqlClient;
    using System.IO;public class EmployeeData
    {
        public static void Main()
        {
            DateTime hireDate = DateTime.Parse("4/27/98");
            int newID = AddEmployee("Smith", "John", "Sales Representative",
                hireDate, 5, "smith.bmp");
            Console.WriteLine("New Employee added. EmployeeID = " + newID);
        }    public static int AddEmployee(string lastName, string firstName,
            string title, DateTime hireDate, int reportsTo, string photoFilePath)
        {
            using (SqlConnection connection = new SqlConnection(
                "Data Source=(local);Integrated Security=true;Initial Catalog=Northwind;"))
            {            SqlCommand addEmp = new SqlCommand(
                    "INSERT INTO Employees (LastName, FirstName, Title, HireDate, ReportsTo, Photo) " +
                    "Values(@LastName, @FirstName, @Title, @HireDate, @ReportsTo, 0x0);" +
                    "SELECT @Identity = SCOPE_IDENTITY();" +
                    "SELECT @Pointer = TEXTPTR(Photo) FROM Employees WHERE EmployeeID = @Identity", 
                    connection);            addEmp.Parameters.Add("@LastName", SqlDbType.NVarChar, 20).Value = lastName;
                addEmp.Parameters.Add("@FirstName", SqlDbType.NVarChar, 10).Value = firstName;
                addEmp.Parameters.Add("@Title", SqlDbType.NVarChar, 30).Value = title;
                addEmp.Parameters.Add("@HireDate", SqlDbType.DateTime).Value = hireDate;
                addEmp.Parameters.Add("@ReportsTo", SqlDbType.Int).Value = reportsTo;            SqlParameter idParm = addEmp.Parameters.Add("@Identity", SqlDbType.Int);
                idParm.Direction = ParameterDirection.Output;
                SqlParameter ptrParm = addEmp.Parameters.Add("@Pointer", SqlDbType.Binary, 16);
                ptrParm.Direction = ParameterDirection.Output;            connection.Open();            addEmp.ExecuteNonQuery();            int newEmpID = (int)idParm.Value;            StorePhoto(photoFilePath, (byte[])ptrParm.Value, connection);            return newEmpID;
            }
        }    public static void StorePhoto(string fileName, byte[] pointer, 
            SqlConnection connection)
        {
            // The size of the "chunks" of the image.
            int bufferLen = 128;          SqlCommand appendToPhoto = new SqlCommand(
                "UPDATETEXT Employees.Photo @Pointer @Offset 0 @Bytes", 
                connection);        SqlParameter ptrParm = appendToPhoto.Parameters.Add(
                "@Pointer", SqlDbType.Binary, 16);
            ptrParm.Value = pointer;
            SqlParameter photoParm = appendToPhoto.Parameters.Add(
                "@Bytes", SqlDbType.Image, bufferLen);
            SqlParameter offsetParm = appendToPhoto.Parameters.Add(
                "@Offset", SqlDbType.Int);
            offsetParm.Value = 0;        // Read the image in and write it to the database 128 (bufferLen) bytes at a time.
            // Tune bufferLen for best performance. Larger values write faster, but
            // use more system resources.
            FileStream fs = new FileStream(fileName, FileMode.Open, FileAccess.Read);
            BinaryReader br = new BinaryReader(fs);        byte[] buffer = br.ReadBytes(bufferLen);
            int offset_ctr = 0;        while (buffer.Length > 0)
            {
                photoParm.Value = buffer;
                appendToPhoto.ExecuteNonQuery();
                offset_ctr += bufferLen;
                offsetParm.Value = offset_ctr;
                buffer = br.ReadBytes(bufferLen);
            }        br.Close();
            fs.Close();
        }
    }