在数据库时候我设计了学生的分数为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. HDU 3499 Flight spfa+dp

    Flight Time Limit : 20000/10000ms (Java/Other)   Memory Limit : 65535/65535K (Java/Other) Total Subm ...

  2. 转:JDBC驱动配置相关

    1.做JDBC请求 ,首先要了解这个JDBC对象是什么,现在以SQLServer为例来说明 首先下载对应的数据库驱动(百度“jdbc sqlserver驱动”,然后下载). 注意 :下载完成后,直接把 ...

  3. LeetCode OJ 236. Lowest Common Ancestor of a Binary Tree

    Given a binary tree, find the lowest common ancestor (LCA) of two given nodes in the tree. According ...

  4. HDU2629:Identity Card

    Problem Description Do you own an ID card?You must have a identity card number in your family's Hous ...

  5. SharePoint 2010 Modal Dialog

    SharePoint 2010 Modal Dialog Tweet   Modal dialog play very important role to improve the user exper ...

  6. oc汉子转拼音

    oc中可以不使用第三方库直接吧数组转成拼音: 代码如下: NSString *str = @"中国abc人民共和国"; CFStringRef aCFString=(__bridg ...

  7. project文件问题

    到编译文件中看看,红色的删除,坤哥这样做的 .

  8. yum no key

    http://serverfault.com/questions/525958/redhat-yum-install-gpg-key-retrieval-failed

  9. hibernate5 中的schemaExport

    hibernate5中的schemaExport与之前版本中的用法有所不同,具体用法如下: ServiceRegistry serviceRegistry = new StandardServiceR ...

  10. Spring 读取XML配置文件的两种方式

    import org.springframework.context.ApplicationContext; import org.springframework.context.support.Cl ...