-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTestSql.txt
106 lines (70 loc) · 2.74 KB
/
TestSql.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Hardware {
class HardDisk {
static private string GetConnectionString()
{
// To avoid storing the connection string in your code,
// you can retrieve it from a configuration file, using the
// System.Configuration.ConfigurationSettings.AppSettings property
return "Data Source=(local);Initial Catalog=OperationSystem;"
+ "Integrated Security=SSPI;";
}
public static SqlConnection connection = new SqlConnection(GetConnectionString());
private static void Open() { connection.Open(); }
public static void Close() { connection.Close(); }
private static bool isOpen = false;
public static void Write(string com) {
if (!isOpen) { Open(); isOpen = true; }
SqlCommand command = new SqlCommand(com, connection);
command.ExecuteScalar();
}
public static void read(ref byte[] c,int offset) {
}
public static byte[] Read(string com) {
if (!isOpen) { Open(); isOpen = true; }
SqlCommand command = new SqlCommand(com, connection);
SqlDataReader result = command.ExecuteReader();
command.Cancel();
result.Read();
return GetBytes(result, 1);
}
private static byte[] GetBytes(SqlDataReader reader, int ordinal)
{
byte[] result = null;
if (!reader.IsDBNull(ordinal))
{
long size = reader.GetBytes(ordinal, 0, null, 0, 0); //get the length of data
result = new byte[size];
int bufferSize = 1024;
long bytesRead = 0;
int curPos = 0;
while (bytesRead < size)
{
bytesRead += reader.GetBytes(ordinal, curPos, result, curPos, bufferSize);
curPos += bufferSize;
}
}
return result;
}
}
}
namespace SqlConnect
{
class Program
{
static void Main(string[] args)
{
//Hardware.HardDisk.Write("insert into SuperBlock values(5, 101, 500, 18)");
byte[] b = Hardware.HardDisk.Read("select * from SuperBlock");
for (int i = 0; i < b.Length; i++) Console.WriteLine(b[i]);
string s = System.Text.Encoding.ASCII.GetString(b);
Console.WriteLine(s);
Hardware.HardDisk.Close();
}
}
}