无法将程序保存在sql数据库中 [英] unable to save the program in the sql database

查看:106
本文介绍了无法将程序保存在sql数据库中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我得到了您的解决方案并实现了该解决方案,但仍然努力克服了.我得到的错误消息是"@imagedata附近的语法不正确".
这是我的代码...请帮助我.......
- - - - - - - - - - - -代码 - - - - - - - - - - - - - ------
收合|复制代码

导入系统
导入System.Net
导入System.Drawing
导入System.Collections
导入System.Componentmodel
导入System.windows.Forms

导入System.Data
导入System.Runtime.InteropServices
导入System.Drawing.Imaging
导入System.IO

导入System.Data.SqlClient
导入System.IO.Directory
导入System.Threading
公共类scan_salary
Dim con As New SqlConnection(str)
私有Sub btn_browse_Click(ByVal发送者作为System.Object,ByVal e作为System.EventArgs)处理btn_browse.Click
试试
OFD1.Filter ="Jpg文件(* .Jpg)| * .Jpg"&"| Jpeg文件(* .Jpeg)| * .Jpeg"&"| Gif文件(* .Gif)| * .Gif"
如果OFD1.ShowDialog = Windows.Forms.DialogResult.OK然后
Label4.Text = OFD1.FileName
其他
Label4.Text ="
如果结束

如果Label4.Text ="然后
PictureBox1.Visible = False
其他
lbl_path.Text = Label4.Text
昏暗imgdata()为字节=读取文件(lbl_path.Text)
Dim ms As MemoryStream =新的MemoryStream(imgdata,0,imgdata.Length)
ms.Write(imgdata,0,imgdata.Length)
昏暗img作为图像= Image.FromStream(ms,True)
PictureBox1.Image = img
如果结束
''PictureBox1.Image = Image.FromFile()

