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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20389906/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 16:01:33  来源:igfitidea点击:

An unhandled exception of type 'System.Data.SqlClient.SqlException' occurred in System.Data.dll

sql-servervb.net

提问by larie

I'm trying to run this program but it gave me an error, I have checked everything like server name, database name, but I don't know where I made a mistake.

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

Please help out to solve this problem

请帮忙解决这个问题

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

回答by PrfctByDsgn

your connection string is missing necessary information ... you have to either set IntegratedSecurity to true (and make sure that your windows user has permissions to access the database) or provide sql user name and password ... I would suggest to use SqlConnectionStringBuilder to create a valid sql connection string

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