如何使用 ASP.Net SqlBulkCopy 将常量值插入到列中
在下面的代码中,我尝试将记录从 Excel 插入到数据库表中,但附加列未通过 Excel 传递,该列必须填充从请求页面分配的常量值(具有不同值的 foreach 循环) 。
string CONSTANTVALUE="Test";
bulkCopy.DestinationTableName = "TABLE NAME";
bulkCopy.ColumnMappings.Add("TABLECOLUMN1", "EXCELCOLUMN1");
bulkCopy.ColumnMappings.Add("TABLECOLUMN2", "EXCELCOLUMN2");
bulkCopy.ColumnMappings.Add("TABLECOLUMN3", CONSTANTVALUE);
bulkCopy.WriteToServer(dr);
但代码不起作用。有什么想法吗?
In the below code, I am trying insert the records from excel to Database table, but an additional column is not passed through the excel, which has to be populated with a constant value(foreach loop with a different value) assigned from the requested page.
string CONSTANTVALUE="Test";
bulkCopy.DestinationTableName = "TABLE NAME";
bulkCopy.ColumnMappings.Add("TABLECOLUMN1", "EXCELCOLUMN1");
bulkCopy.ColumnMappings.Add("TABLECOLUMN2", "EXCELCOLUMN2");
bulkCopy.ColumnMappings.Add("TABLECOLUMN3", CONSTANTVALUE);
bulkCopy.WriteToServer(dr);
But the code doesn't work. Any ideas?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以通过更改命令文本来做到这一点。如下
You can do it, by changing your command text. As below
我假设您的
博士
是某种读者。它的人口情况如何?可以选择一个默认值到列中并映射该值。像这样的东西(sql语法)然后有:
HTH
I assume your
dr
is a reader of some kind. How is it populated? It may be possible to select a default value into a column and map that. Something like this (sql syntax)Then have:
HTH
您是否尝试为数据库中的列设置默认值?我认为这是最简单的方法,因为插入任何记录后,默认值也会被插入到指定的列中(它的作用类似于触发器)。
Did you try setting a default value for you column in database? I think it's the most easiest way, as after inserting any record, the default value would also get inserted into the specified column (it acts like triggers).