详情:
“System.Threading.ThreadStateException”类型的异常在 System.Windows.Forms.dll 中发生,但未在用户代码中进行处理
其他信息: 在可以调用 OLE 之前,必须将当前线程设置为单线程单元(STA)模式。请确保您的 Main 函数带有 STAThreadAttribute 标记。 只有将调试器附加到该进程才会引发此异常。
asp.net web
网上说加[STAThread]啥的怎么加?
代码如下:
protected void Button1_Click(object sender, EventArgs e)
{
OpenFileDialog ofd = new OpenFileDialog();
ofd.Filter = "*.jpg|*.JPG|*.gif|*.GIF|*.bmp|*.BMP";
if(ofd.ShowDialog()==DialogResult.OK)
{
string filepath = ofd.FileName.ToString();
FileStream fs = new FileStream(filepath, FileMode.Open,FileAccess.Read);
Byte[] imagebytes = new byte[fs.Length];
BinaryReader br = new BinaryReader(fs);
imagebytes = br.ReadBytes(Convert.ToInt32(fs.Length));
br.Close();
fs.Close();
SqlCommand sqlcmd = new SqlCommand("insert into battery(picture) values(@imagebytes) where No='41111'",SqlHelper.conn);
SqlParameter para = new SqlParameter("@picture", SqlDbType.Image);
para.Value = imagebytes;
sqlcmd.Parameters.Add(para);
try
{
SqlHelper.conn.Open();
sqlcmd.ExecuteNonQuery();
}
catch(Exception)
{
Response.Write("<script>alert('写入失败!')</script>");
}
finally
{
SqlHelper.conn.Close();
}
}
}