我正在开展一个学校项目,我们应该将用户输入的数据保存到我们在项目中创建的数据库中。我在项目的App_Data文件夹中创建了一个名为“db1”的数据库,并创建了一个名为“Video_Games”的表。我的代码有点像弗兰肯斯坦的怪物,它来自我的教科书和在线例子。
Protected Sub btnTable_Click(sender As Object, e As EventArgs) Handles btnTable.Click
Dim strTitle As String = txtTitle.Text
Dim strConsole As String = txtConsole.Text
Dim strYear As String = txtYear.Text
Dim strESRB As String = txtRating.Text
Dim strScore As String = txtScore.Text
Dim strPublisher As String = txtPublisher.Text
Dim strDeveloper As String = txtDeveloper.Text
Dim strGenre As String = txtGenre.Text
Dim strPurchase As String = calDate.SelectedDate.ToString
Dim conn As SqlConnection
Dim cmd As SqlCommand
Dim cmdString As String = "INSERT INTO Video_Games(Title, Console, Year, ESRB, Score, Publisher, Developer, Genre, Purchase)
VALUES (@strTitle, @strConsole, @strYear, @strESRB, @strScore, @strPublisher, @strDeveloper, @strGenre, @strPurchase)"
conn = New SqlConnection("Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\db1;Integrated Security=True;User Instance=True")
cmd = New SqlCommand(cmdString, conn)
cmd.Parameters.AddWithValue("@strTitle", strTitle)
cmd.Parameters.AddWithValue("@strConsole", strConsole)
cmd.Parameters.AddWithValue("@strYear", strYear)
cmd.Parameters.AddWithValue("@strESRB", strESRB)
cmd.Parameters.AddWithValue("@strScore", strScore)
cmd.Parameters.AddWithValue("@strPublisher", strPublisher)
cmd.Parameters.AddWithValue("@strDeveloper", strDeveloper)
cmd.Parameters.AddWithValue("@strGenre", strGenre)
cmd.Parameters.AddWithValue("@strPurchase", strPurchase)
conn.Open()
cmd.ExecuteNonQuery()
conn.Close()
End Sub
我得到的错误来自“conn.Open()”行是: System.Data.dll中发生了'System.Data.SqlClient.SqlException'类型的异常,但未在用户代码中处理
其他信息:建立与SQL Server的连接时发生与网络相关或特定于实例的错误。服务器未找到或无法访问。验证实例名称是否正确以及SQL
根据错误判断,我假设它与我的'conn = New SqlConnection'行有关,但我找不到如何使其工作。感谢您提供任何帮助。
答案 0 :(得分:0)
从https://www.connectionstrings.com获取连接字符串。 您可以使用可信连接或标准安全性。当你安装mssql时定义用户登录混合模式或正常。混合模式表示使用用户名和密码
答案 1 :(得分:0)
我想通了,实际上采取了完全不同的方向;我去了Access数据库溃败而不是SQL。谢谢大家的帮助和支持。我使用的代码是:
Protected Sub btnDisplay_Click(ByVal sender As Object, ByVal e As System.EventArgs)
Dim strTitle As String = txtTitle.Text
Dim strConsole As String = txtConsole.Text
Dim strYear As String = txtYear.Text
Dim strESRB As String = txtRating.Text
Dim strScore As String = txtScore.Text
Dim strPublisher As String = txtPublisher.Text
Dim strDeveloper As String = txtDeveloper.Text
Dim strGenre As String = txtGenre.Text
Dim strPurchase As String = calDate.SelectedDate.ToString
Dim strDate As Date = calDate.SelectedDate
Dim intYear As Integer = CInt(strYear)
If strDate > Date.Today Then
Response.Write("<script>alert('Error: The selected date must be today or earlier')</script>")
Else
lblDisp2.Text = "Data Saved to Database"
Dim connection As String = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=|DataDirectory|\db1.accdb"
Dim query As String = "INSERT INTO [Video_Games](Title, Console, Year_Released, ESRB, Score, Publisher, Developer, Genre, Purchase)
VALUES (@strTitle, @strConsole, @strYear, @strESRB, @strScore, @strPublisher, @strDeveloper, @strGenre, @strPurchase)"
Dim con As New OleDbConnection(connection)
Dim cmd As New OleDbCommand()
cmd.CommandText = query
cmd.CommandType = CommandType.Text
cmd.Connection = con
cmd.Parameters.AddWithValue("@strTitle", strTitle)
cmd.Parameters.AddWithValue("@strConsole", strConsole)
cmd.Parameters.AddWithValue("@strYear", strYear)
cmd.Parameters.AddWithValue("@strESRB", strESRB)
cmd.Parameters.AddWithValue("@strScore", strScore)
cmd.Parameters.AddWithValue("@strPublisher", strPublisher)
cmd.Parameters.AddWithValue("@strDeveloper", strDeveloper)
cmd.Parameters.AddWithValue("@strGenre", strGenre)
cmd.Parameters.AddWithValue("@strPurchase", strPurchase)
con.Open()
Dim dr As OleDbDataReader = cmd.ExecuteReader()
Dim dt As New DataTable()
dt.Load(dr)
con.Close()
End If
End Sub