C#操作SQL float类型数据,在C#中用double类型就OK了,C#数据类型和SQL数据类型对照
C#操作SQL Float类型,数据会多很多小数,原来是C#的float和sql的float类型不一致
/// <summary>
/// 数据库中与C#中的数据类型对照
/// </summary>
/// <param name="type"></param>
/// <returns></returns>
private string ChangeToCSharpType(string type)
{
string reval = string.Empty;
switch (type.ToLower())
{
case "int":
reval = "Int32";
break;
case "text":
reval = "String";
break;
case "bigint":
reval = "Int64";
break;
case "binary":
reval = "System.Byte[]";
break;
case "bit":
reval = "Boolean";
break;
case "char":
reval = "String";
break;
case "datetime":
reval = "System.DateTime";
break;
case "decimal":
reval = "System.Decimal";
break;
case "float":
reval = "System.Double";
break;
case "image":
reval = "System.Byte[]";
break;
case "money":
reval = "System.Decimal";
break;
case "nchar":
reval = "String";
break;
case "ntext":
reval = "String";
break;
case "numeric":
reval = "System.Decimal";
break;
case "nvarchar":
reval = "String";
break;
case "real":
reval = "System.Single";
break;
case "smalldatetime":
reval = "System.DateTime";
break;
case "smallint":
reval = "Int16";
break;
case "smallmoney":
reval = "System.Decimal";
break;
case "timestamp":
reval = "System.DateTime";
break;
case "tinyint":
reval = "System.Byte";
break;
case "uniqueidentifier":
reval = "System.Guid";
break;
case "varbinary":
reval = "System.Byte[]";
break;
case "varchar":
reval = "String";
break;
case "Variant":
reval = "Object";
break;
default:
reval = "String";
break;
}
return reval;
}
SQL Server类型 | C#类型 |
bit | bool |
tinyint | byte |
smallint | short |
int | int |
bigint | long |
real | float |
float | double |
money | decimal |
datetime | DateTime |
char | string |
varchar | string |
nchar | string |
nvarchar | string |
text | string |
ntext | string |
image | byte[] |
binary | byte[] |
uniqueidentifier | Guid |
数据库中字段类型对应C#中的数据类型:
| SQL SERVER类型 | | C#类型 |
精确数字 | bigint | 从 -2^63 (-9223372036854775808) 到 2^63-1 (9223372036854775807) 的整型数据(所有数字)。存储大小为 8 个字节。 | Int64 |
int | 从 -2^31 (-2,147,483,648) 到 2^31 - 1 (2,147,483,647) 的整型数据(所有数字)。存储大小为 4 个字节。int 的 SQL-92 同义字为integer。 | Int32 | |
smallint | 从 -2^15 (-32,768) 到 2^15 - 1 (32,767) 的整型数据。存储大小为 2 个字节。 | Int16 | |
tinyint | 从 0 到 255 的整型数据。存储大小为 1 字节。 | System.Byte | |
bit | 1 或 0 的整数数据。 | Boolean | |
decimal | 从 -10^38 +1 到 10^38 –1 的固定精度和小数位的数字数据。 | System.Decimal | |
numeric | 功能上等同于 decimal。 | System.Decimal | |
money | 货币数据值介于 -2^63 (-922,337,203,685,477.5808) 与 2^63 - 1 (+922,337,203,685,477.5807) 之间,精确到货币单位的千分之十。 | System.Decimal | |
smallmoney | 货币数据值介于 -214,748.3648 与 +214,748.3647 之间,精确到货币单位的千分之十。 | System.Decimal | |
近似数字 | float | 从 -1.79E + 308 到 1.79E + 308 的浮点精度数字。 | System.Double |
real | 从 -3.40E + 38 到 3.40E + 38 的浮点精度数字。 | System.Single | |
| datetime | 从 1753 年 1 月 1 日到 9999 年 12 月 31 日的日期和时间数据,精确到百分之三秒(或 3.33 毫秒)。 | System.DateTime |
smalldatetime | 从 1900 年 1 月 1 日到 2079 年 6 月 6 日的日期和时间数据,精确到分钟。 | System.DateTime | |
字符串 | char | 固定长度的非 Unicode 字符数据,最大长度为 8,000 个字符。 | String |
varchar | 可变长度的非 Unicode 数据,最长为 8,000 个字符。 | String | |
text | 可变长度的非 Unicode 数据,最大长度为 2^31 - 1 (2,147,483,647) 个字符。 | String | |
Unicode 字符串 | nchar | 固定长度的 Unicode 数据,最大长度为 4,000 个字符。 | String |
nvarchar | 可变长度 Unicode 数据,其最大长度为 4,000 字符。sysname 是系统提供用户定义的数据类型,在功能上等同于nvarchar(128),用于引用数据库对象名。 | String | |
ntext | 可变长度 Unicode 数据,其最大长度为 2^30 - 1 (1,073,741,823) 个字符。 | String | |
二进制字符串 | binary | 固定长度的二进制数据,其最大长度为 8,000 个字节。 | System.Byte[] |
varbinary | 可变长度的二进制数据,其最大长度为 8,000 个字节。 | System.Byte[] | |
image | 可变长度的二进制数据,其最大长度为 2^31 - 1 (2,147,483,647) 个字节。 | System.Byte[] | |
其它数据类型 | timestamp | 数据库范围的唯一数字,每次更新行时也进行更新。 | System.DateTime |
uniqueidentifier | 全局唯一标识符 (GUID)。 | System.Guid | |
Variant | | Object |
1.合并DataTable
遇到的问题:
需要选择两个不同数据库的不同表中的记录重新组合成一个新的集合。
解决方法:
将两个DataTable合并成为一个DataTable
取得两个DataTable某些列,按照某种选择条件重新组合成为一个新的DataTable
这个很类似于”select * from 表1,表2 where 表1.列名= 表2.列名”
示例代码
/// <summary>合并dt1和dt2的数据</summary>
/// <returns>获得合并之后的新DataTable---newTable</returns>
private static DataTable CombineTable(DataTable dt1, DataTable dt2)
{
//创建新的DataTable
DataTable newTable = new DataTable();
//为newTable添加新列
newTable.Columns.Add("StudentID", Type.GetType("System.String"), " ");
newTable.Columns.Add("StudentCode", Type.GetType("System.String"), " ");
newTable.Columns.Add("StudentName", Type.GetType("System.String"), " ");
newTable.Columns.Add("Indexing", Type.GetType("System.Int64"), " ");
newTable.Columns.Add("ClassID", Type.GetType("System.String"), " ");
newTable.Columns.Add("ClassCode", Type.GetType("System.String"), " ");
newTable.Columns.Add("ClassName", Type.GetType("System.String"), " ");
newTable.Merge(dt1); //将dt1合并到newTable
foreach (DataRow newrow in newTable.Rows) //遍历newTable全部列
{
foreach (DataRow dr2 in dt2.Rows) //遍历dt2全部列
{
if (newrow["StudentID"].ToString() == dr2["StudentID"].ToString()) //当newTable的StudentID和dt2的StudentID相同时,执行下列操作
{
//将dtStudentExecutiveClassLink对应列的信息newTable对应列中
newrow["StudentCode"] = dr2["StudentCode"];
newrow["StudentName"] = dr2["StudentName"];
newrow["ClassID"] = dr2["ClassID"];
newrow["ClassCode"] = dr2["ClassCode"];
newrow["EClassName"] = dr2["ClassName"];
}
}
}
return newTable; //返回结果
}