【问题标题】:How to save data to the sql database using SqlCommand如何使用 SqlCommand 将数据保存到 sql 数据库
【发布时间】:2012-07-18 02:09:19
【问题描述】:

我是 Visual Studio 和 SQL Server 的新手。我对如何从 Visual Studio 应用程序插入数据感到困惑

这是我的代码:

试试

'===============Add New Patient===================
        patient_name = txtName.Text
        patient_age = nudAge.Value
        date_of_confinement = dtpDate.Value
        type_of_sickness = txtSickness.Text
        type_of_IVfluid = txtFluid.Text
        number_of_bottles = txtBottle.Text
        drop_rate = nudDrop.Value

        'To check if all values have been filled up
        If txtName.Text = "" Or nudAge.Value = "" Or dtpDate.Value = "" _
        Or txtSickness.Text = "" Or txtFluid.Text = "" Or txtBottle.Text = "" Or nudDrop.Value = "" _
        Then

            MsgBox("Please Complete All the Required Fields")

        Else
            Try

                Dim PatientInfoConnection As SqlConnection = New _
                SqlConnection("Server=CATH-PC; database=PatientInfoDB;user id=sa;password=*******") 'connection to SQL database

                PatientInfoConnection.Open() 'open database connection

                Dim sql As String = ""

                sql = "insert into model (name, age, date_of_confinement,type_of_sickness, type_of_IVfluid, number_of_bottles, drop_rate)" & _
                       " values (@txtName.Text, @nudAge.Value, @dtpDate.Value, @txtSickness.Text, @txtFluid.Text, @txtBottle.Text, @nudDrop.Value)"

                Dim insert As New SqlCommand(sql, PatientInfoConnection)

                insert.Parameters.AddWithValue(patient_name, @txtName.Text)'@ error  
                insert.Parameters.AddWithValue("val2", nudAge.Value) 'error
                insert.Parameters.AddWithValue(Name, patient_name) 'not sure

                insert.ExecuteNonQuery()

                MsgBox("Successfully Saved")
                Me.Visible = False
                Mainform.Show()


            Catch myerror As MySqlException
                MessageBox.Show("Error Connecting to Database: " & myerror.Message & ". Please contact the operator")


            End Try

        End If

    Catch ex As Exception

    End Try

我不确定SqlCommand,因为我不知道如何将用户的输入保存到我的数据库中。我想将文本框中输入的值添加到我的数据库中

Dim insert As New SqlCommand(sql, PatientInfoConnection)

insert.Parameters.AddWithValue(patient_name, @txtName.Text) 'error @ char is not valid

insert.Parameters.AddWithValue("val2", nudAge.Value) 'error

insert.Parameters.AddWithValue(Name, patient_name) 'not error but not sure

name(text,null) 是我在 SQL Server Management Studio 中设计的数据库的列之一

请帮助我。任何帮助,将不胜感激。谢谢!

我更改了insert.Parameters.AddWithValue("@name", txtName.Text) 和 sql = "按照建议插入模型部分,但出现错误

“'System.InvalidCastException' 类型的第一次机会异常 发生在 Microsoft.VisualBasic.dll"

我很困惑

【问题讨论】:

    标签: sql vb.net sqlcommand


    【解决方案1】:

    你很接近。

    尝试只使用一个变量名,然后匹配它们。

    所以 SQL 应该是:

    sql = "insert into model (name, age, date_of_confinement,type_of_sickness, type_of_IVfluid, number_of_bottles, drop_rate)" & _
                       " values (@name, @age, @dateOfConfinement, @sicknessType, @fluidType, @bottleAmount, @dropRate)"
    

    然后设置参数如:

    insert.Parameters.AddWithValue("@name", txtName.Text)
    

    【讨论】:

    • 我收到错误消息“Microsoft.VisualBasic.dll 中发生了“System.InvalidCastException”类型的第一次机会异常”我很困惑。
    • 您可能试图传递无效类型,或者与数据库值不一致的类型。您可以尝试使用“toString()”设置值,最佳做法是为参数指定 DbType,然后验证值类型是否与您的数据库表相同(int 到 int、varchar/char到字符串)
    • 以下是一些工作示例:msdn.microsoft.com/en-us/library/…
    猜你喜欢
    • 1970-01-01
    • 2015-08-04
    • 1970-01-01
    • 1970-01-01
    • 2013-08-03
    • 2014-04-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多