2016-04-21 136 views
-1

当试图将以下信息插入到我的数据库中时,我得到“System.Data.dll中发生类型'System.Data.SqlClient.SqlException'的异常,但未在用户代码错误中处理?”错误。在System.Data.dll中发生类型'System.Data.SqlClient.SqlException'的异常,但未在用户代码错误中处理?

我的代码如下:

Imports System.Data.SqlClient 
Partial Class SecurePages_AddBackup 
Inherits System.Web.UI.Page 

Protected Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click 

    Dim conn As SqlConnection 
    Dim cmd As SqlCommand 

    Dim customer As Integer 
    Dim backupDate As String 
    Dim Server As Integer 
    Dim Status As String 
    Dim Product As Integer 
    Dim Details As String 

    customer = ddlCustomer.SelectedValue 
    backupDate = clDate.SelectedDate 
    Server = DDLserver.SelectedValue 
    Status = DDLStatus.Text 
    Product = DDLproduct.SelectedValue 
    Details = txtDetails.Text 




    Dim cmdstring As String = "INSERT INTO Backup(CustomerID, Date, ServerID, Status, ProductID, Details) Values (@Customer, @BackupDate, @Server, @Status, @Product, @Details)" 

    conn = New SqlConnection("Data Source=(LocalDB)\v11.0;AttachDbFilename=|DataDirectory|\Backups.mdf;Integrated Security=True") 
    cmd = New SqlCommand(cmdstring, conn) 

    cmd.Parameters.AddWithValue("@Customer", customer) 
    cmd.Parameters.AddWithValue("@BackupDate", backupDate) 
    cmd.Parameters.AddWithValue("@Server", Server) 
    cmd.Parameters.AddWithValue("@Status", Status) 
    cmd.Parameters.AddWithValue("@Product", Product) 
    cmd.Parameters.AddWithValue("@Details", Details) 
    conn.Open() 

    cmd.ExecuteNonQuery() 




    conn.Close() 

    lblStatus.Text = "Backup added to the database!" 
End Sub 
End Class 

我不知道我要去的地方错了。

+0

您在项目或SQL Express或SQL Server中使用.MDF文件吗?如果您使用express或sql server ...这是一个连接字符串格式,可以帮助您。 数据源=您的服务器名称\ SQLEXPRESS; Initial Catalog = YOUR-DB-NAME;“Integrated Security = True providerName =”System.Data.SqlClient“ –

+0

我在我的项目中使用了一个MDF文件,它在'cmd.ExecuteNonQuery() –

+0

当程序崩溃时,我在浏览器中得到以下错误在关键字'Backup'附近有错误的语法,但仍然不确定错误代码在关键字'Backup'附近的位置? –

回答

0

Backup是SQL Server中的reserved keyword

尝试INSERT INTO [Backup]

+1

完美工作,谢谢! –

相关问题