在批量复制累积之前将数据类型转换为日期
本文关键字:数据 类型转换 日期 复制 | 更新日期: 2023-09-27 18:33:55
可能的重复项:
将 C# 中的 nvarchar 值转换为 SQL 中的等效日期/时间
我从包含不同列的txt文件中读取数据。我需要在插入数据库之前将一列的类型转换为日期/时间
SqlBulkCopy bc = new SqlBulkCopy(Consql.ConnectionString, SqlBulkCopyOptions.TableLock);
bc.DestinationTableName = "name";
bc.ColumnMappings.Add(dt.Columns[0].ColumnName, "IDATE");
bc.ColumnMappings.Add(dt.Columns[1].ColumnName, "ITIME");
bc.ColumnMappings.Add(dt.Columns[2].ColumnName, "TECHID");
bc.ColumnMappings.Add(dt.Columns[5].ColumnName, "CNTRC");
bc.BatchSize = dt.Rows.Count;
Consql.Open();
我认为要使用SqlBulkCopy,您需要实现IDataReader接口。实现它,您可以将源文件中的值转换为所需的类型。
您的自定义阅读器可能如下所示
public class Reader : IDataReader
{
readonly StreamReader _streamReader;
readonly Func<string, object>[] _convertTable;
readonly Func<string, bool>[] _constraintsTable;
string[] _currentLineValues;
string _currentLine;
//Constructing reader you can specify your converters
public Reader(string filepath, Func<string, bool>[] constraintsTable, Func<string, object>[] convertTable)
{
_constraintsTable = constraintsTable;
_convertTable = convertTable;
_streamReader = new StreamReader(filepath);
_currentLine = null;
_currentLineValues = null;
}
public object GetValue(int i)
{
try
{
return _convertTable[i](_currentLineValues[i]);
}
catch (Exception)
{
return null;
}
}
public bool Read()
{
if (_streamReader.EndOfStream) return false;
_currentLine = _streamReader.ReadLine();
_currentLineValues = _currentLine.Split(/*any column splitter*/);
var invalidRow = false;
for (int i = 0; i < _currentLineValues.Length; i++)
{
if (!_constraintsTable[i](_currentLineValues[i]))
{
invalidRow = true;
break;
}
}
return !invalidRow || Read();
}
//other methods...
}
你可以像这样使用它(将其中一个源列转换为日期时间):
static void Main(string[] args)
{
// Create Reader
var reader = GetReader();
// DB connection string
var connectionString = @"Server={blah};initial catalog={blah-blah};Integrated Security=true";
using (var loader = new SqlBulkCopy(connectionString, SqlBulkCopyOptions.Default))
{
loader.ColumnMappings.Add(0, 2);
loader.ColumnMappings.Add(1, 1);
loader.ColumnMappings.Add(2, 3);
loader.ColumnMappings.Add(3, 4);
loader.DestinationTableName = "Customers";
loader.WriteToServer(reader);
Console.WriteLine("Done!");
}
Console.ReadLine();
}
static IDataReader GetReader()
{
var sourceFilepath = "sqlbulktest.txt";
//our converters!
var convertTable = GetConvertTable();
var constraintsTable = GetConstraintsTable();
var reader = new Reader(sourceFilepath, constraintsTable, convertTable);
return reader;
}
static Func<string, bool>[] GetConstraintsTable()
{
var constraintsTable = new Func<string, bool>[4];
constraintsTable[0] = x => !string.IsNullOrEmpty(x);
constraintsTable[1] = constraintsTable[0];
constraintsTable[2] = x => true;
constraintsTable[3] = x => true;
return constraintsTable;
}
static Func<string, object>[] GetConvertTable()
{
var convertTable = new Func<object, object>[4];
convertTable[0] = x => x;
convertTable[1] = x => x;
// Convert to DateTime from specific format!
convertTable[2] = x =>
{
DateTime datetime;
if (DateTime.TryParseExact(x.ToString(), "dd.MM.yyyy", CultureInfo.InvariantCulture,
DateTimeStyles.None, out datetime))
{
return datetime;
}
return null;
};
convertTable[3] = x => Convert.ToInt32(x);
return convertTable;
}