Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.1k views
in Technique[技术] by (71.8m points)

asp.net - Inserting new values in database Asp .Net

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"/>
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

I think your Initial Catalog is wrong. your pointing at a file you should use here the database-name. I guess o2database.

if this is not the case - you are using SSPI to login - maybe your user does not have the permission to do so.

another thing is that your web-application is not configured in the iis to pass on your domain-user credentials - so it cannot work using SSPI to login.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...