【发布时间】: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