C# StreamReader 导入txt文件,数据丢失了
我这个方法是导入一个目录下的3个TXT文件(每个文件共四行信息),读取后逐行导入数据库,可是我执行后,我每个TXT文件只导入了第一行和第三行信息,第二行,第四行不翼而飞,...
我这个方法是导入一个目录下的3个TXT文件(每个文件共四行信息),读取后逐行导入数据库,可是我执行后,我每个TXT文件只导入了第一行和第三行信息,第二行,第四行不翼而飞,求大神扶住,求解啊,我暂时没分,好人一生平安啊!
public static void Input(FolderBrowserDialog Date)
{
string Strs = ConfigurationManager.ConnectionStrings["Strs"].ConnectionString;
using (SqlConnection Conn = new SqlConnection(Strs))
{
Conn.Open();
using (SqlCommand _Input = Conn.CreateCommand())
{
_Input.CommandText = "Insert into Student_Information values(newid(),@Class,@No,@Name,@Score,@Password,0)";
string path = Date.SelectedPath;
string[] Information = Directory.GetFiles(path, "*.txt", SearchOption.AllDirectories);
foreach (string file in Information)
{
//string[] lines = File.ReadAllLines(sum, Encoding.Default);
//foreach (string line in lines)
using (StreamReader reader = new StreamReader(file, Encoding.Default))
{
string line = reader.ReadLine();
while (reader.ReadLine() != null)
{
string[] Info = line.Split('|');
string Class = Info[0];
string No = Info[1];
string Name = Info[2];
string Score = Info[3];
string Password = No.Substring(4);
_Input.Parameters.Clear();
_Input.Parameters.Add(new SqlParameter("Class", Class));
_Input.Parameters.Add(new SqlParameter("No", No));
_Input.Parameters.Add(new SqlParameter("Name", Name));
_Input.Parameters.Add(new SqlParameter("Score", Convert.ToInt32(Score)));
_Input.Parameters.Add(new SqlParameter("Password", Password));
_Input.ExecuteNonQuery();
line = reader.ReadLine();
}
reader.Close();
}
}
}
}
} 展开
public static void Input(FolderBrowserDialog Date)
{
string Strs = ConfigurationManager.ConnectionStrings["Strs"].ConnectionString;
using (SqlConnection Conn = new SqlConnection(Strs))
{
Conn.Open();
using (SqlCommand _Input = Conn.CreateCommand())
{
_Input.CommandText = "Insert into Student_Information values(newid(),@Class,@No,@Name,@Score,@Password,0)";
string path = Date.SelectedPath;
string[] Information = Directory.GetFiles(path, "*.txt", SearchOption.AllDirectories);
foreach (string file in Information)
{
//string[] lines = File.ReadAllLines(sum, Encoding.Default);
//foreach (string line in lines)
using (StreamReader reader = new StreamReader(file, Encoding.Default))
{
string line = reader.ReadLine();
while (reader.ReadLine() != null)
{
string[] Info = line.Split('|');
string Class = Info[0];
string No = Info[1];
string Name = Info[2];
string Score = Info[3];
string Password = No.Substring(4);
_Input.Parameters.Clear();
_Input.Parameters.Add(new SqlParameter("Class", Class));
_Input.Parameters.Add(new SqlParameter("No", No));
_Input.Parameters.Add(new SqlParameter("Name", Name));
_Input.Parameters.Add(new SqlParameter("Score", Convert.ToInt32(Score)));
_Input.Parameters.Add(new SqlParameter("Password", Password));
_Input.ExecuteNonQuery();
line = reader.ReadLine();
}
reader.Close();
}
}
}
}
} 展开
3个回答
展开全部
你也不看看你调了几次ReadLine,while那个地方,你Read出来保存了没?
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
while (reader.ReadLine() != null)
换成while (line != null)
换成while (line != null)
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询