【问题标题】:An unhandled exception of type 'System.Data.SqlClient.SqlException' occurred in System.Data.dll [duplicate]System.Data.dll 中出现“System.Data.SqlClient.SqlException”类型的未处理异常 [重复]
【发布时间】:2013-12-05 01:52:49
【问题描述】:

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

请帮忙解决这个问题

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

【问题讨论】:

  • “但它给了我一个错误” - 悬念正在杀死我!
  • 您是否尝试在 * 和 termpaper1 - select * from termpaper1 上放置一个空格
  • 是的,我做了,但仍然没有发生任何事情
  • 请报告任何错误信息。请调试您的工作 - 变量是否填充了数据?那么你的表格拼写正确了吗?
  • @DonA ,变量没有填充数据,当我尝试调试我的项目时发生错误。它显示一个指向“con.open()”的黄色箭头,并显示此消息System.Data.dll 中发生“System.Data.SqlClient.SqlException”类型的未处理异常

标签: sql-server vb.net


【解决方案1】:

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

【讨论】:

  • @ PrfctByDsgn - 好的,谢谢你,我会尝试,我会告诉你它是否工作,因为在我使用完整性安全性和初始目录之前,但它也没有工作......所以我想我将使用 sqlconnectionStringBuilder,或者可能是因为我使用的是 Windows 8
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-12-01
相关资源
最近更新 更多