在数据库的ASP.NET插入新的价值 [英] Inserting new values in database Asp .Net

查看:94
本文介绍了在数据库的ASP.NET插入新的价值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个code使用VB在ASP.net插入值。我有问题,我的code称登录失败,无法打开数据库。

 暗淡strUser的,strpass,stremail作为字符串
            strUser的= TextBox1.Text
            strpass = TextBox2.Text
            stremail = TextBox4.Text
            声明SQL连接。            昏暗thisConnection作为新的SqlConnection(ConfigurationManager.ConnectionStrings(的DatabaseConnection)。ConnectionString中)            '创建Command对象
            昏暗nonqueryCommand作为的SqlCommand = thisConnection.CreateCommand()
            尝试
                打开连接
                thisConnection.Open()                昏暗strcommand作为字符串
                strcommand =插入到账户(用户名,密码,电子邮件)VALUES('+ strUser的+,+ strpass +,+ stremail +')                昏暗sqlcomm作为新的SqlCommand(strcommand,thisConnection)
                昏暗O作为字符串= sqlcomm.ExecuteNonQuery()
            抓住EX作为的SQLException
                '显示错误
                MSGBOX(ex.ToString())
            最后
                关闭连接
                MSGBOX(成功)
                thisConnection.Close()
            结束Try

连接字符串:

 <添加名称=的DatabaseConnection的connectionString =数据源= \\ SQLEX $ P $干燥综合征;初始目录= o2database.mdf;集成安全性= SSPI的providerName =系统.Data.SqlClient/>


解决方案

  

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


  
  

2)你可以使用服务器资源管理器'和;尝试只连接到数据库
  从那里。一旦得手刚才复制的连接字符串
  性能和放大器;替换当前的连接字符串。


I have a code for inserting values in ASP.net using vb. I'm having problem with my code says login failed, cannot open database.

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

connection string:

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

解决方案

1) Initial catalog must be name of the schema you are accessing

2) You may use 'Server Explorer' & try to just connect to the database from there. Once succeeded just copy the connection string from properties & replace your current connection string.

这篇关于在数据库的ASP.NET插入新的价值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