在数据库时候我设计了学生的分数为nvarchar(50),是为了在从TXT文件中读取数据插入到数据库表时候方便,但是在后期由于涉及到统计问题,比如求平均值等,需要int类型才可以,方法是:Convert(int,字段名)。例如:select avg(Convert(int,M_Score)) from temp

建立视图,将视图当表示用

CREATE VIEW temp AS
select StudentId, MAX(StudentScore) as M_Score
from T_StudentScores group by StudentId;

----------------------------------------------------------

建立两个表之间的关系,内部连接

select * from T_StudentScores inner join  temp
on temp.StudentId=T_StudentScores.StudentId and T_StudentScores.StudentScore=temp.M_Score

-----------------------------------------------------------------------

将查询结果放入新表中的语句参考:

SELECT TOP  * into newtable FROM [oldtable] Where UserID= Order By CreateTime Desc;
select * into newTable from temp

-------------------------------------------------------------------------------------

TxT文件数据导入数据库:

 private void btnImport_Click(object sender, RoutedEventArgs e)
{
string connStr = ConfigurationManager.ConnectionStrings["dbStudentScoreStr"].ConnectionString;
OpenFileDialog ofd = new OpenFileDialog();
ofd.Filter = "文本文件|*.txt";
if (ofd.ShowDialog() == false)
{
return;
}
string[] lines = File.ReadLines(ofd.FileName, Encoding.Default).ToArray(); DataTable table = new DataTable();
table.Columns.Add("Id");
table.Columns.Add("StudentId");
table.Columns.Add("StudentName");
table.Columns.Add("Category");
table.Columns.Add("StudentScore");
//查看文件的编码格式,可以在文件->另存为->编码中查看,若是ANSI使用参数DEFAULT编码即可,
//若是UTF-8不使用参数,或是指定UTF-8即可
//使用如下的方式将其变成string数组 for (int i = ; i < lines.Count(); i++)
{
string line = lines[i];
//注意文件里是按制表符来分割的,不是字符串
string[] strs = line.Split('\t');//‘\t’为制表符
int id = Convert.ToInt32(strs[]);
string studentId = strs[];
string studentName = strs[];
string category = strs[];
string studentScore = strs[]; DataRow row = table.NewRow();//创建一个DataRow对象
row["Id"] = id;//一定要在一开始创建table.Columns添加列
row["StudentId"] = studentId;
row["StudentName"] = studentName;
row["Category"] = category;
row["StudentScore"] = studentScore;
table.Rows.Add(row);//NewRow只是创建,没有插入
}
using (SqlBulkCopy bulkCope = new SqlBulkCopy(connStr))
{
bulkCope.DestinationTableName = "T_StudentScores";
//添加dataTable中列名与数据库表中列名的映射
bulkCope.ColumnMappings.Add("Id", "Id");
bulkCope.ColumnMappings.Add("StudentId", "StudentId");
bulkCope.ColumnMappings.Add("StudentName", "StudentName");//dataTable中与数据库表的对应关系(datatable中的名字,数据库中的名字)
bulkCope.ColumnMappings.Add("Category", "Category");
bulkCope.ColumnMappings.Add("StudentScore", "StudentScore");
bulkCope.WriteToServer(table);
} MessageBox.Show("导入成功");
}

------------------------------------------------------------------------

TXT文本数据直接读取到控件上:

string[] lines = File.ReadAllLines("C:/Documents and Settings/Administrator/桌面/12.txt");  //读取所有行的代码
foreach (var line in lines)
{
string[] str = line.Split('\t');//分析数据是以Tab键分割的,所以去除
控件.Text = str[];
控件2.Text = str[];
}

--------------------------------------------------------------------

数据如何存到TXT文件中:如果控件的数据是Datagrid中的,且是以对象的方式显示,在保存时候用Tab键分割。

private void btnOutput_Click(object sender, RoutedEventArgs e)
{
string str;
StudentDAL dal = new StudentDAL();
Student[] students = dal.ListStudentMaxScore();
StreamWriter sw = new StreamWriter(@"C:/Documents and Settings/Administrator/桌面/3.txt", false, Encoding.Default); for(int i=;i<students.Length;i++)
{
Student student = new Student();
student =students[i];
string stName=student.StudentName;
string tab="\t";
string stScort=student.StudentScore;
str = strcat(strcat(stName, tab), stScort); sw.WriteLine(str); }
sw.Close();
MessageBox.Show("成功导出!"); }

-------------------------------------------------------------

