将二进制数据从文本框插入到列
本文关键字:插入 文本 二进制 数据 | 更新日期: 2023-09-27 18:32:00
请有人告诉我如何在SQL数据库中插入文本机器人中的数据,但以二进制格式。这是我代码的一部分:
private void button1_Click(object sender, EventArgs e)
{
string connectionString = @"Data Source=" + File.ReadAllText("Server.ini") + ";" + "Initial Catalog=" + "lin2db" + ";" + "User ID=" + File.ReadAllText("User.ini") + ";" + "Password=" + File.ReadAllText("Password.ini");
using (SqlConnection connection = new SqlConnection(connectionString))
using (SqlCommand command = connection.CreateCommand())
{
command.CommandText = "INSERT INTO server (id, name, ip, inner_ip, ageLimit, pk_flag, kind, port, region) VALUES (@id, @name, @ip, @inner_ip, @ageLimit, @pk_flag, @kind, @port, @region)";
command.Parameters.AddWithValue("@id", textBox1.Text);
command.Parameters.AddWithValue("@name", textBox2.Text);
command.Parameters.AddWithValue("@ip", textBox3.Text);
command.Parameters.AddWithValue("@inner_ip", textBox4.Text);
command.Parameters.AddWithValue("@ageLimit", textBox5.Text);
command.Parameters.AddWithValue("@pk_flag", textBox6.Text);
command.Parameters.AddWithValue("@kind", textBox7.Text);
command.Parameters.AddWithValue("@port", textBox8.Text);
command.Parameters.AddWithValue("@region", textBox9.Text);
connection.Open();
command.ExecuteNonQuery();
}
在该
文本框的ONBLUR上将该文本更改为二进制格式
在代码的帮助下
String s = "foo";
byte[] bytes = s.getBytes();
StringBuilder binary = new StringBuilder();
for (byte b : bytes)
{
int val = b;
for (int i = 0; i < 8; i++)
{
binary.append((val & 128) == 0 ? 0 : 1);
val <<= 1;
}
binary.append(' ');
}
System.out.println("'" + s + "' to binary: " + binary);
然后将该二进制数据插入到列中
已解决:我在SQL查询上使用,更简单: command.CommandText = "INSERT INTO server (id, name, ip, inner_ip, ageLimit, pk_flag, kind, port, region) VALUES (CONVERT(binary,@id), @name, @ip, @inner_ip, @ageLimit, @pk_flag, @kind, @port, @region)";