SQL 存取二进制数据

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;
using System.Text;
using System.Timers;
using System.Runtime.InteropServices;
//二进制数据的存取
namespace Maticsoft.DBUtility
{

public partial class Default4 : System.Web.UI.Page
{
public struct fix
{
public float[] k;
};
protected void Page_Load(object sender, EventArgs e)
{

}
protected void Button1_Click(object sender, EventArgs e)
{
fix fx = new fix();
fx.k = new float[5];
TextBox[] TX = { TextBox1, TextBox2, TextBox3, TextBox4, TextBox5 };
for (int i = 0; i < 5; i++)
{

fx.k[i] = ConvertBS(float.Parse(TX[i].Text.Trim()));
// float k = float.Parse(TX[i].Text.Trim());
}
byte[] fixparam = StructToBytes(fx);

if (Exists(1))
{
Update(1, fixparam);
}
else
{
add(1, fixparam);
}

}
public static float ConvertBS(float f)
{
byte[] temp = StructToBytes(f);
Array.Reverse(temp);
return (float)BytesToStuct(temp, typeof(float));
}
public static byte[] StructToBytes(object structObj)
{
//得到结构体的大小
int size = Marshal.SizeOf(structObj);
//创建byte数组
byte[] bytes = new byte[size];
//分配结构体大小的内存空间
IntPtr structPtr = Marshal.AllocHGlobal(size);
//将结构体拷到分配好的内存空间
Marshal.StructureToPtr(structObj, structPtr, false);
//从内存空间拷到byte数组
Marshal.Copy(structPtr, bytes, 0, size);
//释放内存空间
Marshal.FreeHGlobal(structPtr);
//返回byte数组
return bytes;
}

/// <summary>
/// byte数组转结构体
/// </summary>
/// <param name="bytes">byte数组</param>
/// <param name="type">结构体类型</param>
/// <returns>转换后的结构体</returns>
public static object BytesToStuct(byte[] bytes, Type type)
{
if (bytes == null)
{
return null;
}

//得到结构体的大小
int size = Marshal.SizeOf(type);
//byte数组长度小于结构体的大小
if (size > bytes.Length)
{
//返回空
return null;
}
//分配结构体大小的内存空间
IntPtr structPtr = Marshal.AllocHGlobal(size);
//将byte数组拷到分配好的内存空间
Marshal.Copy(bytes, 0, structPtr, size);
//将内存空间转换为目标结构体
object obj = Marshal.PtrToStructure(structPtr, type);
//释放内存空间
Marshal.FreeHGlobal(structPtr);
//返回结构体
return obj;
}
public void add(int id, byte[] bt)
{
StringBuilder strSql = new StringBuilder();
strSql.Append("insert into fix(");
strSql.Append("ID,fix)");
strSql.Append(" values (");
strSql.Append("@id,@fix)");
SqlParameter[] parameters = {
new SqlParameter("@id", SqlDbType.Int,4),

new SqlParameter("@fix", SqlDbType.VarBinary,50),
};
parameters[0].Value = id;
parameters[1].Value = bt;

DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
}
public bool Exists(int DeviceID)
{
StringBuilder strSql = new StringBuilder();
strSql.Append("select count(1) from dbo.fix where id=@DeviceID ");
// strSql.Append(" where ID=@DeviceID ");
SqlParameter[] parameters = {
new SqlParameter("@DeviceID", SqlDbType.Int,4)};
parameters[0].Value = DeviceID;

return DbHelperSQL.Exists(strSql.ToString(), parameters);


}
public bool Update(int id,byte[] bt)
{
StringBuilder strSql = new StringBuilder();
strSql.Append("update fix set ");
strSql.Append("fix=@fix");

strSql.Append(" where ID=@DeviceID ");
SqlParameter[] parameters ={
new SqlParameter("@fix", SqlDbType.VarBinary,50),
new SqlParameter("@DeviceID", SqlDbType.Int,4)
};
parameters[0].Value = bt;
parameters[1].Value = id;

int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
if (rows > 0)
{
return true;
}
else
{
return false;
}
}
}
}

转载于:https://www.cnblogs.com/nocoding/archive/2012/09/18/2690083.html

猜你喜欢

转载自blog.csdn.net/weixin_33841722/article/details/94183393