这是表单中我的SAVE按钮中的代码。我正在使用VB6.0和MSSQL Server 2005.
CommonDialog1& 2保存插入图片框中的图像的文件名。
Set rs = con.Execute("insert into table1
values('" & txtbox1.Text & "' , "_
& " '" & txtbox2.Text & "' , "_
& " '" & CommonDialog1.FileName & "' , "_
& " '" & CommonDialog2.FileName & "' , "_
& " '" & txtbox3.Text & "' )")
“不允许从数据类型varchar到varbinary(max)的隐式转换。使用CONVERT函数运行此查询。” - 在运行时弹出错误。
答案 0 :(得分:0)
由于SQL的“INSERT”功能不接受子查询,我尝试转换CommonDialog1&的文件名。 2使用 CAST ,这是成功的。
设置rs = con.Execute(“插入table1
值('“& txtbox1.Text&”',“_
&安培; “'”& txtbox2.Text& “',”_
的&安培; “cast('”& CommonDialog1.FileName&“'as varbinary(max)),”_
&安培; “cast('”& CommonDialog2.FileName&“'as varbinary(max)),”_
&安培; “'”& txtbox3.Text& “')”)
答案 1 :(得分:0)
Dim mConn As New ADODB.Connection Dim mCmd As New ADODB.Command Dim mStreamCD1 As New ADODB.Stream Dim mStreamCD2 As New ADODB.Stream mStreamCD1.Type = adTypeBinary mStreamCD1.Open mStreamCD1.LoadFromFile (CommonDialog1.FileName) mStreamCD2.Type = adTypeBinary mStreamCD2.Open mStreamCD2.LoadFromFile (CommonDialog2.FileName) mConn.Open "Insert your Connection String" Set mCmd.ActiveConnection = mConn mCmd.CommandType = adCmdText mCmd.CommandText = "InsertIntoTable1" mCmd.Parameters.Append mCmd.CreateParameter("@txtbox1Text", adVarChar, adParamInput, 250, txtbox1.Text) mCmd.Parameters.Append mCmd.CreateParameter("@txtbox2Text", adVarChar, adParamInput, 250, txtbox2.Text) mCmd.Parameters.Append mCmd.CreateParameter("@CD1", adVarBinary, adParamInput, mStreamCD1.Size, mStreamCD1.Read) mCmd.Parameters.Append mCmd.CreateParameter("@CD2", adVarBinary, adParamInput, mStreamCD2.Size, mStreamCD2.Read) mCmd.Parameters.Append mCmd.CreateParameter("@txtbox3Text", adVarChar, adParamInput, 250, txtbox3.Text) mCmd.Execute , , adExecuteNoRecords