输入字符串在c#中的格式不正确,int值的格式不正确
以下是它的代码:输入字符串在c#中的格式不正确,int值的格式不正确,c#,sql,.net,csv,int32,C#,Sql,.net,Csv,Int32,以下是它的代码: protected void Upload(object sender, EventArgs e) { if (FileUpload1.HasFile) { //Upload and save the file string csvPath = Server.MapPath("~/App_Data/") + Path.GetFileName(FileU
protected void Upload(object sender, EventArgs e)
{
if (FileUpload1.HasFile)
{
//Upload and save the file
string csvPath = Server.MapPath("~/App_Data/") + Path.GetFileName(FileUpload1.PostedFile.FileName);
FileUpload1.SaveAs(csvPath);
DataTable dt = new DataTable();
dt.Columns.AddRange(new DataColumn[7]
{
new DataColumn("pataintno", typeof(int)),
new DataColumn("Firstname", typeof(string)),
new DataColumn("Lastname",typeof(string)),
new DataColumn("Age", typeof(int)),
new DataColumn("Address", typeof(string)),
new DataColumn("Email", typeof(string)),
new DataColumn("Phno", typeof(int)),});
string csvData = File.ReadAllText(csvPath);
foreach (string row in csvData.Split('\n'))
{
if (!string.IsNullOrEmpty(row))
{
dt.Rows.Add();
int i = 0;
foreach (string cell in row.Split(','))
{
dt.Rows[dt.Rows.Count - 1][i] = cell;
i++;
}
}
}
string consString = ConfigurationManager.ConnectionStrings["cnstr"].ConnectionString;
using (SqlConnection con = new SqlConnection(consString))
{
using (SqlBulkCopy sqlBulkCopy = new SqlBulkCopy(con))
{
//Set the database table name
sqlBulkCopy.DestinationTableName = "Pataint";
con.Open();
sqlBulkCopy.WriteToServer(dt);
con.Close();
Array.ForEach(Directory.GetFiles((Server.MapPath("~/App_Data/"))), File.Delete);
}
}
}
else
{
Label1.Text = "PlZ TRY AGAIN";
}
}
如果DataTable中有3个整型字段,则错误表示从文件中提取的一个或多个数据不是有效的整型 因此,您需要检查错误输入(在这些情况下总是如此) 如果字符串不能转换为整数,则对输入的检查将返回false。在这种情况下,您应该编写某种错误日志,以查看循环何时完成,并发现哪些行不正确并修复它们
还请注意,我在某些方面更改了代码:使用File.ReadAllLines,这样您就已经在每一新行拆分了输入(如果新行只是
\n
或\r\n
代码,则没有问题),并且向数据表添加新行的代码应该遵循以下模式:创建新行,用值填充它,将新行添加到现有集合中。我检查了代码,似乎没有问题。我建议您检查csv文件,确保没有任何列的标题。我今天在将csv解析为sql表时遇到了这个问题。我的解析器从一年以来一直工作良好,但今天突然抛出了int转换错误。SQL大容量复制没有那么多信息,查看csv文件也不会显示数据中有任何错误。csv中的所有数值列都具有有效的数值
为了找到错误,我写了下面的自定义方法。错误在第一条记录上立即弹出。实际问题是供应商更改了数值的csv格式,现在开始呈现十进制值而不是整数。例如,csv文件的值为1.0,而不是值1。当我打开csv文件时,它只反映1,但在记事本中,它显示1.0。我的sql表包含所有整数值,但sql BulkCopy无法处理此转换。花了大约3个小时来找出这个错误。灵感来自以下方面的解决方案:
private void TestData(CsvDataReader-dataReader)
{
int a=0;
while(dataReader.Read())
{
尝试
{
a=int.Parse(dataReader[].ToString());
}
捕获(例外情况除外){}
}
}
欢迎来到StackOverflow。请在您的问题中添加详细信息,在“巫婆行”中您是否遇到错误?你试过什么办法来解决它吗?如果是,结果是什么?你的问题是什么?有什么问题?你试过什么?
// Read all lines and get back an array of the lines
string[] lines = File.ReadAllLines(csvPath);
// Loop over the lines and try to add them to the table
foreach (string row in lines)
{
// Discard if the line is just null, empty or all whitespaces
if (!string.IsNullOrWhiteSpace(row))
{
string[] rowParts = row.Split(',');
// We expect the line to be splittes in 7 parts.
// If this is not the case then log the error and continue
if(rowParts.Length != 7)
{
// Log here the info on the incorrect line with some logging tool
continue;
}
// Check if the 3 values expected to be integers are really integers
int pataintno;
int age;
int phno;
if(!Int32.TryParse(rowParts[0], out pataintno))
{
// It is not an integer, so log the error
// on this line and continue
continue;
}
if(!Int32.TryParse(rowParts[3], out age))
{
// It is not an integer, so log the error
// on this line and continue
continue;
}
if(!Int32.TryParse(rowParts[6], out phno))
{
// It is not an integer, so log the error
// on this line and continue
continue;
}
// OK, all is good now, try to create a new row, fill it and add to the
// Rows collection of the DataTable
DataRow dr = dt.NewRow();
dr[0] = pataintno;
dr[1] = rowParts[1].ToString();
dr[2] = rowParts[2].ToString();
dr[3] = age
dr[4] = rowParts[4].ToString();
dr[5] = rowParts[5].ToString();
dr[6] = phno;
dt.Rows.Add(dr);
}
}
private void TestData(CsvDataReader dataReader)
{
int a = 0;
while(dataReader.Read())
{
try
{
a = int.Parse(dataReader[<<Column name>>].ToString());
}
catch (Exception ex){}
}
}