我正在构建一个sql_insert_string
用于Microsoft.ApplicationBlocks.Data.SqlHelper
使用方法如下:
SqlHelper.ExecuteNonQuery(Transaction, CommandType.Text, sql_insert_string)
当我将鼠标悬停在 SQL 语句上时,它如下所示:
string sql_insert_string = "Insert into images_table(image_id, image_byte_array) values ('123', System.Byte[])
其中一个插入值是一个字节数组,如上所示。该变量在字节数组中具有值,例如 byte[6738] 。但之后sql_insert_string
被构造出来,它是System.Byte[]
. The image_byte_array
列类型是varbinary(max)
。数据库是SQL Server 2008。因此数据库抛出以下错误:
对象或列名称缺失或为空。对于 SELECT INTO 语句,验证每列都有一个名称。对于其他语句,请查找空别名。不允许定义为 \"\" 或 [] 的别名。将别名更改为有效名称。
您可以像这样插入字节数组:
private void FireSql(byte[] input)
{
const string sql_insert_string =
"Insert into images_table(image_id, image_byte_array) values (@image_id, @image_byte_array)";
SqlTransaction transaction = null; //wherever you get the transaction obj from.
var imageIdParam = new SqlParameter("@image_id", SqlDbType.Int, 4)
{
Direction = ParameterDirection.Input,
Value = 123
}; //change the data type to whatever data type you are expecting
var byteParam = new SqlParameter("@image_byte_array", SqlDbType.VarBinary)
{
Direction = ParameterDirection.Input,
Size = input.Length,
Value = input
}; //change the data type to whatever data type you are expecting
SqlHelper.ExecuteNonQuery(transaction, CommandType.Text, sql_insert_string, imageIdParam, byteParam);
}
我建议查看 ORM (https://en.wikipedia.org/wiki/Object-relational_mapping)像实体框架(http://www.asp.net/entity-framework)为您完成所有这一切,同时提高安全性并使未来的更改变得更加容易。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)