VB快递2010
1 个表单,1 个文本框,1 个按钮
我通过单击创建了 .mdf sql 数据库文件Project>add new item>service based database>
并将其命名为 DXDB - 表名 DXtest 和 1 列名为 test
似乎代码有效..但是当我右键单击 DXtest 并单击显示表数据时......那里什么都没有...... =(它似乎并没有真正将数据插入数据库本身......
这是完整的代码
Imports System.Data
Imports System.Data.SqlClient
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim con As New SqlConnection
Dim cmd As New SqlCommand
Dim Tx1 As String
Tx1 = TextBox1.Text
con.ConnectionString = "Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\DXDB.mdf;Integrated Security=True;Connect Timeout=30;User Instance=True"
con.Open()
cmd.Connection = con
cmd.CommandText = "INSERT INTO DXTest(Test) VALUES(@Tx1)"
cmd.Parameters.Add("@Tx1", SqlDbType.VarChar, 50).Value = Tx1
cmd.ExecuteNonQuery()
con.Close()
End Sub
End Class