問題描述
在我運行程序后,我添加了數(shù)據(jù),然后我收到了這條消息:
After I run the program, I add the data, and then i get this message:
不允許從數(shù)據(jù)類型 varchar 隱式轉(zhuǎn)換為 varbinary
implicit conversion from data type varchar to varbinary is not allowed
我和我在 YouTube 上觀看了這個視頻和youtuber完全一樣.這對他有效,但對我無效.
I watched this video on YouTube and I did exactly the same as youtuber. It works for him but not me.
我正在嘗試將 Visual Studio 中的數(shù)據(jù)添加到 SQL 數(shù)據(jù)庫中:
I am trying to add data from Visual studio into an SQL database:
代碼如下:
Imports System.Data.SqlClient
SELECT CONVERT(varchar(100), CONVERT(varbinary(max),'0xFFD8FFE000'))
Public Class Form1
Dim Conn As SqlConnection
Dim CMD As SqlCommand
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Conn = New SqlConnection
Conn.ConnectionString = "Data Source=BYG-A101-MOELKA;Initial Catalog=App;Integrated Security=True"
Dim READER As SqlDataReader
Try
Conn.Open()
Dim Query As String
Query = "insert into person (ID,firstname,lastname,age) values ('" & TextBox1.Text & "', '" & TextBox2.Text & "', '" & TextBox3.Text & "', '" & TextBox4.Text & "')"
CMD = New SqlCommand(Query, Conn)
READER = CMD.ExecuteReader
MessageBox.Show("Datasaved")
Conn.Close()
Catch ex As Exception
MessageBox.Show(ex.Message)
Finally
Conn.Dispose()
End Try
End Sub
推薦答案
一些問題
ExecuteReader 不應(yīng)用于插入
使用 ExecuteNonQuery一個>
ExecuteReader should not be used for an insert
use ExecuteNonQuery
這會受到注入攻擊.使用參數(shù).參數(shù)必須與表列的數(shù)據(jù)類型匹配.
That is subject to injection attack. Use parameters. The parameters must match the data type of the table columns.
這篇關(guān)于不允許從數(shù)據(jù)類型 varchar 到 varbinary 的隱式轉(zhuǎn)換的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!