m0_37056907 2016-12-15 18:33 采纳率: 0%
浏览 2101

“System.Threading.ThreadStateException”异常

详情:
“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();
            }



        }
    }
  • 写回答

2条回答 默认 最新

报告相同问题?

悬赏问题

  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿
  • ¥15 回答4f系统的像差计算
  • ¥15 java如何提取出pdf里的文字?