2016-04-21 137 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

完美工作,謝謝! –

相關問題