我想要做的是将我的图像添加到我的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 (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.WebControls.FileUpload到已知的托管提供程序 本地类型。
任何想法我应该在我的
中写什么cmd.Parameters.AddWithValue("@Image", FileUpload1.????);
谢谢你!
答案 0 :(得分:0)
您需要获得var input = FileUpload1.FileBytes
。
然后将此内容推送到字符串:
// Copy the byte array to a string.
for (int loop = 0; loop < input.length; loop++) {
displayString = displayString + input[loop].ToString();
}
然后将其发送到数据库:
cmd.Parameters.AddWithValue("@Image", displayString);
正如@SonerGönül在评论中所说:
顺便说一下,将来的SQL Server版本将删除图像类型。 考虑将列类型更改为varbinary(max)。