在数据库时候我设计了学生的分数为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 10 * into newtable FROM [oldtable] Where UserID=1 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 = 1; i < lines.Count(); i++) { string line = lines[i]; //注意文件里是按制表符来分割的,不是字符串 string[] strs = line.Split(' ');//‘ ’为制表符 int id = Convert.ToInt32(strs[0]); string studentId = strs[1]; string studentName = strs[2]; string category = strs[3]; string studentScore = strs[4]; 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(' ');//分析数据是以Tab键分割的,所以去除 控件.Text = str[0]; 控件2.Text = str[1]; }
--------------------------------------------------------------------
数据如何存到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=0;i<students.Length;i++) { Student student = new Student(); student =students[i]; string stName=student.StudentName; string tab=" "; 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 = 0; i < str.Length; i++) { str1 = str[i]; sw.WriteLine(str1); } sw.Close(); this.Close(); }