2012-01-08 108 views
1

我有下面的代碼,它執行沒有任何錯誤,甚至報告1行受到影響。然而,沒有什麼是保存的。爲什麼不? 我可以儘管數據庫檢索..但不能寫什麼都沒有插入數據庫?

Dim SQLConn As New SqlConnection() 'The SQL Connection 
SQLConn.ConnectionString = connstring 'Set the Connection String 
SQLConn.Open() 
Dim SQLCmd As New SqlCommand() 'The SQL Command 
Dim SQLStr As String = "INSERT into region_table VALUES(10,23,4,'test')" 
SQLCmd.CommandText = SQLStr 
sqlCmd.ExecuteNonQuery() 
SQLConn.Close() 

SOLUTION :: 打開右鍵單擊服務器資源管理器,選擇屬性數據庫連接,並使用該連接字符串!希望這能解決別人的問題太多:d

+1

你有和我一樣的問題嗎? http://stackoverflow.com/questions/7223223/sqlce-query-having-no-effect – Ryan 2012-01-08 18:25:21

+0

也許你需要提交交易? – Yaniro 2012-01-08 18:25:56

+0

你插入正確的數據庫嗎?你沒有副本在主人錯誤或任何你? – MatBailie 2012-01-08 18:31:21

回答

1

你是不是來分配連接到命令:

這一行後:

SQLCmd.CommandText = SQLStr 

加上這樣一條:

SQLCmd.Connection = SQLConn 

你還應該使用使用陳述,以確保一切正確清理:

Using SQLConn As New SqlConnection() 'The SQL Connection 
     SQLConn.ConnectionString = connstring 'Set the Connection String 
     SQLConn.Open() 
     Using SQLCmd As New SqlCommand() 'The SQL Command 
      Dim SQLStr As String = "INSERT into region_table VALUES(10,23,4,'test')" 
      SQLCmd.CommandText = SQLStr 
      SQLCmd.Connection = SQLConn 
      SQLCmd.ExecuteNonQuery() 
      SQLConn.Close() 
     End Using 
    End Using 
+0

它仍然不工作......如何使用? – 2012-01-08 19:37:00

+0

@ user1137385:剛更新了答案。 – 2012-01-08 19:37:51

+0

它仍然不起作用:( 雖然我可以選擇它.. – 2012-01-08 19:45:06

0

您是否嘗試過檢查連接字符串中的數據庫路徑? XD