Datagridview不工作! [英] Datagridview doesnt work!
问题描述
我已经编写代码来显示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登录失败。请检查我的代码帮我摆脱这个问题&不要忘了用你的答案添加示例。
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.
Imports System.Data.SqlClient
Public Class Form34
Private Const ConnectionString As String = "Server=.\SQLEXPRESS;" & _
"Database=SUIMT;Trusted_Connection=True"
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 = '" & cmbdmiidn.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 Form34_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
cmbdmidt.Items.Clear()
cmbdmidt.Text = "DATE"
cmbdmiidn.Items.Clear()
cmbdmiidn.Text = "ID NUMBER"
txtdmirn.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 cmbdmidt.Items.Contains(.Fields("dt").Value) Then
cmbdmidt.Items.Add(.Fields("dt").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 cmbdmiidn.Items.Contains(.Fields("stu_id").Value) Then
cmbdmiidn.Items.Add(.Fields("stu_id").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
Private Sub cmbdmidt_Leave(ByVal sender As Object, ByVal e As System.EventArgs) Handles cmbdmidt.Leave
dg1.DataSource = Nothing
dg1.DataSource = GetData()
End Sub
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
End Class
推荐答案
看起来像我正在尝试使用网络凭据连接到数据库,但您在连接字符串中缺少一部分。
Looks to me like you are trying to use network credentials to connect to the database, but you are missing a part in the connection string.
尝试更改:
Private Const ConnectionString As String = "Server=.\SQLEXPRESS;" & _
"Database=SUIMT;Trusted_Connection=True"
为此:
Private Const ConnectionString As String = "Server=.\SQLEXPRESS;" & _
"Database=SUIMT;Trusted_Connection=True;Integrated Security=SSPI;"
这篇关于Datagridview不工作!的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!