Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/36.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql 将用户输入保存到数据库表asp.net_Sql_Asp.net_Database_Vb.net - Fatal编程技术网

Sql 将用户输入保存到数据库表asp.net

Sql 将用户输入保存到数据库表asp.net,sql,asp.net,database,vb.net,Sql,Asp.net,Database,Vb.net,我正在做一个学校项目,我们应该将用户输入的数据保存到我们在项目中创建的数据库中。我在我的项目的App_Data文件夹中创建了一个名为“db1”的数据库,并创建了一个名为“Video_Games”的表。我的代码有点像弗兰肯斯坦的怪物,它来自我的教科书中的示例以及在线示例 Protected Sub btnTable_Click(sender As Object, e As EventArgs) Handles btnTable.Click Dim strTitle As String =

我正在做一个学校项目,我们应该将用户输入的数据保存到我们在项目中创建的数据库中。我在我的项目的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=newsqlconnection”行有关,但我找不到如何使它工作。感谢您提供的任何帮助。

从中获取连接字符串。
您可以使用可信连接或标准安全性。当您使用mssql安装定义的用户日志记录混合模式或正常模式时。混合模式意味着使用用户名和密码

我想出来了,实际上采取了完全不同的方向;我使用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
Protected Sub btnDisplay\u单击(ByVal sender作为对象,ByVal e作为System.EventArgs)
Dim STRITLE As String=txtTitle.Text
Dim strConsole As String=txtConsole.Text
Dim strYear As String=txtYear.Text
Dim strESRB As String=txtRating.Text
Dim strScore作为字符串=txtScore.Text
Dim strPublisher As String=txtPublisher.Text
Dim strDeveloper As String=txtDeveloper.Text
Dim strGenre As String=txtgree.Text
Dim strPurchase As String=calDate.SelectedDate.ToString
Dim strDate As Date=calDate.SelectedDate
Dim intYear作为整数=CInt(strYear)
如果标准日期>日期。那么今天
Response.Write(“警报('Error:所选日期必须是今天或更早”))
其他的
lblDisp2.Text=“数据保存到数据库”
Dim连接为String=“Provider=Microsoft.ACE.OLEDB.12.0;数据源=| DataDirectory |\db1.accdb”
Dim query As String=“插入[视频游戏](标题、控制台、发布年份、ESRB、分数、出版商、开发者、流派、购买)
值(@strTitle、@strConsole、@strYear、@strESRB、@strScore、@strPublisher、@strDeveloper、@strGenre、@strPurchase)
尺寸控制为新的OLEDB连接(连接)
Dim cmd作为新的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()
作为OleDbDataReader=cmd.ExecuteReader()的Dim dr
Dim dt作为新数据表()
dt.负载(dr)
con.Close()
如果结束
端接头

您的计算机上是否有名为“SQLEXPRESS”的SQL Server Express实例?如果您刚刚从某个地方复制了一个连接字符串,那么它可能对您的环境无效;它没有给出太多的解释,所以我不知道如何正确使用它。当你创建数据库时,你是如何连接的?您可以尝试使用LocalDB,它与最新版本的Visual Studio一起安装。也许像
conn=newsqlconnection(“数据源=(LocalDB)\v11.0;AttachDbFilename=| DataDirectory |\db1;Integrated Security=True;”)这样的东西可以工作(取决于您在PC上安装/配置了什么)。我只需右键单击App_Data文件夹,进入Add,然后是SQL Server数据库。这就是我在网上找到的关于如何向项目中添加数据库的内容。我尝试了LocalDB行,但它仍然不想工作。