控件中的数据导出到TXT格式:

 public void Save_Quit()
{
string[] str = { txtMinX.Text, txtMaxX.Text, txtMaxY.Text, txtMinY.Text };
StreamWriter sw = new StreamWriter(@"C:/Documents and Settings/Administrator/桌面/2.txt", false, Encoding.Default);
string str1;
for (int i = ; i < str.Length; i++)
{
str1 = str[i];
sw.WriteLine(str1); }
sw.Close();
this.Close(); }

SQL C# nvarchar类型转换为int类型 多表查询的问题,查询结果到新表,TXT数据读取到控件和数据库,生成在控件中的数据如何存到TXT文件中的更多相关文章

  1. Java进阶(二十三)java中long类型转换为int类型

    java中long类型转换为int类型 由int类型转换为long类型是向上转换,可以直接进行隐式转换,但由long类型转换为int类型是向下转换,可能会出现数据溢出情况: 主要以下几种转换方法,供参 ...

  2. java中long类型转换为int类型

    由int类型转换为long类型是向上转换,可以直接进行隐式转换,但由long类型转换为int类型是向下转换,可能会出现数据溢出情况: 主要以下几种转换方法,供参考: 一.强制类型转换 [java] l ...

  3. pandas把'<m8[ns]'类型转换为int类型进行运算

    工作中经常碰到两列数据为date类型,当这两列数据相减或者相加时,得到天数,当运用这个值进行运算会报错:ufunc true_divide cannot use operands with types ...

  4. double类型转换为int类型四舍五入工具类

    package com.qiyuan.util; import java.math.BigDecimal; import java.text.DecimalFormat; public class G ...

  5. SQL 对decimal类型转换为int类型

    ) AS INT) CountQty select ISNULL( CAST(E.Qty AS INT),0 )  FROM  OrderDetail E 空值 需要默认为0 即可

  6. 34 char类型转换为int类型

    #include<iostream> #include<cstdlib > using namespace std; int main() { char a=101; int ...

  7. C++中的string类型转换为int类型

    给定一个十进制整数n,输出n的各位数字之和 #include<iostream> #include<string> using namespace std; int main( ...

  8. lgp20151222 java中如何将Object类型转换为int类型

    if (object instanceof Integer) {    Integer.parseInt(object.toString()); } 很简单是不是?我就想提醒下自己,java有个特殊词 ...

  9. 随性练习:excel中文字和链接存到html文件

    这是一个简单的练习,主要是将excel中文字和链接存到html文件中,并且可通过点击文字直通链接 excel格式如下图示,我这里得excel是07版的,所以用到xlrd模块 代码: import xl ...

随机推荐

  1. java 邮件

      使用java语言实现邮件的简单的发送和接受. 说明:使用Java应用程序发送E-mail比较简单,在使用下列程序之前,你需要将mail.jar和activation.jar 添加到你的CLASSP ...

  2. js基础和工具库

    /* * 作者: 胡乐 * 2015/4/18 * js 基础 和 工具库 * * * */ //根据获取对象 function hGetId(id){ return document.getElem ...

  3. 无论url请求什么.都可以拼接class类名.实例化.传递get参数-->给当前控制器-->传递给抽象父类-->都交给抽象父类.这个方法去处理call_user_func_array()

    <?phpdefine('DS','/');define('A_PATH',str_replace('\\','/',dirname(__FILE__)).DS); //01获取到主程序目录cl ...

  4. LeetCode OJ 118. Pascal's Triangle

    Given numRows, generate the first numRows of Pascal's triangle. For example, given numRows = 5,Retur ...

  5. 在Linux系统如何让程序开机时自动启动

    在Linux系统如何让程序开机时自动启动      核心提示:系统的服务在开机时一般都可以自动启动,那在linux系统下如果想要程序在开机时自动启动怎么办?我们知道在 windows系统“开始”--& ...

  6. Oracle 游标及存储过程实例

    /*********实例一*********/ create or replace procedure users_procedure is cursor users_cursor is select ...

  7. Inno Setup入门(四)——为程序创建桌面快捷方式

    Icons这一可选段定义所有创建在开始菜单和\或其它位置 (比如桌面) 的快捷方式.一个例子如下: [setup] ;全局设置,本段必须 AppName=Test AppVerName=TEST De ...

  8. Java学习笔记之接口和抽象类

    接口(interface)1.interface创建一个接口,implements实现接口 interface jiekou{} class lie implements jiekou{}2.接口可以 ...

  9. 【匈牙利算法】 二分图模板 poj 1274

    #include <iostream> #include <cstdio> #include <memory.h> using namespace std; int ...

  10. 用ifconfig命令,只有lo,没有eth0的解决方案

    解决方案: 1. 进入/etc/sysconfig/network-scripts 目录,发现有ifcfg-eth0,即网卡(驱动)存在但未启用. 2. 输入ifconfig -a命令,可显示eth0 ...