永发信息网

C#中怎么样将string类型转化为tinyint类型

答案:3  悬赏:40  手机版
解决时间 2021-03-17 06:40
  • 提问者网友:棒棒糖
  • 2021-03-16 21:58
C#中怎么样将string类型转化为tinyint类型
最佳答案
  • 五星知识达人网友:低音帝王
  • 2021-03-16 23:27
SQL SERVER中的tinyint类型为从 0 到 255 的整型数据,存储大小为 1 字节。
.NET中C#的byte关键字映射.NET的Byte 结构,表示一个 8 位无符号整数,Byte 值类型表示值介于 0 和 255 之间的无符号整数。
所以相当于C#中的byte类型,把string类型先转换成byte就可以当作tinyint使用。代码如下:
string str="123";
byte tin = Convert.ToByte(str);
//然后把tin直接赋值给tinyint类型字段
全部回答
  • 1楼网友:动情书生
  • 2021-03-17 01:16
using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Text; namespace baidu { class temp { public static void main() { string s = "25"; s.ToTinyInt().show(); } } public static class MyExtensions { public static string sort(this string temp) { return temp.convert2int().sort().convert2chararray().CharToString(); } public static string CharToString(this char[] temp) { string s = ""; for (int i = 0; i < temp.Length; i++) { s += temp[i].ToString(); } return s; } public static char[] convert2chararray(this int[] temp) { List ls = new List(); for (int i = 0; i < temp.Length; i++) { ls.Add((char)temp[i]); } return ls.ToArray(); } public static int[] sort(this int[] temp) { List ls; ls = temp.ToList(); ls.Sort(); return ls.ToArray(); } public static int[] convert2int(this string temp) { List ls = new List(); for (int i = 0; i < temp.Length; i++) { ls.Add((int)temp[i]); } return ls.ToArray(); } public static byte ToTinyInt(this string temp) { return Convert.ToByte(temp); } public static void show(this byte temp) { Console.WriteLine(temp); wline(); } public static int[,] init(this int[,] temp) { Random rm = new Random(); int rank = temp.Rank; int line = temp.GetLength(0); int row = temp.Length / line; for (int j = 0; j < line; j++) { for (int i = 0; i < row; i++) { temp[j, i] = rm.Next(9); } } return temp; } public static string init(this string s,int lenght) { string temp = ""; Random rm = new Random(); for (int i = 0; i < lenght; i++) { temp += ((char)rm.Next(65, 90)).ToString(); } return temp; } public static int[] init(this int[] temp) { Random rm = new Random(); for (int i = 0; i < temp.Length; i++) { temp[i] = rm.Next(10000); } return temp; } #region public static void show(this string temp) { Console.WriteLine(temp); wline(); } public static void show(this int[,] temp) { int rank = temp.Rank; int line = temp.GetLength(0); int row = temp.Length / line; for (int j = 0; j < line; j++) { for (int i = 0; i < row; i++) { Console.Write(temp[j, i] + " "); } Console.WriteLine(); } wline(); } public static void show(this int[] temp) { for (int i = 0; i < temp.Length; i++) { Console.Write(temp[i] + " "); } Console.WriteLine(); wline(); } #endregion public static void wline() { Console.WriteLine("-------------------------------------------------------------------"); } } }
  • 2楼网友:孤独入客枕
  • 2021-03-17 00:06
你好! sql server 2005里面的tinyint 长度为一个字节 相当于c#中的byte类型 其范围为0到255 所以转换过程为 string str="123"; byte byten=Convert.ToByte(str); 这样byte就和数据库里面的tinyint对应上了 如果对你有帮助,望采纳。
我要举报
如以上回答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
点此我要举报以上问答信息
大家都在看
推荐资讯