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#中的数据类型:
|
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; //返回结果
}