C#:向SqlServer数据库中插入imange类型

2023-07-29,,

using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text; namespace Common
{
public class ConvertData
{
/// <summary>
/// 构造函数
/// </summary>
public ConvertData()
{ } /// <summary>
/// 将byte[]数组转换为double[]数组
/// </summary>
/// <param name="b"></param>
/// <returns></returns>
public double[] BytesToDoubles(byte[] b)
{
// Initialize unmanged memory to hold the array.
int size = Marshal.SizeOf(b[0]) * b.Length; IntPtr pnt = Marshal.AllocHGlobal(size); try
{
// Copy the array to unmanaged memory.
Marshal.Copy(b, 0, pnt, b.Length); // Copy the unmanaged array back to another managed array.
double[] managedArray2 = new double[b.Length / 8]; Marshal.Copy(pnt, managedArray2, 0, b.Length / 8);
return managedArray2;
}
finally
{
// Free the unmanaged memory.
Marshal.FreeHGlobal(pnt);
} } /// <summary>
/// 获取内存中double[],并转换为String字符串
/// </summary>
/// <param name="b"></param>
/// <returns></returns>
public string DoublesToString(double[] doubleArray)
{
string values = ""; for (int i = 0; i < doubleArray.Length; i++)
{
values += doubleArray[i] + ",";
} return values;
} /// <summary>
/// 获取内存中double[],并转换为String字符串
/// </summary>
/// <param name="b"></param>
/// <returns></returns>
public string BytesToString(byte[] b)
{
double[] doubleArray = BytesToDoubles(b); return DoublesToString(doubleArray); } /// <summary>
/// 获取内存中double[],并转换为String字符串
/// </summary>
/// <param name="b"></param>
/// <returns></returns>
public string GetByteToDouble(byte[] b)
{
// Initialize unmanged memory to hold the array.
int size = Marshal.SizeOf(b[0]) * b.Length; IntPtr pnt = Marshal.AllocHGlobal(size);
string values = "";
try
{
// Copy the array to unmanaged memory.
Marshal.Copy(b, 0, pnt, b.Length); // Copy the unmanaged array back to another managed array.
double[] managedArray2 = new double[b.Length / 8]; Marshal.Copy(pnt, managedArray2, 0, b.Length / 8); for (int i = 0; i < managedArray2.Length; i++)
{
values += managedArray2[i] + ",";
}
}
finally
{
// Free the unmanaged memory.
Marshal.FreeHGlobal(pnt);
} return values;
} /// <summary>
/// 将double[]数组转换为byte[]数组
/// </summary>
/// <param name="d"></param>
/// <returns></returns>
public byte[] DoublesToBytes(double[] d)
{
int size = Marshal.SizeOf(d[0]) * d.Length;
IntPtr pnt = Marshal.AllocHGlobal(size);
try
{
// Copy the array to unmanaged memory.
Marshal.Copy(d, 0, pnt, d.Length); // Copy the unmanaged array back to another managed array.
byte[] managedArray2 = new byte[d.Length * 8]; Marshal.Copy(pnt, managedArray2, 0, d.Length * 8);
return managedArray2;
}
finally
{
// Free the unmanaged memory.
Marshal.FreeHGlobal(pnt);
}
} }
}

调用:

 public int InsertWSWSUH(string wscd, int uintv, int udrp, string unitnm, int usteps, double[] uhdata, double mfp,/*datetime,*/int nsteps) //// <param name="datetime">时间戳</param>
{ string sqlStr = "Insert into [HyModelDB_new].[dbo].[FFM_WSWSUH_B] ([WSCD],[UINTV],[UDRP],[UNITNM] ,[USTEPS] ,[UHDATA],[MFP],[DATETM],[NSTEPS]) values ('" + wscd + "'," + uintv + "," + udrp + ",'" + unitnm + "'," + usteps + "," + "@unitData" + "," + mfp + "," + "@dateTime" + "," + nsteps + ")"; SqlParameter[] parameters = {
new SqlParameter("@unitData", SqlDbType.Image),
new SqlParameter("@dateTime", SqlDbType.DateTime)}; parameters[0].Value = ConvertData.DoublesToBytes(uhdata);
parameters[1].Value = DateTime.Now.ToString("yyyy-MM-dd");
return db.ExecuteSql(sqlStr);
}

参考代码:

/// <summary>
/// 增加一条数据
/// </summary>
public int Add(OA.Model.InformationOfDoc model)
{
StringBuilder strSql=new StringBuilder();
strSql.Append("insert into InformationOfDoc(");
strSql.Append("DocName,DocType,RwID,RWType,RWClass,Execution,DocBLOB,DocAttribute,CreateYMD,Remark)");
strSql.Append(" values (");
strSql.Append("@DocName,@DocType,@RwID,@RWType,@RWClass,@Execution,@DocBLOB,@DocAttribute,@CreateYMD,@Remark)");
strSql.Append(";select @@IDENTITY");
SqlParameter[] parameters = {
new SqlParameter("@DocName", SqlDbType.VarChar,40),
new SqlParameter("@DocType", SqlDbType.VarChar,100),
new SqlParameter("@RwID", SqlDbType.VarChar,18),
new SqlParameter("@RWType", SqlDbType.Char,10),
new SqlParameter("@RWClass", SqlDbType.Char,4),
new SqlParameter("@Execution", SqlDbType.Char,6),
new SqlParameter("@DocBLOB", SqlDbType.Image),
new SqlParameter("@DocAttribute", SqlDbType.Char,6),
new SqlParameter("@CreateYMD", SqlDbType.Date,3),
new SqlParameter("@Remark", SqlDbType.VarChar,200)};
parameters[0].Value = model.DocName;
parameters[1].Value = model.DocType;
parameters[2].Value = model.RwID;
parameters[3].Value = model.RWType;
parameters[4].Value = model.RWClass;
parameters[5].Value = model.Execution;
parameters[6].Value = model.DocBLOB;
parameters[7].Value = model.DocAttribute;
parameters[8].Value = model.CreateYMD;
parameters[9].Value = model.Remark; object obj = DbHelperSQL.GetSingle(strSql.ToString(),parameters);
if (obj == null)
{
return 0;
}
else
{
return Convert.ToInt32(obj);
}
}

C#:向SqlServer数据库中插入imange类型的相关教程结束。

《C#:向SqlServer数据库中插入imange类型.doc》

下载本文的Word格式文档,以方便收藏与打印。