[英]Connection of VB.NET to SQL server
I had a project running perfectly ie entering values in database linked by code. 我有一个项目运行得很好,即在通过代码链接的数据库中输入值。 But in the other project I am implementing it shows values correctly submitted, gives no error while running but values are not entered in database.
但是在另一个我正在实现的项目中,它显示了正确提交的值,运行时没有错误,但是值未输入数据库。
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
You've missed the executenonquery()
,so the query you have provided is niether executed.replace the below code and eeverything will be working. 您已经错过了
executenonquery()
,因此您提供的查询完全执行了。替换下面的代码,一切将正常进行。
cmd.Connection = cn
cmd.CommandText = "insert into StudResume values('" + TextBox1.Text + "','" + TextBox2.Text + "','" + DateTimePicker1.Value.ToShortDateString() + "'," + TextBox3.Text + ")"
cmd.ExecuteNonQuery()
cmd.Dispose()
cn.Close()
Ie Add cmd.ExecuteNonQuery()
after providing the commandtext. 即在提供命令
cmd.ExecuteNonQuery()
后添加cmd.ExecuteNonQuery()
。
change your button3 code as the following, it will give u an error message in msgbox, post the error message here so may we can help, 如下更改您的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
尝试这种方式cn = new SqlConnection(“数据源= ROHAN-PC \\ SQLEXPRESS;初始目录= Libeasy;集成安全性= true”)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.