Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/278.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 使用带C的SqlBulkCopy自动添加SQL日期时间#_C#_Sql Server_Datetime - Fatal编程技术网

C# 使用带C的SqlBulkCopy自动添加SQL日期时间#

C# 使用带C的SqlBulkCopy自动添加SQL日期时间#,c#,sql-server,datetime,C#,Sql Server,Datetime,我正在使用SqlBulkCopy命令将数据输入sql表。我的第一列是CreatedDate,我将默认值或绑定设置为getdate(),不带引号。然而,当bulkCopy.WriteToServer(表)出现时: 无法删除数据源中字符串类型的给定值 已转换为指定目标列的datetime类型 内部的例外是 无法将参数值从字符串转换为日期时间 我已将CreatedDate列的默认值或绑定设置为getdate()。执行SqlBulkCopy命令时,是否会自动将当前日期和时间插入列中 是否需要将列Crea

我正在使用
SqlBulkCopy
命令将数据输入sql表。我的第一列是
CreatedDate
,我将默认值或绑定设置为
getdate()
,不带引号。然而,当
bulkCopy.WriteToServer(表)出现时

无法删除数据源中字符串类型的给定值 已转换为指定目标列的datetime类型

内部的例外是

无法将参数值从字符串转换为日期时间

我已将
CreatedDate
列的默认值或绑定设置为
getdate()
。执行
SqlBulkCopy
命令时,是否会自动将当前日期和时间插入列中

是否需要将列
CreatedDate
添加到
DataTable
?如果我有自动插入的列,我需要映射它们还是添加它们?如果是这样,您将如何添加或映射SQL数据类型的列
datetime

var conn = new SqlConnection(connectionString);
var sessionId = Guid.NewGuid();

var table = new DataTable(); // create the datatable

table.Columns.Add("location", typeof (string));
table.Columns.Add("product_name", typeof (string));
table.Columns.Add("product_id", typeof (string));
table.Columns.Add("product_price", typeof (string));
table.Columns.Add("session_id", typeof (string));

foreach (var pair in idsAndPrices) // then loop all dictionary entries and add the rows
{
    table.Rows.Add(location, productName, pair.Key, pair.Value, sessionId);
}

using (var bulkCopy = new SqlBulkCopy(conn))
{
    bulkCopy.DestinationTableName = "dbo.ProductInventory";
    conn.Open();
    bulkCopy.WriteToServer(table); // write to database
    conn.Close();
}

这能满足你的需要吗

// Add a column with the correct name
table.Columns.Add("CreatedDate", typeof (DateTime));
// ...
// Update the row you are inserting
table.Rows.Add(location, productName, pair.Key, pair.Value, sessionId, DateTime.Now);

我查看了数据库中的行顺序,发现它们已被更改。为了解决这个问题,我实现了
SqlBulkCopyColumnMapping
,以确保在调用
bulkCopy.WriteToServer()之前列名是对齐的添加条目时,数据库中的“CreatedDate”列会自动填充表。对于自动递增的列,不需要映射,但建议映射其他列

            var table = new DataTable(); // create the datatable

            table.Columns.Add("location", typeof (string));
            table.Columns.Add("product_name", typeof (string));
            table.Columns.Add("product_id", typeof (string));
            table.Columns.Add("object_price", typeof (string));
            table.Columns.Add("session_id", typeof (string));


            foreach (var pair in webidsAndXPaths) // then loop all dictionary entries and add the rows
            {
                table.Rows.Add(location, productName, pair.Key, pair.Value, sessionId);
            }

            using (var bulkCopy = new SqlBulkCopy(conn))
            {
                // Set up the column mappings by name.
                var l = new SqlBulkCopyColumnMapping("location", "location");
                var pn = new SqlBulkCopyColumnMapping("product_name", "product_name");
                var pid = new SqlBulkCopyColumnMapping("product_id", "product_id");
                var pp = new SqlBulkCopyColumnMapping("product_price", "product_price");
                var sid = new SqlBulkCopyColumnMapping("session_id", "session_id");


                bulkCopy.ColumnMappings.Add(l);
                bulkCopy.ColumnMappings.Add(pn);
                bulkCopy.ColumnMappings.Add(pid);
                bulkCopy.ColumnMappings.Add(pp);
                bulkCopy.ColumnMappings.Add(sid);

                // destinationDatabase = "dbo.ObjectInventory"
                bulkCopy.DestinationTableName = destinationDatabase;
                conn.Open();
                bulkCopy.WriteToServer(table); // write to database
                conn.Close();
            }

            return true;

可以向表中添加datetime列。标准SQL日期时间格式为YYYY-MM-DD HH:MM:SS。因此,您可以使用
datetime.now
在.net中获取当前日期时间。然后您可以使用string.format解析该日期时间,如so
string.format(“{0:yyyy-MM-dd HH:MM:ss}”,datetime.now)
然后可以将该格式化日期时间输入数据库。