c# 新手:如何再sql server express的表中的某一列存放image数据? 如,我的d:\1.jpg,如何存放到表的某一列?我希望先用sql server mse实现;如果有可能,再用语句实现。 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 这个用语句好像不行用SqlCommand,把文件读成byte[],赋给SqlCommand 该示例按块从提供的文件路径中检索员工照片。 它根据指定的缓冲区大小,将每个块读入一个字节数组。 然后将该字节数组设置为 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(); }} 关于注释标识符问题 跪求一个tcp连接的代码 用AxWebBrowser怎么实现这个效果? 这个软件如何更新? 问两个关于DataSet的问题:字段局部唯一性和局部自动编号??? ListBox当鼠标移到某一项时,如何自动选中 怎样监视本地文件是否被其他机器访问 DataGridColumnStyle类中哪个事件是在keyup是发生? 枚举变量用foreach怎么写? 98 下的打印问题,急,解决了,马上结帖 谁知道楚广明c#简明教程在什么地方可以下到 在线等!!怎样自动启动COM+
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();
}
}