System.Data.dll中发生了类型为“System.Data.SqlClient.SqlException”的未处理异常

时间:2013-12-05 01:52:49

标签: sql-server vb.net

我正在尝试运行这个程序,但它给了我一个错误,我检查了所有的服务器名称,数据库名称,但我不知道我在哪里犯了一个错误。

请帮忙解决这个问题

Imports System.Data
Imports System.Data.SqlClient
Public Class Form1
    Dim con As New SqlConnection("Server=PAULIN\LARIE4; database=TermPaper;")
    Dim da As New SqlDataAdapter()
    Dim dt As New DataTable
    Dim ds As New DataSet

Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    ld()


End Sub
Private Sub clearControls()
    txtN.Text = ""
    txtP.Text = ""

End Sub
Private Sub clrbinding()
    ds.Tables(0).Rows.Clear()
    dt.Rows.Clear()
    DataGridView1.DataSource = Nothing

End Sub
Private Sub ld()
    da.SelectCommand = New SqlCommand
    da.SelectCommand.Connection = New SqlConnection
    da.SelectCommand.CommandText = "select *from termpaper1"
    da.SelectCommand.CommandType = CommandType.Text
    con.Open()
    da.Fill(ds, "termpaper1")
    con.Close()
    dt = ds.Tables("termpaper1")
    DataGridView1.DataSource = dt
End Sub
Private Sub btnInser_Click(sender As Object, e As EventArgs) Handles btnInser.Click
    clearControls()
    clrbinding()


End Sub

Private Sub btnSave_Click(sender As Object, e As EventArgs) Handles btnSave.Click
    da.InsertCommand = New SqlCommand
    da.InsertCommand.Connection = New SqlConnection
    da.InsertCommand.CommandText = "insert into termpaper1(user_name,password)values('" & txtN.Text & "','" & txtP.Text & "')"
    da.InsertCommand.CommandType = CommandType.Text
    con.Open()
    da.InsertCommand.ExecuteNonQuery()
    MsgBox("one record added")
    clrbinding()
    ld()

End Sub
End Class

1 个答案:

答案 0 :(得分:0)

您的连接字符串缺少必要的信息...您必须将IntegratedSecurity设置为true(并确保您的Windows用户具有访问数据库的权限)或提供sql用户名和密码...我建议使用SqlConnectionStringBuilder创建有效的sql连接字符串