lbl_path.Text = Label4.Text
Label4.Text = Label4.Text.Remove(Label4.Text.LastIndexOf("\"))

异常捕获
MsgBox(ex.ToString())
结束尝试
结束子

公共函数readfile(ByVal FPath作为字符串)作为Byte()
昏暗的data()作为字节
试试
昏暗的FI作为新的FileInfo(FPath)
昏暗的numbytes只要= FI.Length
Dim fstream As FileStream =新FileStream(FPath,FileMode.Open,FileAccess.Read)
Dim br As BinaryReader =新的BinaryReader(fstream)
数据= br.ReadBytes(Convert.ToInt64(numbytes))
fstream.Close()
异常捕获
MsgBox(ex.Message.ToString)
结束尝试

返回数据
最终功能


私有Sub btn_save_Click(ByVal发送者为System.Object,ByVal e为System.EventArgs)处理btn_save.Click
如果con.State<> ConnectionState.Open然后
con.Open()


昏暗的insdate,effdate为String
insdate = Now().Date
effdate = dtp1.Value.ToString


昏暗的emp_id为字符串= Class2.Empid
Dim sal As String = Class2.salary

昏暗imgdata()为字节=读取文件(lbl_path.Text)
Dim cmd As New SqlCommand(插入emp.dbo.scan_salary(emp_id,salary,imagedata,insert_date,effect_date)"&_
值("&emp_id&'',""&sal&'',""&insdate&'',''''&effdate&"'',@ imagedata,con)
cmd.Parameters.Add(新SqlParameter("@ imagedata",CType(imgdata,Object)))
''cmd.Parameters.Add(新SqlParameter("@ insert_date",DateTime.Now.ToString("yyyy-MM-dd")))
''cmd.Parameters.Add(新SqlParameter("@ effect_date",dtp1.Value.ToString("yyyy-MM-dd")))
''cmd.Parameters.Add(新SqlParameter("@ emp_id",emp_id.ToString))
''cmd.Parameters.Add(New SqlParameter("@ salary",sal.ToString.Trim))
''cmd1.Parameters.Add(New SqlParameter("@ Docimage",CType(imgdata,Object)))
cmd.ExecuteNonQuery()
cmd.CommandTimeout = 0

如果结束
MsgBox(文档已上传")
结束子
结束类

i got your solution and implement that also but still dint get it over.the error message i got is "incorrect syntax near @imagedata".
here''s my code...plz assist me.......
-----------------------CODE--------------------------------
Collapse | Copy Code

Imports System
Imports System.Net
Imports System.Drawing
Imports System.Collections
Imports System.Componentmodel
Imports System.windows.Forms

Imports System.Data
Imports System.Runtime.InteropServices
Imports System.Drawing.Imaging
Imports System.IO

Imports System.Data.SqlClient
Imports System.IO.Directory
Imports System.Threading
Public Class scan_salary
Dim con As New SqlConnection(str)
Private Sub btn_browse_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_browse.Click
Try
OFD1.Filter = "Jpg Files(*.Jpg)|*.Jpg" & "|Jpeg Files(*.Jpeg)|*.Jpeg" & "|Gif Files(*.Gif)|*.Gif"
If OFD1.ShowDialog = Windows.Forms.DialogResult.OK Then
Label4.Text = OFD1.FileName
Else
Label4.Text = ""
End If

If Label4.Text = "" Then
PictureBox1.Visible = False
Else
lbl_path.Text = Label4.Text
Dim imgdata() As Byte = readfile(lbl_path.Text)
Dim ms As MemoryStream = New MemoryStream(imgdata, 0, imgdata.Length)
ms.Write(imgdata, 0, imgdata.Length)
Dim img As Image = Image.FromStream(ms, True)
PictureBox1.Image = img
End If
''PictureBox1.Image = Image.FromFile()

lbl_path.Text = Label4.Text
Label4.Text = Label4.Text.Remove(Label4.Text.LastIndexOf("\"))

Catch ex As Exception
MsgBox(ex.ToString())
End Try
End Sub

Public Function readfile(ByVal FPath As String) As Byte()
Dim data() As Byte
Try
Dim FI As New FileInfo(FPath)
Dim numbytes As Long = FI.Length
Dim fstream As FileStream = New FileStream(FPath, FileMode.Open, FileAccess.Read)
Dim br As BinaryReader = New BinaryReader(fstream)
data = br.ReadBytes(Convert.ToInt64(numbytes))
fstream.Close()
Catch ex As Exception
MsgBox(ex.Message.ToString)
End Try

Return data
End Function


Private Sub btn_save_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_save.Click
If con.State <> ConnectionState.Open Then
con.Open()


Dim insdate, effdate As String
insdate = Now().Date
effdate = dtp1.Value.ToString


Dim emp_id As String = Class2.Empid
Dim sal As String = Class2.salary

Dim imgdata() As Byte = readfile(lbl_path.Text)
Dim cmd As New SqlCommand("insert into emp.dbo.scan_salary ( emp_id,salary, imagedata, insert_date, effect_date)" & _
"values ( ''" & emp_id & "'', ''" & sal & "'', ''" & insdate & "'',''" & effdate & "'',@imagedata", con)
cmd.Parameters.Add(New SqlParameter("@imagedata", CType(imgdata, Object)))
''cmd.Parameters.Add(New SqlParameter("@insert_date", DateTime.Now.ToString("yyyy-MM-dd")))
''cmd.Parameters.Add(New SqlParameter("@effect_date", dtp1.Value.ToString("yyyy-MM-dd")))
''cmd.Parameters.Add(New SqlParameter("@emp_id", emp_id.ToString))
''cmd.Parameters.Add(New SqlParameter("@salary", sal.ToString.Trim))
''cmd1.Parameters.Add(New SqlParameter("@Docimage", CType(imgdata, Object)))
cmd.ExecuteNonQuery()
cmd.CommandTimeout = 0

End If
MsgBox("document uploaded")
End Sub
End Class

推荐答案

hi,

只需在 @imagedata之前添加一个"",然后尝试....



just add a " before @imagedata" and try....

Dim cmd As New SqlCommand("insert into emp.dbo.scan_salary ( emp_id,salary, imagedata, insert_date, effect_date)" & _
"values ( '" & emp_id & "', '" & sal & "', '" & insdate & "','" & effdate & "',@imagedata", con)



问候



Regards


这篇关于无法将程序保存在sql数据库中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