Vb.Net Datagridview什么都不显示! [英] Vb.Net Datagridview show nothing!

查看:142
本文介绍了Vb.Net Datagridview什么都不显示!的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经编写了代码来显示datagridview中的记录,但是什么也没显示.我试图调试&当它进入GetData函数时,遵循以下代码

I have written code to show records in a datagridview but it shows nothing. I have tried to debug & when it step into GetData function following codes

Catch ex As Exception
returnData = Nothing
If connection.State = ConnectionState.Open Then
    connection.Close()
End If



变灰它显示错误符号.当我将鼠标指针放在上面时,它会显示消息.它显示无法打开登录名请求的数据库SUIMT.登录失败.用户" MY-PC \ James"的登录失败".请检查我的代码&帮助我摆脱这个问题&不要忘记在答案中添加示例.
必要的信息:我正在使用SQL SERVER 2000个人版.我的数据库名称是"SUIMT"&我想在datagridview中显示的表称为"monthly_instal".



become gray & it shows error sign. When I take my mouse pointer on it it shows message. It says "Cannot open database SUIMT requested by the login. The login failed. Login failed for user ''MY-PC\James''". Please check my code & help me to get rid of this problem & dont forget to add example with your answer.
Necessary information : I am using SQL SERVER 2000 PERSONAL EDITION. Name of my database is "SUIMT" & table which I want to show in datagridview is called "monthly_instal".

Imports System.Data.SqlClient
Public Class Form34
    Private Const ConnectionString As String = "Server=.\SQLEXPRESS;" & _
    "Database=SUIMT;Trusted_Connection=True"

    Private Sub Form34_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        cmbdmiid.Items.Clear()
        cmbdmiid.Text = "ID NUMBER"
        cmbdmidt.Items.Clear()
        cmbdmidt.Text = "DATE"
        txtdmiron.Text = "ROW NUMBER"
        con = New ADODB.Connection
        con.Open("Provider=SQLOLEDB.1;Integrated Security=SSPI;Persist Security Info=False;Initial Catalog=SUIMT")
        rst = New ADODB.Recordset
        With rst
            .Open("Select * From monthly_instal", con, ADODB.CursorTypeEnum.adOpenStatic, ADODB.LockTypeEnum.adLockReadOnly)
            If .BOF = False Then
                .MoveFirst()
                While .EOF = False
                    If Not cmbdmiid.Items.Contains(.Fields("stu_id").Value) Then
                        cmbdmiid.Items.Add(.Fields("stu_id").Value)
                    End If
                    .MoveNext()
                End While
            End If
            .Close()
        End With
        rst = New ADODB.Recordset
        With rst
            .Open("Select * From monthly_instal", con, ADODB.CursorTypeEnum.adOpenStatic, ADODB.LockTypeEnum.adLockReadOnly)
            If .BOF = False Then
                .MoveFirst()
                While .EOF = False
                    If Not cmbdmidt.Items.Contains(.Fields("dt").Value) Then
                        cmbdmidt.Items.Add(.Fields("dt").Value)
                    End If
                    .MoveNext()
                End While
            End If
            .Close()
        End With
        Me.CREATEUSERToolStripMenuItem.Enabled = False
        Me.DELETEUSERToolStripMenuItem.Enabled = False
        Me.CHANGEPASSWORDToolStripMenuItem.Enabled = False
        Me.ASSIGNPERMISSIONToolStripMenuItem.Enabled = False
        Me.SHOWALLToolStripMenuItem.Enabled = False
        Me.CREATEACADEMICYEARToolStripMenuItem.Enabled = False
        Me.DELETEACADEMICYEARToolStripMenuItem.Enabled = False
        Me.CREATESESSIONToolStripMenuItem.Enabled = False
        Me.DELETESESSIONToolStripMenuItem.Enabled = False
        Me.CREATEDEPARTMENTToolStripMenuItem.Enabled = False
        Me.DELETEDEPARTMENTToolStripMenuItem.Enabled = False
        Me.CREATEEXAMTYPEToolStripMenuItem.Enabled = False
        Me.DELETEEXAMTYPEToolStripMenuItem.Enabled = False
        Me.CREATESUBJECTToolStripMenuItem.Enabled = False
        Me.DELETESUBJECTToolStripMenuItem.Enabled = False
        Me.ADMISSIONToolStripMenuItem.Enabled = False
        Me.DELETEADMISSIONToolStripMenuItem.Enabled = False
        Me.ADMISSIONREPORTToolStripMenuItem.Enabled = False
        Me.STUDENTINFORMATIONToolStripMenuItem.Enabled = False
        Me.DELETESTUDENTINFORMATIONToolStripMenuItem.Enabled = False
        Me.SEARCHSTUDENTINFORMATIONToolStripMenuItem.Enabled = False
        Me.STUDENTINFORMATIONREPORTToolStripMenuItem.Enabled = False
        Me.CREATESTUDENTRESULTToolStripMenuItem.Enabled = False
        Me.DELETESTUDENTRESULTToolStripMenuItem.Enabled = False
        Me.STUDENTRESULTREPORTToolStripMenuItem.Enabled = False
        Me.RESULTSHEETREPORTToolStripMenuItem.Enabled = False
        Me.CREATEMONTHLYINSTALLMENTToolStripMenuItem.Enabled = False
        Me.DELETEMONTHLYINSTALLMENTToolStripMenuItem.Enabled = False
        Me.MONTHLYINSTALLMENTREPORTToolStripMenuItem.Enabled = False
        Me.CREATEBALANCESHEETToolStripMenuItem.Enabled = False
        Me.DELETEBALANCESHEETToolStripMenuItem.Enabled = False
        Me.BALANCESHEETREPORTToolStripMenuItem.Enabled = False
        Me.ABOUTTHISPROGRAMToolStripMenuItem.Enabled = False
        Me.EXIToolStripMenuItem.Enabled = False

    End Sub
    Public Function GetData() As DataTable
        Dim SelectQry = "Select row_num, stu_id, tot_amou, paid, du, aoins, due, dt From monthly_instal where stu_id = '" & cmbdmiid.Text & "' AND dt = '" & cmbdmidt.Text & "'"
        Dim connection As New SqlConnection(ConnectionString)
        Dim returnData As New DataTable("monthly_instal")
        Try
            connection.Open()
            Dim command As New SqlCommand(SelectQry, connection)
            Dim adapter = New SqlDataAdapter(command)
            adapter.Fill(returnData)
            con.Close()
        Catch ex As Exception
            returnData = Nothing
            If connection.State = ConnectionState.Open Then
                connection.Close()
            End If
        End Try
        Return returnData
    End Function

    Private Sub butdmiclo_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles butdmiclo.Click
        speak34.Speak("Delete monthly installment window has been closed successfully")
        Me.Close()
    End Sub

    Private Sub cmbdmidt_Leave(ByVal sender As Object, ByVal e As System.EventArgs) Handles cmbdmidt.Leave
        dtg1.DataSource = Nothing
        dtg1.DataSource = GetData()
    End Sub


