C#任意上传下载byte到MSSQL

这篇博客展示了如何使用C#从MSSQL数据库读取和写入byte数据。通过创建SqlCommand对象,执行SQL命令进行数据的读取和更新。同时,文章还演示了如何读写文件中的byte数据,将double类型转换为byte数组,并在文件系统中进行存取。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Data.SqlClient;
  6. using System.Data;
  7. namespace ConsoleApplication1
  8. {
  9.     class Program
  10.     {
  11.         static void Main(string[] args)
  12.         {
  13.             SqlCommand cmd = new SqlCommand();
  14.             /*byte[] blob = new byte[16];
  15.             byte[] blob0, blob1;
  16.             double db0 = 7.5, db1 = 1.27;
  17.             blob0 = BitConverter.GetBytes(db0);
  18.             blob1 = BitConverter.GetBytes(db1);
  19.             blob0.CopyTo(blob, 0);
  20.             blob1.CopyTo(blob, 8);*/
  21.             double db0, db1;
  22.             byte[] blob0 = new byte[8];
  23.             byte[] blob1 = new byte[8];
  24.             using (SqlConnection conn = new SqlConnection("server=(local);database=database;uid=name;pwd=password"))
  25.             {
  26.                 /*cmd.CommandType = System.Data.CommandType.Text;
  27.                 cmd.CommandText = "update WMETER_LOG set ExamData = @examdatas where DeviceCode = '5070386'";   
  28.                 SqlParameter param = new SqlParameter("@examdatas", SqlDbType.Binary, blob.Length, ParameterDirection.Input, false, 0, 0, null, DataRowVersion.Current, blob);
  29.                 cmd.Parameters.Add(param);
  30.                 cmd.Connection = conn;  
  31.                 conn.Open();
  32.                 cmd.ExecuteNonQuery();*/
  33.                 cmd.CommandType = System.Data.CommandType.Text;
  34.                 cmd.CommandText = "select ExamData from WMETER_LOG where DeviceCode='5070386'";
  35.                 cmd.Connection = conn;
  36.                 conn.Open();
  37.                 SqlDataReader rdr = cmd.ExecuteReader();
  38.                 while (rdr.Read())
  39.                 {
  40.                     long val0 = rdr.GetBytes(0, 0, blob0, 0, 8);
  41.                     long val1 = rdr.GetBytes(0, 8, blob1, 0, 8);
  42.                 }
  43.             }
  44.             db0 = BitConverter.ToDouble(blob0, 0);
  45.             db1 = BitConverter.ToDouble(blob1, 0);
  46.             Console.Write(db1);
  47.             Console.ReadLine();
  48.         }
  49.     }
  50. }

读写文件

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Runtime.Serialization;
  6. using System.IO;
  7. namespace FileTest
  8. {
  9.     class Program
  10.     {
  11.         static void Main(string[] args)
  12.         {
  13.             double db;
  14.             FileInfo myFile = new FileInfo(@"D:/lihongdian.msd");
  15.             byte[] bt = new byte[8];
  16.             /*db = 7.5;
  17.             bt = BitConverter.GetBytes(db);
  18.             FileStream fs = myFile.OpenWrite();
  19.             fs.Write(bt, 0, 8);
  20.             fs.Close();*/
  21.             FileStream fs = myFile.OpenRead();
  22.             fs.Read(bt, 0, 8);
  23.             fs.Close();
  24.             db = BitConverter.ToDouble(bt, 0);
  25.             Console.Write(db);
  26.             Console.ReadLine();
  27.         }
  28.     }
  29. }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值