C# “设置”;空";在CsvReader中的空单元格中
这是我的密码:C# “设置”;空";在CsvReader中的空单元格中,c#,asp.net,asp.net-mvc,csv,C#,Asp.net,Asp.net Mvc,Csv,这是我的密码: using (System.Net.WebResponse tmpRes = tmpReq.GetResponse()) { using (System.IO.Stream tmpStream = tmpRes.GetResponseStream()) { using (System.IO.TextReader tmpReader = new System.IO.StreamReader(tmpStream)) {
using (System.Net.WebResponse tmpRes = tmpReq.GetResponse())
{
using (System.IO.Stream tmpStream = tmpRes.GetResponseStream())
{
using (System.IO.TextReader tmpReader = new System.IO.StreamReader(tmpStream))
{
string fileContents = tmpReader.ReadToEnd();
for (int i = 0; i < fileContents.Length; i++)
{
if (fileContents[i] == "")
{
fileContents[i] = "null";
}
}
using (Stream s = GenerateStreamFromString(fileContents))
{}
}
}
}
使用(System.Net.WebResponse tmpRes=tmpReq.GetResponse())
{
使用(System.IO.Stream tmpStream=tmpRes.GetResponseStream())
{
使用(System.IO.TextReader tmpReader=new System.IO.StreamReader(tmpStream))
{
字符串fileContents=tmpReader.ReadToEnd();
for(int i=0;i
这显示错误“字符串”到“字符”隐式转换。有没有其他方法可以在CSVReader的空字段中设置“NULL”您根本没有使用CSVReader。您也没有通过分隔符拆分字符串以获得“单元格”。但是,您可以从
CsvReader
加载DataTable
,并对其进行修改
下面是一个假定制表符为分隔符的示例:
var tblCSV = new DataTable();
using (System.Net.WebResponse tmpRes = tmpReq.GetResponse())
using (System.IO.Stream tmpStream = tmpRes.GetResponseStream())
using (System.IO.TextReader tmpReader = new System.IO.StreamReader(tmpStream))
using (var csv = new CsvReader(tmpReader, true, '\t', '"', '\0', '\0', ValueTrimmingOptions.All))
{
csv.MissingFieldAction = MissingFieldAction.ParseError;
csv.DefaultParseErrorAction = ParseErrorAction.RaiseEvent;
csv.ParseError += csv_ParseError;
csv.SkipEmptyLines = true;
// load into DataTable
tblCSV.Load(csv, LoadOption.OverwriteChanges, csvTable_FillError);
}
现在循环行和列并相应地修改它们:
foreach(DataRow row in tblCSV.Rows)
{
foreach(DataColumn col in tblCSV.Columns)
{
if(string.IsNullOrWhiteSpace(row.Field<string>(col)))
row.SetField(col, "null");
}
}
foreach(tblCSV.Rows中的数据行)
{
foreach(tblCSV.Columns中的数据列col)
{
if(string.IsNullOrWhiteSpace(row.Field(col)))
行设置字段(列,“空”);
}
}
更新与您的评论相关的内容:
我想在sql数据库的空单元格中添加空值,当csv 数据保存在数据库中。还有别的办法吗 您可以简单地使用上面的循环来更新表:
using (var con = new SqlConnection("ConnectionString"))
{
con.Open();
foreach (DataRow row in tblCSV.Rows)
{
using (var cmd = new SqlCommand("INSERT INTO table(Col1,Col2) VALUES (@Col1,Col2);", con))
{
string col1 = row.Field<string>("Col1");
if (string.IsNullOrWhiteSpace(col1))
col1 = null;
string col2 = row.Field<string>("Col2");
if (string.IsNullOrWhiteSpace(col2))
col2 = null;
cmd.Parameters.AddWithValue("@col1", col1);
cmd.Parameters.AddWithValue("@col2", col2);
int inserted = cmd.ExecuteNonQuery();
}
}
}
使用(var-con=new-SqlConnection(“ConnectionString”))
{
con.Open();
foreach(tblCSV.Rows中的数据行)
{
使用(var cmd=newsqlcommand(“插入表(Col1,Col2)值(@Col1,Col2);”,con))
{
字符串col1=行字段(“col1”);
if(string.IsNullOrWhiteSpace(col1))
col1=null;
字符串col2=行字段(“col2”);
if(string.IsNullOrWhiteSpace(col2))
col2=null;
cmd.Parameters.AddWithValue(“@col1”,col1);
cmd.Parameters.AddWithValue(“@col2”,col2);
int inserted=cmd.ExecuteNonQuery();
}
}
}
您在哪里使用CSVReader
呢?还有一些CSVReader-您在使用哪一个?从ftpwebrequestI读取CSV我想在数据库中保存CSV数据时在sql数据库的空单元格中添加空值。还有别的办法吗,谢谢回答