标题:求助VB.NET与数据库联接出错
取消只看楼主
chencjf1991
Rank: 1
等 级:新手上路
帖 子:2
专家分:0
注 册:2005-4-22
 问题点数:0 回复次数:0 
求助VB.NET与数据库联接出错

本人在用VB.NET与SQL做一个简单的数据绑定 用代码实现的,就是中国铁道出版社 章立民编著的 ADO.NET+VB.NET数据库应用开发指南第5页的例2,有所改动 Imports System.Data.SqlClient Public Class Form1 Inherits System.Windows.Forms.Form Dim ds As DataSet = New DataSet

#Region " Windows 窗体设计器生成的代码 "

Public Sub New() MyBase.New()

'该调用是 Windows 窗体设计器所必需的。 InitializeComponent()

'在 InitializeComponent() 调用之后添加任何初始化

End Sub

'窗体重写 dispose 以清理组件列表。 Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean) If disposing Then If Not (components Is Nothing) Then components.Dispose() End If End If MyBase.Dispose(disposing) End Sub

'Windows 窗体设计器所必需的 Private components As System.ComponentModel.IContainer

'注意: 以下过程是 Windows 窗体设计器所必需的 '可以使用 Windows 窗体设计器修改此过程。 '不要使用代码编辑器修改它。 Friend WithEvents TextBoxid As System.Windows.Forms.TextBox Friend WithEvents TextBoxname As System.Windows.Forms.TextBox Friend WithEvents TextBoxaddress As System.Windows.Forms.TextBox Friend WithEvents DateTimePickerbirthday As System.Windows.Forms.DateTimePicker Friend WithEvents TextBoxdepartment As System.Windows.Forms.TextBox Friend WithEvents NumericUpDownsalary As System.Windows.Forms.NumericUpDown Friend WithEvents Button1 As System.Windows.Forms.Button Friend WithEvents Button2 As System.Windows.Forms.Button Friend WithEvents Button3 As System.Windows.Forms.Button Friend WithEvents Button4 As System.Windows.Forms.Button <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent() Me.TextBoxid = New System.Windows.Forms.TextBox Me.TextBoxname = New System.Windows.Forms.TextBox Me.TextBoxaddress = New System.Windows.Forms.TextBox Me.NumericUpDownsalary = New System.Windows.Forms.NumericUpDown Me.DateTimePickerbirthday = New System.Windows.Forms.DateTimePicker Me.TextBoxdepartment = New System.Windows.Forms.TextBox Me.Button1 = New System.Windows.Forms.Button Me.Button2 = New System.Windows.Forms.Button Me.Button3 = New System.Windows.Forms.Button Me.Button4 = New System.Windows.Forms.Button CType(Me.NumericUpDownsalary, System.ComponentModel.ISupportInitialize).BeginInit() Me.SuspendLayout() ' 'TextBoxid ' Me.TextBoxid.Location = New System.Drawing.Point(128, 16) Me.TextBoxid.Name = "TextBoxid" Me.TextBoxid.Size = New System.Drawing.Size(248, 21) Me.TextBoxid.TabIndex = 0 Me.TextBoxid.Text = "" ' 'TextBoxname ' Me.TextBoxname.Location = New System.Drawing.Point(128, 56) Me.TextBoxname.Name = "TextBoxname" Me.TextBoxname.Size = New System.Drawing.Size(248, 21) Me.TextBoxname.TabIndex = 1 Me.TextBoxname.Text = "" ' 'TextBoxaddress ' Me.TextBoxaddress.Location = New System.Drawing.Point(128, 104) Me.TextBoxaddress.Name = "TextBoxaddress" Me.TextBoxaddress.Size = New System.Drawing.Size(256, 21) Me.TextBoxaddress.TabIndex = 2 Me.TextBoxaddress.Text = "" ' 'NumericUpDownsalary ' Me.NumericUpDownsalary.Location = New System.Drawing.Point(128, 192) Me.NumericUpDownsalary.Name = "NumericUpDownsalary" Me.NumericUpDownsalary.Size = New System.Drawing.Size(288, 21) Me.NumericUpDownsalary.TabIndex = 3 ' 'DateTimePickerbirthday ' Me.DateTimePickerbirthday.Location = New System.Drawing.Point(128, 136) Me.DateTimePickerbirthday.Name = "DateTimePickerbirthday" Me.DateTimePickerbirthday.Size = New System.Drawing.Size(280, 21) Me.DateTimePickerbirthday.TabIndex = 4 ' 'TextBoxdepartment ' Me.TextBoxdepartment.Location = New System.Drawing.Point(120, 240) Me.TextBoxdepartment.Name = "TextBoxdepartment" Me.TextBoxdepartment.Size = New System.Drawing.Size(296, 21) Me.TextBoxdepartment.TabIndex = 5 Me.TextBoxdepartment.Text = "" ' 'Button1 ' Me.Button1.Location = New System.Drawing.Point(32, 304) Me.Button1.Name = "Button1" Me.Button1.Size = New System.Drawing.Size(72, 40) Me.Button1.TabIndex = 6 Me.Button1.Text = "|<" ' 'Button2 ' Me.Button2.Location = New System.Drawing.Point(160, 304) Me.Button2.Name = "Button2" Me.Button2.Size = New System.Drawing.Size(72, 40) Me.Button2.TabIndex = 7 Me.Button2.Text = "<" ' 'Button3 ' Me.Button3.Location = New System.Drawing.Point(288, 304) Me.Button3.Name = "Button3" Me.Button3.Size = New System.Drawing.Size(72, 40) Me.Button3.TabIndex = 8 Me.Button3.Text = ">" ' 'Button4 ' Me.Button4.Location = New System.Drawing.Point(416, 296) Me.Button4.Name = "Button4" Me.Button4.Size = New System.Drawing.Size(72, 40) Me.Button4.TabIndex = 9 Me.Button4.Text = ">|" ' 'Form1 ' Me.AutoScaleBaseSize = New System.Drawing.Size(6, 14) Me.ClientSize = New System.Drawing.Size(560, 381) Me.Controls.Add(Me.Button4) Me.Controls.Add(Me.Button3) Me.Controls.Add(Me.Button2) Me.Controls.Add(Me.Button1) Me.Controls.Add(Me.TextBoxdepartment) Me.Controls.Add(Me.DateTimePickerbirthday) Me.Controls.Add(Me.NumericUpDownsalary) Me.Controls.Add(Me.TextBoxaddress) Me.Controls.Add(Me.TextBoxname) Me.Controls.Add(Me.TextBoxid) Me.Name = "Form1" Me.Text = "数据绑定" CType(Me.NumericUpDownsalary, System.ComponentModel.ISupportInitialize).EndInit() Me.ResumeLayout(False)

