Public cn As ADODBConnection
Public rs As ADODBRecordset
Public cc As String
Sub aa()
Set cn = New ADODBConnection
Set rs = New ADODBRecordset
cc = "Provider=SQLOLEDB1;Integrated Security=SSPI;Persist Security Info=False;Initial Catalog=library" ‘这里要改成连接的数据库。
cnOpen cc
End Sub
以上是模块代码。
Private Sub Command1_Click()
If Text1Text = "" Then
MsgBox "请输入商品ID!"
Text1SetFocus
Exit Sub
End If
If Text2Text = "" Then
MsgBox "请输入商品名称!"
Text2SetFocus
Exit Sub
End If
If Text3Text = "" Then
MsgBox "请输入商品价格!"
Text3SetFocus
Exit Sub
End If
sql = "select from goods where goodid='" & Text1Text & "'"
Set rs = New ADODBRecordset
rsOpen sql, cn, 1, 3
If rsEOF Then
rsAddNew
rsFields(0) = Text1Text
rsFields(1) = Text2Text
rsFields(2) = Text3Text
rsUpdate
MsgBox "商品信息添加成功!"
Text1Text = ""
Text2Text = ""
Text3Text = ""
Text1SetFocus
Else
MsgBox "此ID已存在,请输入新ID!"
Text1Text = ""
Text1SetFocus
End If
rsClose
Set rs = Nothing
End Sub
Private Sub Form_Load()
Call aa
End Sub
以上是窗体代码。
你好好看一下,这是一个正确的源代码。
必须安装mysql的驱动才行,不管是vb还是c++都需要这个驱动。
代码如下:
public
function
exemysql(byval
sql
as
string)
as
adodbrecordset
sql
=
trim$(sql)
set
conn
=
new
adodbconnection
set
rs
=
new
adodbrecordset
connopen
"driver=mysql
odbc
351
driver;"
&
_
"server=服务器地址;"
&
_
"port=3306;"
&
_
"database=数据库名;"
&
_
"uid=用户名;"
&
_
"pwd=密码"
conndefaultdatabase
=
"数据库名"
conncursorlocation
=
aduseclient
set
rsactiveconnection
=
conn
rslocktype
=
adlockbatchoptimistic
rscursortype
=
adopenkeyset
rsopen
sql
set
exesql
=
rs
set
rs
=
nothing
set
conn
=
nothing
end
function
读写EXCEL表: \x0d\1、在工程中引用Microsoft Excel类型库: \x0d\\x0d\从"工程"菜单中选择"引用"栏;选择Microsoft Excel 120 Object Library(EXCEL2007),然后选择"确定"。表示在工程中要引用EXCEL类型库。 \x0d\\x0d\2、在通用对象的声明过程中定义EXCEL对象: \x0d\\x0d\Dim xlApp As ExcelApplication \x0d\Dim xlBook As ExcelWorkBook \x0d\Dim xlSheet As ExcelWorksheet \x0d\\x0d\3、在程序中 *** 作EXCEL表常用命令: \x0d\\x0d\Set xlApp = CreateObject("ExcelApplication") '创建EXCEL对象 \x0d\Set xlBook = xlAppWorkbooksOpen("文件名") '打开已经存在的EXCEL工件簿文件 \x0d\Set xlBook = xlAppWorkbooksAdd("文件名") '创建新的EXCEL工件簿文件 \x0d\xlAppVisible = True '设置EXCEL对象可见(或不可见) \x0d\Set xlSheet = xlBookWorksheets("表名") '设置活动工作表 \x0d\for i=1 to 100\x0d\ for j=1 to 50\x0d\numArr(j,i)=xlSheetCells(j, i) '\x0d\ next\x0d\next\x0d\xlBookClose (True) '关闭工作簿 \x0d\xlAppQuit '结束EXCEL对象 \x0d\Set xlApp = Nothing '释放xlApp对象\x0d\张志晨
以上就是关于vb写入数据库全部的内容,包括:vb写入数据库、vb高手进,vb连接mysql数据库,怎么把数据写入数据库!、vb如何读取excel数据并存入数组等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)