vb高手进,vb连接mysql数据库,怎么把数据写入数据库!
我在连接数据库的时候会报数据源太长,而且还会出现类型不匹配的情况!我的代码如下各位高手帮忙看下:所有的变量定义都是:dimXXPrivateSubCommand4_Cli...
我在连接数据库的时候会报数据源太长 ,而且还会出现类型不匹配的情况!
我的代码如下各位高手帮忙看下:
所有的变量定义都是:dim XX
Private Sub Command4_Click()
Dim conn As New ADODB.Connection '创建一个连接实例
Dim rs As ADODB.Recordset '创建一个recordset实例,因为经常要用 所以不用new
Dim CnStr As String '创建两个字符串变量
Dim Sql As String
Dim db_host As String '连接所需要的变量
Dim db_user As String
Dim db_pass As String
Dim db_data As String
Dim db_ip As String
Dim i As Integer, j As Integer
Dim strline As String
db_host = "localhost"
db_user = "root"
db_pass = ""
db_data = "al_dbform" '开始连接数据库
db_ip = "C:\Program Files\MySQL\MySQL Server 5.1\bin\" ‘数据库安装目录
CnStr = "DRIVER={MySQL ODBC 5.1 Driver};" & _
"SERVER=" & db_host & ";" & _
"Data Source=" & db_ip & ";" & _
"DATABASE=" & db_data & ";" & _
"UID=" & db_user & ";PWD=" & db_pass & ";" & _
"OPTION=4;"
conn.Open CnStr, db, 3
conn.CursorLocation = adUseClient
conn.Open CnStr, db, 3
For i = 0 To UBound(cs) - 1
Sql = "insert into forminfo1(FormType,FormStatus,FormCodeStart,FormCodeEnd,Reason,Remark,OperationTime) values(" + ft + ",'" + fs + "," + cs(i) + "','" + ce(i) + "'," + rss + ",'" + Text1.Text + "'," + Now() + ")" ’rss回报类型不匹配
conn.Execute Sql
Next i
conn.Close
Set conn = Nothing
End Sub
请高手指点下 !
数据库的类型都是设定varchar类型的。
我稍微改了以下 但是现在却出现这个错误:
实时错误‘-2107477259 (80004005)’;
[Mysql][ODBC 5.1 Driver]Access denied for user 'root'@'localhost'(using password:YES)
代码上没有太大的改变就是重新添加了odbc驱动和设置的数据源,把db_ip去掉了 换成了db_port="3306" 但是却一直报上面的错 请帮忙分析下 谢谢! 展开
我的代码如下各位高手帮忙看下:
所有的变量定义都是:dim XX
Private Sub Command4_Click()
Dim conn As New ADODB.Connection '创建一个连接实例
Dim rs As ADODB.Recordset '创建一个recordset实例,因为经常要用 所以不用new
Dim CnStr As String '创建两个字符串变量
Dim Sql As String
Dim db_host As String '连接所需要的变量
Dim db_user As String
Dim db_pass As String
Dim db_data As String
Dim db_ip As String
Dim i As Integer, j As Integer
Dim strline As String
db_host = "localhost"
db_user = "root"
db_pass = ""
db_data = "al_dbform" '开始连接数据库
db_ip = "C:\Program Files\MySQL\MySQL Server 5.1\bin\" ‘数据库安装目录
CnStr = "DRIVER={MySQL ODBC 5.1 Driver};" & _
"SERVER=" & db_host & ";" & _
"Data Source=" & db_ip & ";" & _
"DATABASE=" & db_data & ";" & _
"UID=" & db_user & ";PWD=" & db_pass & ";" & _
"OPTION=4;"
conn.Open CnStr, db, 3
conn.CursorLocation = adUseClient
conn.Open CnStr, db, 3
For i = 0 To UBound(cs) - 1
Sql = "insert into forminfo1(FormType,FormStatus,FormCodeStart,FormCodeEnd,Reason,Remark,OperationTime) values(" + ft + ",'" + fs + "," + cs(i) + "','" + ce(i) + "'," + rss + ",'" + Text1.Text + "'," + Now() + ")" ’rss回报类型不匹配
conn.Execute Sql
Next i
conn.Close
Set conn = Nothing
End Sub
请高手指点下 !
数据库的类型都是设定varchar类型的。
我稍微改了以下 但是现在却出现这个错误:
实时错误‘-2107477259 (80004005)’;
[Mysql][ODBC 5.1 Driver]Access denied for user 'root'@'localhost'(using password:YES)
代码上没有太大的改变就是重新添加了odbc驱动和设置的数据源,把db_ip去掉了 换成了db_port="3306" 但是却一直报上面的错 请帮忙分析下 谢谢! 展开
展开全部
必须安装mysql的驱动才行,不管是vb还是c++都需要这个驱动。
代码如下:
public
function
exemysql(byval
sql
as
string)
as
adodb.recordset
sql
=
trim$(sql)
set
conn
=
new
adodb.connection
set
rs
=
new
adodb.recordset
conn.open
"driver=mysql
odbc
3.51
driver;"
&
_
"server=服务器地址;"
&
_
"port=3306;"
&
_
"database=数据库名;"
&
_
"uid=用户名;"
&
_
"pwd=密码"
conn.defaultdatabase
=
"数据库名"
conn.cursorlocation
=
aduseclient
set
rs.activeconnection
=
conn
rs.locktype
=
adlockbatchoptimistic
rs.cursortype
=
adopenkeyset
rs.open
sql
set
exesql
=
rs
set
rs
=
nothing
set
conn
=
nothing
end
function
代码如下:
public
function
exemysql(byval
sql
as
string)
as
adodb.recordset
sql
=
trim$(sql)
set
conn
=
new
adodb.connection
set
rs
=
new
adodb.recordset
conn.open
"driver=mysql
odbc
3.51
driver;"
&
_
"server=服务器地址;"
&
_
"port=3306;"
&
_
"database=数据库名;"
&
_
"uid=用户名;"
&
_
"pwd=密码"
conn.defaultdatabase
=
"数据库名"
conn.cursorlocation
=
aduseclient
set
rs.activeconnection
=
conn
rs.locktype
=
adlockbatchoptimistic
rs.cursortype
=
adopenkeyset
rs.open
sql
set
exesql
=
rs
set
rs
=
nothing
set
conn
=
nothing
end
function
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询