System.IO.StreamReader sr = new System.IO.StreamReader("c:/f.txt"); //伪代码 get Connection // DBTrans tran = Connection.BeginTran(); try{ while (!sr.EndOfStream) { string strReadLine = sr.ReadLine(); string[] strArray = strReadLine.Split(' '); //这里判断你想要的字符串格式 } tran.Commit(); } catch(Exception ex) { tran.Rollback(); } finally { Connection.Close(); sr.Close(); }
System.IO.StreamReader sr = new System.IO.StreamReader("c:/f.txt"); //伪代码 get Connection // DBTrans tran = Connection.BeginTran(); try{ while (!sr.EndOfStream) { string strReadLine = sr.ReadLine(); string[] strArray = strReadLine.Split(' '); //这里判断你想要的字符串格式 } tran.Commit(); } catch(Exception ex) { tran.Rollback(); } finally { Connection.Close(); sr.Close(); }
void Main() { List<string>list =new List<string>{ "刘德华|50|600", "郭德纲|60|500", "小沈阳|30|400"}; var query=(from l in list let paramString=l.Split('|') orderby int.Parse(paramString[2]) descending select new { name=paramString[0] }).First(); Console.WriteLine(query.name); //刘德华}
//冒泡取第一条就可以! protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { IList<Info> ilist = new List<Info>(); StringBuilder sb = new StringBuilder(); string path = Server.MapPath("/Merchants/txt.txt"); string[] str = File.ReadAllLines(path, Encoding.GetEncoding("gb2312")); foreach (string s in str) { string[] result = s.Split('|'); Info info = new Info(); info.Username = result[0].ToString(); info.Age = Convert.ToInt32(result[1]); info.Score = Convert.ToInt32(result[2]); ilist.Add(info); } for (int i = 0; i < ilist.Count; i++) { for (int j = i + 1; j < ilist.Count; j++) { if (ilist[i].Score < ilist[j].Score) { Info info = ilist[j]; ilist[j] = ilist[i]; ilist[i] = info; } } } Response.Write("用户名:" + ilist[0].Username + " 年龄:" + ilist[0].Age + " 得分:" + ilist[0].Score); } } public class Info { private string username; public string Username { get { return username; } set { username = value; } } private int age; public int Age { get { return age; } set { age = value; } } private int score; public int Score { get { return score; } set { score = value; } } }
给出了比较的代码: 至于从文件中读取出来,我想这个也是很简单的事情;楼主可以自己去实现以下。 部分的代码如下:/* * 刘德华|50|300 * 郭德纲|60|500 * 小沈阳|30|400 */ List<Person> persons = new List<Person>(); Person p=new Person(); p.Name="刘德华"; p.Age=50; p.Score=300; persons.Add(p); p = new Person(); p.Name = "郭德纲"; p.Age = 60; p.Score = 500; persons.Add(p); p = new Person(); p.Name = "小沈阳"; p.Age = 30; p.Score = 400; persons.Add(p); persons.Sort(new PersonCompare()); Person maxPerson = persons[persons.Count-1]; Response.Write(string.Format("得分最多的人是:{0}, 年龄是:{1}",maxPerson.Name,maxPerson.Age.ToString())); public class Person { public string Name { get; set; } public int Age { get; set; } public int Score { get; set; } } public class PersonCompare : IComparer<Person> { #region IComparer<Person> Members public int Compare(Person x, Person y) { return x.Score.CompareTo(y.Score); } #endregion }
protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { List<Info> list = new List<Info>(); StringBuilder sb = new StringBuilder(); string path = Server.MapPath("/Merchants/txt.txt"); string[] str = File.ReadAllLines(path, Encoding.GetEncoding("gb2312")); foreach (string s in str) { string[] result = s.Split('|'); Info info = new Info(); info.Username = result[0].ToString(); info.Age = Convert.ToInt32(result[1]); info.Score = Convert.ToInt32(result[2]); list.Add(info); } list.Sort(new InfoCompare()); Response.Write("用户名:" + list[0].Username + " 年龄:" + list[0].Age + " 得分:" + list[0].Score); } } public class InfoCompare : IComparer<Info> { public int Compare(Info x, Info y) { return y.Score.CompareTo(x.Score); } } public class Info { private string username; public string Username { get { return username; } set { username = value; } } private int age; public int Age { get { return age; } set { age = value; } } private int score; public int Score { get { return score; } set { score = value; } } }
int maxScore;
while(streamToPrint.ReadLine()!= null)
{
读入每行split成 score
判断maxScore < score
则 maxscore = score
同时记录 该行姓名
}
//伪代码 get Connection
// DBTrans tran = Connection.BeginTran();
try{
while (!sr.EndOfStream)
{
string strReadLine = sr.ReadLine();
string[] strArray = strReadLine.Split(' ');
//这里判断你想要的字符串格式
}
tran.Commit();
}
catch(Exception ex)
{
tran.Rollback();
}
finally
{
Connection.Close();
sr.Close();
}
//伪代码 get Connection
// DBTrans tran = Connection.BeginTran();
try{
while (!sr.EndOfStream)
{
string strReadLine = sr.ReadLine();
string[] strArray = strReadLine.Split(' ');
//这里判断你想要的字符串格式
}
tran.Commit();
}
catch(Exception ex)
{
tran.Rollback();
}
finally
{
Connection.Close();
sr.Close();
}
void Main()
{
List<string>list =new List<string>{ "刘德华|50|600",
"郭德纲|60|500",
"小沈阳|30|400"};
var query=(from l in list
let paramString=l.Split('|')
orderby int.Parse(paramString[2]) descending
select new
{
name=paramString[0]
}).First();
Console.WriteLine(query.name);
//刘德华}
//冒泡取第一条就可以!
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
IList<Info> ilist = new List<Info>();
StringBuilder sb = new StringBuilder();
string path = Server.MapPath("/Merchants/txt.txt");
string[] str = File.ReadAllLines(path, Encoding.GetEncoding("gb2312"));
foreach (string s in str)
{
string[] result = s.Split('|');
Info info = new Info();
info.Username = result[0].ToString();
info.Age = Convert.ToInt32(result[1]);
info.Score = Convert.ToInt32(result[2]);
ilist.Add(info);
}
for (int i = 0; i < ilist.Count; i++)
{
for (int j = i + 1; j < ilist.Count; j++)
{
if (ilist[i].Score < ilist[j].Score)
{
Info info = ilist[j];
ilist[j] = ilist[i];
ilist[i] = info;
}
}
}
Response.Write("用户名:" + ilist[0].Username + " 年龄:" + ilist[0].Age + " 得分:" + ilist[0].Score);
}
}
public class Info
{
private string username; public string Username
{
get { return username; }
set { username = value; }
}
private int age; public int Age
{
get { return age; }
set { age = value; }
}
private int score; public int Score
{
get { return score; }
set { score = value; }
}
}
至于从文件中读取出来,我想这个也是很简单的事情;楼主可以自己去实现以下。
部分的代码如下:/*
* 刘德华|50|300
* 郭德纲|60|500
* 小沈阳|30|400
*/
List<Person> persons = new List<Person>();
Person p=new Person();
p.Name="刘德华";
p.Age=50;
p.Score=300;
persons.Add(p); p = new Person();
p.Name = "郭德纲";
p.Age = 60;
p.Score = 500;
persons.Add(p); p = new Person();
p.Name = "小沈阳";
p.Age = 30;
p.Score = 400;
persons.Add(p); persons.Sort(new PersonCompare()); Person maxPerson = persons[persons.Count-1];
Response.Write(string.Format("得分最多的人是:{0}, 年龄是:{1}",maxPerson.Name,maxPerson.Age.ToString()));
public class Person
{
public string Name { get; set; }
public int Age { get; set; }
public int Score { get; set; }
} public class PersonCompare : IComparer<Person>
{ #region IComparer<Person> Members public int Compare(Person x, Person y)
{
return x.Score.CompareTo(y.Score);
} #endregion
}
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
List<Info> list = new List<Info>();
StringBuilder sb = new StringBuilder();
string path = Server.MapPath("/Merchants/txt.txt");
string[] str = File.ReadAllLines(path, Encoding.GetEncoding("gb2312"));
foreach (string s in str)
{
string[] result = s.Split('|');
Info info = new Info();
info.Username = result[0].ToString();
info.Age = Convert.ToInt32(result[1]);
info.Score = Convert.ToInt32(result[2]);
list.Add(info);
}
list.Sort(new InfoCompare());
Response.Write("用户名:" + list[0].Username + " 年龄:" + list[0].Age + " 得分:" + list[0].Score);
}
}
public class InfoCompare : IComparer<Info>
{
public int Compare(Info x, Info y)
{
return y.Score.CompareTo(x.Score);
}
}
public class Info
{
private string username; public string Username
{
get { return username; }
set { username = value; }
}
private int age; public int Age
{
get { return age; }
set { age = value; }
}
private int score; public int Score
{
get { return score; }
set { score = value; }
}
}