如何使用sqlbulkcopy语句为SQL stringed数据库创建每个语句 [英] How do I make an for each statement for an SQL stringed database using a sqlbulkcopy statement

查看:108
本文介绍了如何使用sqlbulkcopy语句为SQL stringed数据库创建每个语句的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我现在通过手动字符串连接使用我的sql数据库,而不是通过vb.net数据库向导连接它们。我试图在数据库之间进行搜索时发现这种方法更充分。我想要做的是连接到我选择的数据集并运行一个bulkcopy语句来导入excel信息。



I am now using my sql databases through manual string connections instead of connecting them through the vb.net database wizard. I found this method to more sufficient when trying to go between databases. what I am trying to do is make a connection to a dataset of my choosing and run a bulkcopy statement to import excel information.

Dim sqlconn As New SqlConnection(MySQLstring)
      Dim sqladaptor = New SqlDataAdapter("select * from" + "[" + TreeView1.SelectedNode.Text.ToString + "]", sqlconn)
      Dim sql As String = "select * from" + "[" + TreeView1.SelectedNode.Text.ToString + "]"
      Dim sqlcmd As SqlClient.SqlCommand
      sqlcmd = New SqlClient.SqlCommand(sql, sqlconn)
      Dim dt As New DataTable
      Dim ds As New DataSet
      sqladaptor.Fill(dt)

      For Each column In ds.Tables(TreeView1.SelectedNode.Text.ToString).Columns

          sqlBulkCopy.ColumnMappings.Add(column.ColumnName, If(String.IsNullOrEmpty(column.ColumnName), " ", column.ColumnName))


      Next





当我运行我的代码时收到此错误消息:

其他通知ation:对象引用未设置为对象的实例。此消息显示在ds.Tables(TreeView1.SelectedNode.Text.ToString).Columns



我不知道在连接到sql时如何访问数据集手动。

如何使用sql连接字符串向数据库写一个语句?



我尝试过:



我试图调用不同的表并尝试导入但是所有人都说同样的事情。有趣的是,当我使用数据库向导导入的数据集时,这是有效的,但现在它不是



when I run my code I get this error message:
Additional information: Object reference not set to an instance of an object. This message appears at the "ds.Tables(TreeView1.SelectedNode.Text.ToString).Columns"

I do not know how to access the dataset while connecting to sql manually.
how can I write a statement using an sql connection string to a database?

What I have tried:

I have tried to call forth different tables and try the import but the all say the same thing. what is interesting is this worked when I used a dataset that the database wizard imported, but now it doesn't

推荐答案

对象引用未设置为实例一个对象。



这意味着您正在尝试使用从未初始化的引用变量。发现哪一个出错的唯一方法是使用调试器逐步执行代码。
Object reference not set to an instance of an object.

It means that you are trying to use a reference variable which has never been initialised. The only way to discover which one is in error is to use the debugger to step through your code.


Richard我弄清楚问题是什么。我正在引用一个新创建的数据表,我将其更改为引用数据集。这是代码修改,只是一个额外的步骤。



Richard I figured out what the problem was. I was referencing a newly created data table, which I changed up to reference the dataset. here is the code modification, it was just an extra step.

 Dim dt As New DataTable
Dim ds As New DataSet
sqladaptor.Fill(ds, TreeView1.SelectedNode.Text)


这篇关于如何使用sqlbulkcopy语句为SQL stringed数据库创建每个语句的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