End Class



必要信息:我的计算机名称是MY-PC.当我单击SQL SERVER SERVICE MANAGER时,它显示SERVER-MY-PC.我猜必须将MY-PC用作服务器名称.结果我用了它



Necessary information : Name of my computer is MY-PC. When I am clicking on SQL SERVER SERVICE MANAGER it shows SERVER - MY-PC. I guess MY-PC must be used as a server name. As a result I used it

Private Const ConnectionString As String = "Server=.\MY-PC;" & _
  "Database=SUIMT;Trusted_Connection=True"



但它显示错误.它说:建立与服务器的连接时发生错误.连接到SQL Server 2005时,此故障可能是由于以下事实导致的:在默认设置下,SQL Server不允许远程连接.(提供者:SQL Network Interfaces,错误:26-查找指定的服务器/实例时出错)"
我很困惑,它说在创建与SQL SERVER 2005的连接时发生了错误!但是我需要将其与SQL SERVER 2000连接.是的,我有SQL SERVER 2000& 2005(随Visual Studio安装了SQL SERVER 2005).但是我正在使用SQL SERVER2000.



But it shows error. It says "An error has occurred while establishing a connection to the server. When connecting to SQL Server 2005, this failure may be caused by the fact that under the default settings SQL Server does not allow remote connections. (provider: SQL Network Interfaces, error: 26 - Error Locating Server/Instance Specified)"
I am confused it says error has occurred to create connection with SQL SERVER 2005! But I need to connect it with SQL SERVER 2000. Yes I have SQL SERVER 2000 & 2005(SQL SERVER 2005 was installed with Visual studio). But I am using SQL SERVER 2000.

推荐答案

您应该使用实例名称来调用服务器.
如果您在本地计算机上安装了SQL Server,则可以构建如下的连接字符串:
You should call server by it''s instance name.
If you have installed SQL server on local computer you can build connection string like this:
Private Const ConnectionString As String = "Server=(local);" & _
"Database=SUIMT;Trusted_Connection=True"




or

Private Const ConnectionString As String = "Server=MY-PC\SQLEXPRESS;" & _
"Database=SUIMT;Trusted_Connection=True"


进入您的SQL Server日志并查看消息.

然后,请参见此处: http://blogs.msdn.com/b/sql_protocols /archive/2006/02/21/536201.aspx [ ^ ]

此页面告诉您状态的含义.这样,您可以判断密码是否不正确,用户是否不存在,等等...
go into your SQL server logs and look at the messages.

Then, see here: http://blogs.msdn.com/b/sql_protocols/archive/2006/02/21/536201.aspx[^]

This page tells you what the state means. This way you can tell if the password wasn''t right, if the user doesn''t exist, etc...


这篇关于Vb.Net Datagridview什么都不显示!的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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