可能是一个非常简单的问题,但我遇到了一些问题。 目前我正在从OLAP Sever中提取数据并将其写入SQL数据库。我用下面的字符串创建了我的数据库。vb.net SQL连接字符串,只写入数据库中的最后一个值
Dim strSQL As String =
"CREATE TABLE Datab(" &
"ID Int IDENTITY(1,1) PRIMARY KEY," &
"No Int NOT NULL," &
"Name NVarChar(40) NOT NULL," &
"basicvalue NVarChar(40) NOT NULL," &
"Datee Date NOT NULL," &
")"
我的连接如下:
昏暗的DbConnection作为新的SqlConnection(的connectionString)
'A SqlCommand object is used to execute the SQL commands.
Dim cmd As New SqlCommand(strSQL, dbConnection)
Dim regDate As DateTime = DateTime.Now
Dim strDate As String = regDate.ToString("yyyy-MM-dd")
If IsError(mdsRet) = vbTrue Then
MsgBox("Error Connecting: " & IsError(MdsGetLastError))
Else
' MsgBox("Connected to Server: " + mdsRet)
'Check the number of Cubes in OLAP
If tables.TablesCount(srv) >= 0 Then
dbConnection.Open()
'Counter from 1 to nu. of Cubes
cmd1.CommandType = System.Data.CommandType.Text
For counter As Integer = 1 To tables.TablesCount(srv)
cmd1.CommandText = "INSERT INTO Datab(No, Name, basicvalue,datee) VALUES ('" & counter & "','" + tables.TablesName(srv, counter) + "','" & tables.TableGetInfo(srv, tables.TablesName(srv, counter), 56) & "','" & strDate & "')"
cmd1.Connection = dbConnection
cmd.ExecuteNonQuery()
cmd1.ExecuteNonQuery()
counter = counter + 1
Next
End If
End If
'close DB connection
dbConnection.Close()
连接写入行成数据库,但不增加索引行,因此我得到了数据库表中的最后一个值,即执行完成后。总共有大约60行要写入。
我尝试了不同的选项来增加计数器,但它不工作。任何人都可以帮助和解释。
谢谢
删除'counter = counter + 1'作为For循环会自动增加'counter'。不确定'tables.TablesName(srv,counter)'的结构,但是它们通常从0开始。所以把'As Counter As Integer = 1'改为'For Counter As Integer = 0',当你插入'(' “&counter&”','“'把它改成'('”&(counter + 1)&“','”'你也想看一下'Parameters'而不是连接在一起的字符串 – Bugs
嗨@ Jinx88909,我没有改变它..如果我改变'对于计数器为整数= 0'然后我得到一个SQL转换错误从字符串到双和当我插入'('“&(counter + 1)&”', '''然后它简单地增加计数器,即当计数器应该是1它写入2(1 + 1)。 – PUser
是啊,你需要从0开始,最后一点工作。我假设'tables.TablesName'是基于零的意思是第一个记录从0开始,而不是1?这个错误将是别的,它可能有助于通过使用参数来解决这个问题,所以'cmd1.Parameters.Add()'。开启** Option Strict **也会从长远来看有助于编码。 – Bugs