在 vb中如何往数据库中实现添加记录 10
1个回答
展开全部
Sub 添加_OnClick(ByVal sender As Object, ByVal e As EventArgs)
Dim SqlConn As New SqlConnection
SqlConn.ConnectionString = "Persist Security Info=true;Initial Catalog=Data;uid=sa;pwd=123456;Data Source=.;"
SqlConn.Open()
Dim SqlCmd As New SqlCommand
SqlCmd.Connection = SqlConn
SqlCmd.CommandText = "Insert into #table (客户编号,性别,电话,电子信箱,地址,住址)values('" & 客户编号.Text & "','" & 性别.Text & "','" & 电话.Text& "','" & 电子信箱.Text & "'," & 地址.Text & ",'" & 住址.Text & "')"
SqlCmd.ExecuteNonQuery()
SqlConn.Close()
End Sub
Dim SqlConn As New SqlConnection
SqlConn.ConnectionString = "Persist Security Info=true;Initial Catalog=Data;uid=sa;pwd=123456;Data Source=.;"
SqlConn.Open()
Dim SqlCmd As New SqlCommand
SqlCmd.Connection = SqlConn
SqlCmd.CommandText = "Insert into #table (客户编号,性别,电话,电子信箱,地址,住址)values('" & 客户编号.Text & "','" & 性别.Text & "','" & 电话.Text& "','" & 电子信箱.Text & "'," & 地址.Text & ",'" & 住址.Text & "')"
SqlCmd.ExecuteNonQuery()
SqlConn.Close()
End Sub
更多追问追答
追问
我们用的是accsess数据库,请问这个是往这数据库中添加么?
追答
我说的是SQL
accsess是这样
dim objConn as New OleDbConnection()
objConn.ConnectionString="Provider=Microsoft.Jet.OLEDB.1.1;Data Source="&Server.MapPath("数据库的路径")
objCOnn.open()
dim objCmd as new OleDbDataAdapter()
'设置SelectCommand属性
objCmd.SelectCommand=New OleDbCommand("Select * from #table")
'创建爱你CommandBuilder对象
dim objCB as OleDbCommandBuilder=new OleDbCommandBuilder(objCmd)
'创建DataSet对象,并将SelectCommand的执行结果置入DataSet对象
dim DS as new DataSet()
objCmd.Fill(DS,"table")
objConn.Close()
Dim myTable as DataTable=ds.Table("table")
'插入数据行
dim myRow as DataRow =myTable.NewRow()
myRow("客户编号")=客户编号.Text
myRow(性别)=性别.Text
myRow(电话)=电话.Text
myRow(电子信箱)=电子信箱.Text
myRow(地址)=地址.Text
myRow(住址)=住址.Text
myTable.Row.Add(myRow)
objCmd.Update(myTable.Select(Nothing,Nothing,DataViewRowState.Added))
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询