【问题标题】:i want to convert from int64 to byte我想从 int64 转换为字节
【发布时间】:2023-03-30 10:10:02
【问题描述】:

无法将参数值从 int32 转换为字节 数据库代码

数据库中的过程是:

后面的webform代码:

client c = new client();
c.name = Textbox_Addclient_first.Text + " " + Textbox_Addclient_second.Text + 
    " " + Textbox_Addclient_third.Text;
c.address = Textbox_addclientadress.Text;
c.phone = int.Parse(Textbox_addclientphone.Text);

类代码为:

SqlCommand com = new SqlCommand("add_client", con);

com.CommandType = CommandType.StoredProcedure;
com.Parameters.Add("@name", SqlDbType.NVarChar).Value = this.name;
com.Parameters.Add("@address", SqlDbType.NVarChar).Value = this.address;
com.Parameters.Add("@name", SqlDbType.TinyInt).Value = this.phone;
com.ExecuteNonQuery();
con.Close();

b = true;

【问题讨论】:

  • 我认为您无法将电话号码转换为字节......也许是字节数组。为什么不更改数据库中的类型呢?见:stackoverflow.com/questions/75105/…
  • 除了@Rufus L 的评论(这很棒,一个字节存储从 0 到 255,这对于北美区号来说甚至都不够大,更不用说完整的电话号码了),电话号码永远不应该以数字类型存储。如果您不需要对其进行数学运算,请不要将其存储为数字。从长远来看,它只会给你带来悲伤。

标签: c# asp.net sql-server-2008 ado.net


【解决方案1】:

这一行:

com.Parameters.Add("@name", SqlDbType.TinyInt).Value = this.phone;
  1. 您正在尝试将电话对象传递给名称参数。
  2. 您使用的是 tinyint,varchar(10) 会是更好的选择,但如果您坚持使用整数,int64 就足够了。
  3. 如果您使用 int 确保仅传递数字,请使用“[0-9]{10}”对字符串进行正则表达式检查以查找数字

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-11-27
    • 2019-10-19
    • 2021-04-24
    • 1970-01-01
    • 1970-01-01
    • 2015-07-12
    • 1970-01-01
    • 2021-09-22
    相关资源
    最近更新 更多