End Sub

#End Region

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load '设定窗体的最小尺寸 Me.MinimumSize = New Size(496, 400) '建立一个连接字符串 Dim strConnection As String = "Server=(local)\CHEN;Database=北风贸易;user id=sa;password=95960" '建立一个查询命令字符串 Dim strSql As String = "SELECT身份证号码,姓名,家庭住址,出生日期,当前工资,部门 FROM飞狐工作室" '建立一个数据连接 . Dim myConnection As SqlConnection = New SqlConnection(strConnection) '建立一个数据适配器以便对数据源执行SELECT '表达式来提取出要填入数据集的数据记录 Dim myCommand As SqlDataAdapter = New SqlDataAdapter(strSql, myConnection)

'将数据填入数据集 myCommand.Fill(ds, "飞狐工作室") '将TextBox控件的Text属性和数据集ds内的 ' ct飞狐工作室”表的“身份证号码”字段进行绑定 TextBoxid.DataBindings.Add("Text", ds, "飞狐工作室.身份证号码") '将TextBox控件的Text属性和数据集ds里的 ' “飞狐工作室”表的“姓名”字段进行绑定 TextBoxname.DataBindings.Add("Text", ds, "飞狐工作室.姓名") '将TextBox控件的Text属性和数据集ds里的 ' “飞狐工作室”表的“家庭住址”字段绑定 TextBoxaddress.DataBindings.Add("Text", ds, "飞狐工作室.家庭住址") '将DateTimePicker控件的Value属性和数据集ds里的 ' “飞狐工作室”表的“出生日期”字段绑定 DateTimePickerbirthday.DataBindings.Add("Value", ds, "飞狐工作室.出生日期") '将NumericUpDown控件的Value属性和数据集ds里的 ' “飞狐工作室”表的“当前工资”字段绑定 NumericUpDownSalary.DataBindings.Add("Value", ds, "飞狐工作室.当前工资") '将TextBox控件的Text属性和数据集ds里的 ' “飞狐工作室”表的“部门”字段绑定 TextBoxdepartment.DataBindings.Add("Text", ds, "飞狐工作室.部门") '将TextBox控件的Text属性和数据集ds里的 '关闭对数据库的连接 myConnection.Close() End Sub

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click Me.BindingContext(ds, "飞狐工作室").Position = 0 End Sub

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click If Me.BindingContext(ds, "飞狐工作室").Position > 0 Then Me.BindingContext(ds, "飞狐工作室").Position -= 1 End If End Sub

Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click ' If Me.BindingContext(ds, "飞狐工作室").Position < Me.BindingContext(ds, "飞狐工作室") .count-1 Then ' Me.BindingContext(ds, "飞狐工作室").Position += 1 ' End If End Sub

Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click Me.BindingContext(ds, "飞狐工作室").Position = Me.BindingContext(ds, "飞狐工作室").Count - 1 End Sub End Class 这是编译的结果 “DefaultDomain”: 已加载“c:\winnt\microsoft.net\framework\v1.1.4322\mscorlib.dll”,未加载符号。 “WindowsApplication7”: 已加载“G:\My Documents\Visual Studio Projects\WindowsApplication7\bin\WindowsApplication7.exe”,符号已加载。 “WindowsApplication7.exe”: 已加载“c:\winnt\assembly\gac\system.windows.forms\1.0.5000.0__b77a5c561934e089\system.windows.forms.dll”,未加载符号。 “WindowsApplication7.exe”: 已加载“c:\winnt\assembly\gac\system\1.0.5000.0__b77a5c561934e089\system.dll”,未加载符号。 “WindowsApplication7.exe”: 已加载“c:\winnt\assembly\gac\system.data\1.0.5000.0__b77a5c561934e089\system.data.dll”,未加载符号。 “WindowsApplication7.exe”: 已加载“c:\winnt\assembly\gac\system.xml\1.0.5000.0__b77a5c561934e089\system.xml.dll”,未加载符号。 “WindowsApplication7.exe”: 已加载“c:\winnt\assembly\gac\system.drawing\1.0.5000.0__b03f5f7f11d50a3a\system.drawing.dll”,未加载符号。 “WindowsApplication7.exe”: 已加载“c:\winnt\assembly\gac\system.enterpriseservices\1.0.5000.0__b03f5f7f11d50a3a\system.enterpriseservices.dll”,未加载符号。 “WindowsApplication7.exe”: 已加载“c:\winnt\assembly\gac\system.enterpriseservices\1.0.5000.0__b03f5f7f11d50a3a\system.enterpriseservices.thunk.dll”,未加载符号。 “WindowsApplication7.exe”: 已加载“c:\winnt\assembly\gac\mscorlib.resources\1.0.5000.0_zh-chs_b77a5c561934e089\mscorlib.resources.dll”,未加载符号。 “WindowsApplication7.exe”: 已加载“c:\winnt\assembly\gac\system.data.resources\1.0.5000.0_zh-chs_b77a5c561934e089\system.data.resources.dll”,未加载符号。 未处理的“System.Data.SqlClient.SqlException”类型的异常出现在 system.data.dll 中。

