简体   繁体   English

如何使用ASP.Net vb .mdf在数据库中插入数据

[英]How to insert data in database with ASP.Net vb .mdf

The add and insert function of my application doesn't work. 我的应用程序的添加和插入功能不起作用。 I have no idea what I did wrong. 我不知道我做错了什么。 Can you find any error? 你能找到任何错误吗? Please help! 请帮忙!

Here is my code: 这是我的代码:

Protected Sub btnConfirm_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnConfirm.Click



    Dim connString As String
    connString = "Data Source=.\SQLEXPRESS;AttachDbFilename=E:\\boomwebsite\App_Data\playaazaleaDatabase.mdf;Integrated Security=True;User Instance=True"
    Dim cmd As New SqlCommand

    Dim myConnection As New Data.SqlClient.SqlConnection(connString)
    Try
        myConnection.Open()

        cmd.CommandText = "INSERT INTO reservations(fname, lname, contactnumber, amenities, date, price, status, statuscount) VALUES (@FNAME, @LNAME, @CONTACT, @AMENITIES, @DATE, @PRICE, @STATUS, @STATUSCOUNT)"
        cmd.Connection = myConnection
        cmd.Parameters.AddWithValue("@FNAME", txtboxFname.Text)
        cmd.Parameters.AddWithValue("@LNAME", txtboxLname.Text)
        cmd.Parameters.AddWithValue("@CONTACT", txtboxContact.Text)
        cmd.Parameters.AddWithValue("@AMENITIES", DropDownListAmenities.SelectedItem.Value)
        cmd.Parameters.AddWithValue("@DATE", txtboxDate.Text)
        cmd.Parameters.AddWithValue("@PRICE", txtboxPrice.Text)
        cmd.Parameters.AddWithValue("@STATUS", txtboxStatus.text)
        cmd.Parameters.AddWithValue("@STATUS2", txtboxStatus2.text)

        cmd.ExecuteNonQuery()
        Response.Redirect("bookedreservations.aspx")

    Catch ex As Exception
        Label1.text = "Error Inserting Data ..."
    Finally
        myConnection.Close()
    End Try
End Sub

Well, having an exception handler that writes out .. "Error Inserting Data"... without telling you what is the error seems to be a little useless, at least write 好吧,拥有一个写出..“错误插入数据”的异常处理程序...而没有告诉你错误是什么,看来至少是没有用的,

Catch ex As Exception
    Label1.text = "Error Inserting Data ... " & ex.Message
...

that will show what's going wrong with your query. 这将显示您的查询出了什么问题。

However there is a big suspect in that query. 但是,该查询有很大的疑问。 The word Date is a reserved keyword in almost any database system. 日期一词是几乎所有数据库系统中的保留关键字。 If you want to use it then enclose in square brackets 如果要使用它,请用方括号括起来

cmd.CommandText = "INSERT INTO reservations(fname, lname, " & _ 
     "contactnumber, amenities, [date], price, status, statuscount) " & _
     "VALUES (@FNAME, @LNAME, @CONTACT, @AMENITIES, @DATE, " & _ 
     "@PRICE, @STATUS, @STATUSCOUNT)"

Another possible problem is the method AddWithValue. 另一个可能的问题是方法AddWithValue。 This method, while handy, has a dark side that you need to be fully aware when using it. 这种方法虽然方便,但在使用时需要充分注意其阴暗面。 It creates a parameter with a datatype that is assumed looking at the datatype of the value that you pass. 它会创建一个参数,该参数的数据类型假定为查看您传递的值的数据类型。 In your case, for example, the price field should be a nvarchar field because you pass a txtboxPrice.Text property that is a string. 例如,在您的情况下, price字段应为nvarchar字段,因为您传递的是字符串txtboxPrice.Text属性。
It is better to use 最好用

 cmd.Parameters.Add("@PRICE", SqlDbType.Decimal).Value = Convert.ToDecimal(txtboxPrice.Text)

and so on for the other parameters. 等等其他参数。

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

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