简体   繁体   English

使用VB.Net将数据插入SQL Server数据库

[英]Inserting data into SQL Server database using VB.Net

I am currently using HDI Membership provider and the design looks as shown below: 我目前正在使用HDI会员提供商,设计如下所示:

在此输入图像描述

Now I am trying to create a new user and insert those values into the database as shown below: 现在我正在尝试创建一个新用户并将这些值插入数据库,如下所示:

Try
   Dim connectionString As String = "Data Source=.\sqlexpress;Initial Catalog=HDIMembershipProvider;Integrated Security=True"
   Using cn As New SqlConnection(connectionString)
      cn.Open()
      Dim cmd As New SqlCommand()
      cmd.CommandText = "INSERT INTO Users VALUES(@Username,@Password,@Email,@PasswordQuestion,@PasswordAnswer)"

      Dim param1 As New SqlParameter()
      param1.ParameterName = "@Username"
      param1.Value = txtUsername.Text.Trim()
      cmd.Parameters.Add(param1)

      Dim param2 As New SqlParameter()
      param2.ParameterName = "@Password"
      param2.Value = txtPassword.Text.Trim()
      cmd.Parameters.Add(param2)

      Dim param3 As New SqlParameter()
      param3.ParameterName = "@Email"
      param3.Value = txtEmail.Text.Trim()
      cmd.Parameters.Add(param3)

      Dim param4 As New SqlParameter()
      param4.ParameterName = "@PasswordQuestion"
      param4.Value = txtSecurityQuestion.Text.Trim()
      cmd.Parameters.Add(param4)

      Dim param5 As New SqlParameter()
      param5.ParameterName = "@PasswordAnswer"
      param5.Value = txtSecurityAnswer.Text.Trim()
      cmd.Parameters.Add(param5)

      cmd.Connection = cn
      cmd.ExecuteNonQuery()
      cn.Close()
   End Using
   Successlbl.show
   Successlbl.show.Text = "Regisration Success."
Catch
   Errolbl.Show()
   Errolbl.Text = "Your account was not created.Please try again."
End Try

Now the problem is the data is not inserting to the database. 现在问题是数据没有插入数据库。 I would like to know If anyone can point me where I'm going wrong? 我想知道如果有人能指出我哪里出错了?

Your insert statement is incorrect - since you are not specifying any field names you should be supplying values for all columns. 您的insert语句不正确 - 因为您没有指定任何字段名称,所以您应该为所有列提供值。

The fix is to supply the names of the columns you are insert into. 修复是提供要插入的列的名称。

The screenshot also shows that there is a required ApplicationName column, so unless it has a DEFAULT defined, you will need to supply that as well. 屏幕截图还显示有一个必需的ApplicationName列,因此除非定义了DEFAULT ,否则您还需要提供它。

Assuming you have a DEFAULT defined on ApplicationName : 假设您在ApplicationName定义了DEFAULT

cmd.CommandText = "INSERT INTO Users ( Username, Password, Email, PasswordQuestion, PasswordAnswer) VALUES(@Username,@Password,@Email,@PasswordQuestion,@PasswordAnswer)"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM