2015-02-12 211 views
0

我有一個項目運行完美,即在代碼鏈接的數據庫中輸入值。但在我正在實施的其他項目中,它顯示正確提交的值,在運行時不會發生錯誤,但值不會輸入到數據庫中。VB.NET連接到SQL服務器

Private Sub frmAddresume_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load 

    cn.ConnectionString = "Data Source=ROHAN-PC\SQLEXPRESS;initial catalog=Libeasy;Integrated Security=true" 

    DateTimePicker1.Value = DateTime.Now.ToShortDateString() 
End Sub 

Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click 
    If (TextBox1.Text <> "" And TextBox2.Text <> "" And TextBox3.Text <> "") Then 
     cn.Open() 
     cmd.Connection = cn 
     cmd.CommandText = "insert into StudResume values('" + TextBox1.Text + "','" + TextBox2.Text + "','" + DateTimePicker1.Value.ToShortDateString() + "'," + TextBox3.Text + ")" 

     cmd.Dispose() 
     cn.Close() 

     MsgBox("Details saved Successfully", MsgBoxStyle.Information, "Done") 
     TextBox1.Text = "" 
     TextBox2.Text = "" 
     TextBox3.Text = "" 

     DateTimePicker1.Value = Now 
     TextBox1.Focus() 
    Else 
     MsgBox("Please Enter Complete Details", MsgBoxStyle.Critical, "Error") 
    End If 
End Sub 

回答

1

你已經錯過了executenonquery(),所以您所提供的查詢是niether executed.replace下面的代碼和eeverything會工作。

cmd.Connection = cn 
     cmd.CommandText = "insert into StudResume values('" + TextBox1.Text + "','" + TextBox2.Text + "','" + DateTimePicker1.Value.ToShortDateString() + "'," + TextBox3.Text + ")" 
cmd.ExecuteNonQuery() 
     cmd.Dispose() 
     cn.Close() 

即在提供commandtext後添加cmd.ExecuteNonQuery()

+0

它給出錯誤sqlException是未註冊的 – 2015-02-12 09:23:44

+0

檢查query.it可能是查詢參數的問題。 – 2015-02-12 09:27:04

+0

實際上,我有另一個項目的代碼工作塊像上面那樣雖然那個工作 – 2015-02-12 09:28:50

0

改變你的BUTTON3代碼下面,將要給你的錯誤消息的msgbox,發佈錯誤消息,在這裏,可能大家能有所幫助,

 If (TextBox1.Text <> "" And TextBox2.Text <> "" And TextBox3.Text <> "") Then 
      Try 
       cn.Open() 
       cmd.Connection = cn 
       cmd.CommandText = "insert into StudResume values('" + TextBox1.Text + "','" + TextBox2.Text + "','" + DateTimePicker1.Value.ToShortDateString() + "'," + TextBox3.Text + ")" 

       cmd.Dispose() 
       cn.Close() 

       MsgBox("Details saved Successfully", MsgBoxStyle.Information, "Done") 
       TextBox1.Text = "" 
       TextBox2.Text = "" 
       TextBox3.Text = "" 

       DateTimePicker1.Value = Now 
       TextBox1.Focus() 
      Catch ex As Exception 
       MsgBox(ex.Message) 
      Finally 

      End Try 

     Else 
      MsgBox("Please Enter Complete Details", MsgBoxStyle.Critical, "Error") 
     End If 
+0

我剛發現如果texbox3中的值是字符串,那麼問題可能是由於(「+ TextBox3.Text +」),所以應該有單引號'「+ TextBox3.text +」' – 2015-02-13 01:36:01

0

嘗試這種方式 CN =新的SqlConnection(」數據源= ROHAN-PC \ SQLEXPRESS;初始目錄= Libeasy;集成安全性= true「)