其他信息: 系统错误。

“WindowsApplication7.exe”: 已加载“c:\winnt\assembly\gac\microsoft.visualbasic\7.0.5000.0__b03f5f7f11d50a3a\microsoft.visualbasic.dll”,未加载符号。

未处理的异常: System.Data.SqlClient.SqlException: SQL Server 不存在或访问被拒绝。 at System.Data.SqlClient.ConnectionPool.GetConnection(Boolean& isInTransaction) at System.Data.SqlClient.SqlConnectionPoolManager.GetPooledConnection(SqlConnectionString options, Boolean& isInTransaction) at System.Data.SqlClient.SqlConnection.Open() at System.Data.Common.DbDataAdapter.QuietOpen(IDbConnection connection, ConnectionState& originalState) at System.Data.Common.DbDataAdapter.FillFromCommand(Object data, Int32 startRecord, Int32 maxRecords, String srcTable, IDbCommand command, CommandBehavior behavior) at System.Data.Common.DbDataAdapter.Fill(DataSet dataSet, Int32 startRecord, Int32 maxRecords, String srcTable, IDbCommand command, CommandBehavior behavior) at System.Data.Common.DbDataAdapter.Fill(DataSet dataSet, String srcTable) at WindowsApplication7.Form1.Form1_Load(Object sender, EventArgs e) in G:\My Documents\Visual Studio Projects\WindowsApplication7\Form1.vb:line 174 at System.Windows.Forms.Form.OnLoad(EventArgs e) at System.Windows.Forms.Form.OnCreateControl() at System.Windows.Forms.Control.CreateControl(Boolean fIgnoreVisible) at System.Windows.Forms.Control.CreateControl() at System.Windows.Forms.Control.WmShowWindow(Message& m) at System.Windows.Forms.Control.WndProc(Message& m) at System.Windows.Forms.ScrollableControl.WndProc(Message& m) at System.Windows.Forms.ContainerControl.WndProc(Message& m) at System.Windows.Forms.Form.WmShowWindow(Message& m) at System.Windows.Forms.Form.WndProc(Message& m) at System.Windows.Forms.ControlNativeWindow.OnMessage(Message& m) at System.Windows.Forms.ControlNativeWindow.WndProc(Message& m) at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam) at System.Windows.Forms.SafeNativeMethods.ShowWindow(HandleRef hWnd, Int32 nCmdShow) at System.Windows.Forms.Control.SetVisibleCore(Boolean value) at System.Windows.Forms.Form.SetVisibleCore(Boolean value) at System.Windows.Forms.Control.set_Visible(Boolean value) at System.Windows.Forms.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context) at System.Windows.Forms.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context) at System.Windows.Forms.Application.Run(Form mainForm) at WindowsApplication7.Form1.Main() in G:\My Documents\Visual Studio Projects\WindowsApplication7\Form1.vb:line 2程序“[2148] WindowsApplication7.exe”已退出,返回值为 0 (0x0)。

本人打算用VB.NET做毕业设计 十分感谢

搜索更多相关主题的帖子: 数据库 NET 联接 
2005-04-22 22:34



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-16419-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.570478 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved