C# 将图像从上载输入添加到数据库
我要做的是将图像添加到sql数据库中。目前我有以下代码:C# 将图像从上载输入添加到数据库,c#,image,C#,Image,我要做的是将图像添加到sql数据库中。目前我有以下代码: SqlConnection con = new SqlConnection("Data Source = 'PAULO'; Initial Catalog=Authorship;Integrated Security =True"); SqlCommand cmd = new SqlCommand(); protected void Button1_Click(object sender, EventArgs e) { if (
SqlConnection con = new SqlConnection("Data Source = 'PAULO'; Initial Catalog=Authorship;Integrated Security =True");
SqlCommand cmd = new SqlCommand();
protected void Button1_Click(object sender, EventArgs e)
{
if (Page.IsValid)
{
cmd.Connection = con; //assigning connection to command
cmd.CommandType = CommandType.Text; //representing type of command
//cmd.CommandText = "INSERT INTO UserDetails (Fname,Lname,Email,Password,Gender,Dob,Mobile,Address) values
// (@Fname,@Lname,@Email,@Password,@Gender,@Dob,@Mobile,@Address)";
cmd.CommandText = "INSERT INTO Products values(@Name,@Description,@Image,@Quantity,@ProductPrice)";
//adding parameters with value
cmd.Parameters.AddWithValue("@Name", TextBox1.Text);
cmd.Parameters.AddWithValue("@Description", TextBox2.Text);
cmd.Parameters.AddWithValue("@Image", FileUpload1);
cmd.Parameters.AddWithValue("@Quantity", TextBox4.Text);
cmd.Parameters.AddWithValue("@ProductPrice", TextBox4.Text);
con.Open(); //opening connection
cmd.ExecuteNonQuery(); //executing query
con.Close(); //closing connection
Label1.Text = "Added Successfully..";
}
else
{
Label1.Text = "Not successful";
}
我有一个错误:
对象类型中不存在映射
System.Web.UI.WebControl.FileUpload到已知的托管提供程序
本地类型
你知道我应该写什么吗
cmd.Parameters.AddWithValue("@Image", FileUpload1.????);
谢谢大家! 您需要获取
var input=FileUpload1.FileBytes
。
然后将此内容推送到字符串:
// Copy the byte array to a string.
for (int loop = 0; loop < input.length; loop++) {
displayString = displayString + input[loop].ToString();
}
//将字节数组复制到字符串。
for(int-loop=0;loop
然后将其发送到数据库:
cmd.Parameters.AddWithValue(“@Image”,displayString)代码>
正如@Soner Gönül在评论中建议的那样:
顺便说一下,映像类型将在未来的SQL Server版本中删除。
考虑将列类型更改为VARDION(MAX)。
您尝试在数据库中插入的Image
列的类型是什么?数据类型是image.im正在尝试插入jpg文件我认为您需要获得文件上载1.FileBytes
然后将其推入字符串并发送到数据库。顺便说一句,image
类型将在未来的SQL Server版本中删除。考虑将列类型更改为<代码> VARDION(MAX)< /代码>。