c# Bytes读写

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ReadAndWriteBytesDemo
{
    class Program
    {
        static void Main(string[] args)
        {
            string _file = "d:\\test.dat";
            long data1 = 100;
            float data2 = 0.1F;
            byte[] _bytes;
            short _length;
            List<byte> _listBytes = new List<byte>();

            _length = 8;
            _bytes = BitConverter.GetBytes(_length);
            if(BitConverter.IsLittleEndian)
            {
                Array.Reverse(_bytes);
            }
            _listBytes.AddRange(_bytes);
            _bytes = BitConverter.GetBytes(data1);
            _listBytes.AddRange(_bytes);

            _length = 8;
            _bytes = BitConverter.GetBytes(_length);
            if (BitConverter.IsLittleEndian)
            {
                Array.Reverse(_bytes);
            }
            _listBytes.AddRange(_bytes);
            _bytes = BitConverter.GetBytes(data2);
            _listBytes.AddRange(_bytes);

            System.IO.File.WriteAllBytes(_file, _listBytes.ToArray()); //写

            if (System.IO.File.Exists(_file))
            {
                _bytes = System.IO.File.ReadAllBytes(_file);//读
                int _index = 0;
                short _realdatal = BitConverter.ToInt16(_bytes, 0);
                _index += 2;

                long _realData1 = BitConverter.ToInt64(_bytes, _index);
                _index += 8;

                _realdatal = BitConverter.ToInt16(_bytes, _index);
                _index += 2;

                float _realData2 = BitConverter.ToSingle(_bytes, _index);
                _index += 8;

                Console.WriteLine(_realData1);
                Console.WriteLine(_realData2);
            }
        }
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
你好!对于C#中的Access数据库操作,你可以使用ADO.NET来实现。以下是一个简单的示例代码,展示了如何连接到Access数据库、执行查询和插入操作: ```csharp using System; using System.Data.OleDb; namespace AccessReadWriteExample { class Program { static void Main(string[] args) { // 连接字符串 string connectionString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\path\to\your\database.accdb"; // 查询数据 using (OleDbConnection connection = new OleDbConnection(connectionString)) { connection.Open(); string query = "SELECT * FROM TableName"; OleDbCommand command = new OleDbCommand(query, connection); OleDbDataReader reader = command.ExecuteReader(); while (reader.Read()) { // 取数据 string columnName = reader["ColumnName"].ToString(); // 处理数据... } reader.Close(); } // 插入数据 using (OleDbConnection connection = new OleDbConnection(connectionString)) { connection.Open(); string insertQuery = "INSERT INTO TableName (ColumnName1, ColumnName2) VALUES (@Value1, @Value2)"; OleDbCommand insertCommand = new OleDbCommand(insertQuery, connection); insertCommand.Parameters.AddWithValue("@Value1", "SomeValue1"); insertCommand.Parameters.AddWithValue("@Value2", "SomeValue2"); int rowsAffected = insertCommand.ExecuteNonQuery(); Console.WriteLine("插入成功!受影响的行数:" + rowsAffected); } } } } ``` 请注意,上述示例中的连接字符串需要根据你的实际情况进行修改。同时,记得在项目中引用 `System.Data` 和 `System.Data.OleDb` 命名空间。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值