古文字学导论pdf:vb.net与SQL连接的问题

来源:百度文库 编辑:杭州交通信息网 时间:2024/04/30 14:53:20
我弄了个管理系统,因为各个窗体都需要对数据库进行访问,我把他们做了一个类来实现,但是运行程序的时候,就没有了反映,VB提示我是如下:
未处理的“System.Data.SqlClient.SqlException”类型的异常出现在 system.data.dll 中。

其他信息: 系统错误
然后停在了 SqlConn.Open()这里
请教一下高手们,是怎么回事?
Imports System.Data
Imports System.Data.SqlClient
Imports System.ComponentModel
Public Class DataBase
'实现接口IDisposable
Implements IDisposable
'数据库连接对象
Private SqlConn As SqlConnection
Public Shared sConn As String = "Persist Security Info=False;Integrated Security=SSPI;database=hisbook;server=localhost;Connect Timeout=30"

Public Sub Dispose() Implements System.IDisposable.Dispose
Dispose(True)
GC.SuppressFinalize(True)
End Sub
Protected Sub Dispose(ByVal disposing As Boolean)
If disposing <> True Then
Return
End If

If SqlConn Is Nothing = False Then
SqlConn.Dispose()
SqlConn = Nothing
End If
End Sub
Public Sub Open()
If SqlConn Is Nothing = True Then
'建立数据库连接对象
SqlConn = New SqlConnection(Me.sConn)
'打开数据库连接
SqlConn.Open()
End If
End Sub
Public Sub Close()
'如果数据库连接对象不为空则关闭数据库连接
If SqlConn Is Nothing = False Then
SqlConn.Close()
End If
End Sub
Public Function RunSelectSQL(ByVal sSQLString As System.String) As DataView
Me.Open()
Dim SqlDS As DataSet = New DataSet
Dim SqlDA As SqlDataAdapter = New SqlDataAdapter(sSQLString, Me.SqlConn)
SqlDA.Fill(SqlDS)
Return SqlDS.Tables(0).DefaultView
End Function
Public Function RunDelOrInsSQL(ByVal sSQLString As System.String)
Me.Open()
Dim SqlComm As SqlCommand = New SqlCommand(sSQLString, Me.SqlConn)
SqlComm.ExecuteNonQuery()
End Function
End Class

using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Data.SqlClient;

namespace WindowsApplication3
{
/// <summary>
/// Form1 的摘要说明。
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.DataGrid dataGrid1;
private System.Windows.Forms.Button button1;
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.Container components = null;

public Form1()
{
//
// Windows 窗体设计器支持所必需的
//
InitializeComponent();

//
// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
//
}

/// <summary>
/// 清理所有正在使用的资源。
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}

#region Windows 窗体设计器生成的代码
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.dataGrid1 = new System.Windows.Forms.DataGrid();
this.button1 = new System.Windows.Forms.Button();
((System.ComponentModel.ISupportInitialize)(this.dataGrid1)).BeginInit();
this.SuspendLayout();
//
// dataGrid1
//
this.dataGrid1.DataMember = "";
this.dataGrid1.HeaderForeColor = System.Drawing.SystemColors.ControlText;
this.dataGrid1.Location = new System.Drawing.Point(16, 8);
this.dataGrid1.Name = "dataGrid1";
this.dataGrid1.Size = new System.Drawing.Size(304, 304);
this.dataGrid1.TabIndex = 0;
//
// button1
//
this.button1.Location = new System.Drawing.Point(352, 24);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(136, 40);
this.button1.TabIndex = 1;
this.button1.Text = "button1";
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(528, 333);
this.Controls.Add(this.button1);
this.Controls.Add(this.dataGrid1);
this.Name = "Form1";
this.Text = "Form1";
this.Load += new System.EventHandler(this.Form1_Load);
((System.ComponentModel.ISupportInitialize)(this.dataGrid1)).EndInit();
this.ResumeLayout(false);

}
#endregion

/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
}

private void Form1_Load(object sender, System.EventArgs e)
{
SqlConnection Conn=new SqlConnection("User ID=sa;Initial Catalog=Northwind;Data Source=localHost");
SqlDataAdapter da=new SqlDataAdapter("Select * from Customers",Conn);
DataSet dt =new DataSet();
da.Fill(dt,"Customers");
dataGrid1.SetDataBinding(dt,"Customers");
}
}
}