注册 登录
编程论坛 VB.NET论坛

紧急求救,vb.net连不到sql server2000数据库??

sexy22 发布于 2008-09-17 16:53, 1087 次点击
Imports System.Data
Imports System.Data.SqlClient

Public Class Form1
    Inherits System.Windows.Forms.Form
    '数据库连接对象
    Dim objConnection As SqlConnection = New SqlConnection("server=(local);database=

(pubs);user id=sa;password=")
    '数据适配器
    Dim objDataAdapter As SqlDataAdapter = New SqlDataAdapter()
    'DataSet
    Dim objDataSet As DataSet = New DataSet()

    Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles

Me.Load
        '设置查询命令属性
        objDataAdapter.SelectCommand = New SqlCommand
        objDataAdapter.SelectCommand.Connection = objConnection
        objDataAdapter. = "select au_lname,au_fname,title,price

from authors join titleauthor on authors.au_id=titleauthor.au_id join titles on

titleauthor.title_id=titles.title_id order by au_lname,au_fname"
        objDataAdapter. = CommandType.Text

        '打开数据库连接
        objConnection.Open()

        '填充DataSet对象
        objDataAdapter.Fill(objDataSet, "authors")

        '关闭数据库连接
        objConnection.Close()

        '给DataGrid绑定数据
        grdAuthorTitles.DataSource = objDataSet
        grdAuthorTitles.DataMember = "authors"

        '清除
        objDataAdapter = Nothing
        objConnection = Nothing
    End Sub
End Class


上面是代码,连接的时候,在objConnection.Open()在这句话上出现了异常,提示:无法打开登录 '(pubs)' 中请求的数据库。登录失败。 用户 'sa' 登录失败。

在sql 2000上,用户名sa,密码为空,就可以登陆,1433端口也开着
请高手帮忙看看是怎么回事???
2 回复
#2
qlong07282008-09-17 18:01
如果没密码可以登陆,有密码不能登陆,哪就是
user id=sa;password="
password=密码和你设的密码不相符
#3
sexy222008-09-17 18:10
回复 2# qlong0728 的帖子
呵呵。。。。非常感谢您的解答,不是密码的问题

应该这样写:

 Dim objConnection As New SqlConnection("Data Source=127.0.0.1; Initial Catalog=pubs; user id=sa; password=;")
1