读书人

vb.net连接SQL出错:索引从0处开始

发布时间: 2014-01-28 21:16:04 作者: rapoo

vb.net连接SQL出错

源程序如下:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim selectclause As String = "select *"
dim fromclause As String = "from 站点信息" '用于构成from子句
Dim whereclause As String = ""
If Not TextBox1.Text = "" Then
whereclause += "and 站点名= " + "'" + TextBox1.Text + "'"
End If
If Not TextBox2.Text = "" Then
whereclause += "and 地址= " + "'" + TextBox2.Text + "'"
End If
If Not TextBox3.Text = "" Then
whereclause += "and 站点类型= " + "'" + TextBox3.Text + "'"
End If

Dim querystring As String = selectclause + fromclause + whereclause
Dim connectionstring As String = "user id="";initial catalog= 站点信息表;data source=localhost"

Dim connection As New SqlConnection(connectionstring)”运行时这句总出错,说是'从索引0初开始,初始化字符串处理异常'“[/color]
Dim dataadapter As New SqlDataAdapter
dataadapter.SelectCommand = New SqlCommand(querystring, connection)

Dim dataset As New DataSet

Try
connection.Open()
dataadapter.Fill(dataset, "站点信息")
DataGridView1.DataSource = dataset.Tables("站点信息")
DataGridView1.Focus()

Catch ex As Exception
MsgBox(ex.Message)

End Try

connection.Close()


End Sub


------解决方法--------------------------------------------------------
Dim querystring As String = selectclause + fromclause + whereclause

querystring 拼起来后里面确少必要的空格吧 你有没有DEBUG进去看下这个SQL啊?

比如 Dim selectclause As String = "select *"
dim fromclause As String = "from 站点信息"
        

读书人网 >.NET

热点推荐