小编典典

在数据库Asp .Net中插入新值

sql

我有一个使用vb在ASP.net中插入值的代码。我的代码有问题,提示登录失败,无法打开数据库。

Dim struser, strpass, stremail As String
            struser = TextBox1.Text
            strpass = TextBox2.Text
            stremail = TextBox4.Text
            'declaring sql connection.

            Dim thisConnection As New SqlConnection(ConfigurationManager.ConnectionStrings("DatabaseConnection").ConnectionString)

            'Create Command object
            Dim nonqueryCommand As SqlCommand = thisConnection.CreateCommand()


            Try
                ' Open Connection
                thisConnection.Open()

                Dim strcommand As String
                strcommand = "Insert into Account (Username,Password, Email) values ('" + struser + "','" + strpass + "','" + stremail + "')"

                Dim sqlcomm As New SqlCommand(strcommand, thisConnection)
                Dim o As String = sqlcomm.ExecuteNonQuery()


            Catch ex As SqlException
                ' Display error
                MsgBox(ex.ToString())
            Finally
                ' Close Connection
                MsgBox("Success")
                thisConnection.Close()


            End Try

连接字符串:

<add name="DatabaseConnection" connectionString="Data Source=.\SQLEXPRESS;Initial Catalog=o2database.mdf;Integrated Security=SSPI" providerName="System.Data.SqlClient"/>

阅读 171

收藏
2021-03-17

共1个答案

小编典典

1)初始目录必须是您正在访问的架构的名称

2)您可以使用“服务器资源管理器”并尝试仅从那里连接到数据库。一旦成功,只需从属性中复制连接字符串并替换您当前的连接字符串即可。

2021-03-17