使用VB.Net将数据插入SQL Server数据库 [英] Inserting data into SQL Server database using VB.Net

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

问题描述

我目前正在使用 HDI Membership 提供程序,设计如下所示:

I am currently using HDI Membership provider and the design looks as shown below:

现在我正在尝试创建一个新用户并将这些值插入到数据库中,如下所示:

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.

解决方法是提供您要插入的列的名称.

The fix is to supply the names of the columns you are insert into.

屏幕截图还显示有一个必需的 ApplicationName 列,因此除非它定义了 DEFAULT,否则您也需要提供它.

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:

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

这篇关于使用VB.Net将数据插入SQL Server数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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