c# 字符串转换问题

l2539641 2008-12-03 12:39:03
c# 字符串转换遇到个问题我要把字符串转换成十六进制插入到SQL表中,该类型为binary;
SqlCommand commm = new SqlCommand("update [user] set data=0x三千多省略了 where account='"+labAcc.Text+"'", conn);
这样是可以的,比如字符串str="32F01023DF也是三千的长度"要把他UPDATE到数据库把我难倒了试了很多转换都不行这个那个的错误都有,请教高手帮帮忙吧 谢谢了
...全文
196 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
jasen520 2008-12-03
  • 打赏
  • 举报
回复
我刚测试过,2楼的方法就可以了!
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 gomoku 的回复:]
C# code
{
SqlCommand commm = new SqlCommand("update [user] set data=@data where account='"+labAcc.Text+"'", conn);

SqlParameter dataParam = new SqlParameter("data", SqlDbType.Binary); //<---
dataParam.Value = GetBytes("32F01023DF"); //<---
commm.Parameters.Add(dataParam); //<---

conn.Open();
com…
[/Quote]
学习!
birdlonger 2008-12-03
  • 打赏
  • 举报
回复
mark
hanyu0528 2008-12-03
  • 打赏
  • 举报
回复
学习啊~!!!
dlmeijianyu 2008-12-03
  • 打赏
  • 举报
回复
学习,

帮顶~
liujt09 2008-12-03
  • 打赏
  • 举报
回复
2楼的方法就可以了啊!
whitechololate 2008-12-03
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 gomoku 的回复:]
C# code
{
SqlCommand commm = new SqlCommand("update [user] set data=@data where account='"+labAcc.Text+"'", conn);

SqlParameter dataParam = new SqlParameter("data", SqlDbType.Binary); //<---
dataParam.Value = GetBytes("32F01023DF"); //<---
commm.Parameters.Add(dataParam); //<---

conn.Open();
com…
[/Quote]

学习,

帮顶~
TonyWu66 2008-12-03
  • 打赏
  • 举报
回复
你用Parameters试一下

public static void AddEmployee(
string lastName,
string firstName,
string title,
DateTime hireDate,
int reportsTo,
string photoFilePath,
string connectionString)
{
byte[] photo = GetPhoto(photoFilePath);

using (SqlConnection connection = new SqlConnection(
connectionString))

SqlCommand command = new SqlCommand(
"INSERT INTO Employees (LastName, FirstName, " +
"Title, HireDate, ReportsTo, Photo) " +
"Values(@LastName, @FirstName, @Title, " +
"@HireDate, @ReportsTo, @Photo)", connection);

command.Parameters.Add("@LastName",
SqlDbType.NVarChar, 20).Value = lastName;
command.Parameters.Add("@FirstName",
SqlDbType.NVarChar, 10).Value = firstName;
command.Parameters.Add("@Title",
SqlDbType.NVarChar, 30).Value = title;
command.Parameters.Add("@HireDate",
SqlDbType.DateTime).Value = hireDate;
command.Parameters.Add("@ReportsTo",
SqlDbType.Int).Value = reportsTo;

command.Parameters.Add("@Photo",
SqlDbType.Image, photo.Length).Value = photo;

connection.Open();
command.ExecuteNonQuery();
}
}

public static byte[] GetPhoto(string filePath)
{
FileStream stream = new FileStream(
filePath, FileMode.Open, FileAccess.Read);
BinaryReader reader = new BinaryReader(stream);

byte[] photo = reader.ReadBytes((int)stream.Length);

reader.Close();
stream.Close();

return photo;
}

yeah920 2008-12-03
  • 打赏
  • 举报
回复
帮忙up
gomoku 2008-12-03
  • 打赏
  • 举报
回复

{
SqlCommand commm = new SqlCommand("update [user] set data=@data where account='"+labAcc.Text+"'", conn);

SqlParameter dataParam = new SqlParameter("data", SqlDbType.Binary); //<---
dataParam.Value = GetBytes("32F01023DF"); //<---
commm.Parameters.Add(dataParam); //<---

conn.Open();
commm.ExecuteNonQuery();
conn.Close();
}

byte[] GetBytes(string hexString)
{
byte[] result = new byte[hexString.Length / 2];
for (int i = 0; i < result.Length; i++)
{
result[i] = Convert.ToByte(hexString.Substring(i + i, 2), 16);
}
return result;
}


It would be easier if you have a byte array at the beginning.
l2539641 2008-12-03
  • 打赏
  • 举报
回复
怎么都没人回答吗,简单的说就是把字符串update到数据类型为binary的字段中

111,130

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Creator Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