C#做图片插入数据库 出现挂起的异步操作,程序无法执行 在线求救

学渣进阶 2020-04-24 04:55:58
图片插入数据库代码如下,



private void pictureBox1_Click(object sender, EventArgs e)
{
OpenFileDialog openFile = new OpenFileDialog();
openFile.Filter = "图片文件(*.jpg)|*.jpg";

string filePath="" ;
if (openFile.ShowDialog() == DialogResult.OK)
{
filePath = openFile.FileName;

this.pictureBox1.ImageLocation = filePath;


//打开文件流,用来读取文件中的数据
FileStream Stream = new FileStream(filePath, FileMode.Open, FileAccess.Read);

byte[] bf = new byte[Stream.Length];
Stream.Read(bf, 0, (int)Stream.Length);
Stream.Close();

try
{
String MyConn = "Data Source=.; Initial Catalog=ShuangXuan; User ID = ; Pwd = "; // 连接字符串

SqlConnection MyConnection = new SqlConnection(MyConn); //创建连接对象

MyConnection.Open(); //打开连接

string getid = GDMU_SX.Index.userid;

//string strName = Path.GetFileName(filePath);

//string sql = "update dbo.Students set Picture= '@Picture' where ID="+getid+"";

//Console.Write(sql);

//MessageBox.Show(sql);

SqlCommand cmd = new SqlCommand("update_Picture", MyConnection);

cmd.Parameters.Add("@ID", SqlDbType.VarChar).Value = getid;
cmd.Parameters.Add("@Picture", SqlDbType.Image).Value = bf;

cmd.CommandType = CommandType.StoredProcedure;

//cmd.Parameters.Add("@Ext",SqlDbType.VarChar).Value= strName;

//SqlParameter img = new SqlParameter("@Picture", SqlDbType.Image);
//img.Value = bf;
//cmd.Parameters.Add(img);

cmd.BeginExecuteNonQuery();

int result = cmd.ExecuteNonQuery();

if (result > 0)
{
MessageBox.Show("插入成功!!!", "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
else
{
MessageBox.Show("插入失败!!!", "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
}

MyConnection.Close();


}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
}
}




存储过程图如下:
Create procedure [dbo].[update_Picture]

@ID varchar(50),
@Picture image

as

update dbo.Students set Picture = @Picture
where ID=@ID





出现问题如下:.

弹出消息框显示 :命令执行无法继续,因为正在处理一个挂起的异步操作

并且数据库没有相应的插入数据
...全文
90 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
ying1234 2020-04-24
  • 打赏
  • 举报
回复
cmd.BeginExecuteNonQuery() 总是和cmd.EndExecuteNonQuery成对出现的,
ying1234 2020-04-24
  • 打赏
  • 举报
回复
cmd.BeginExecuteNonQuery() 这是异步查询操作,需要在回调函数通过cmd.EndExecuteNonQuery来处理查询结果 int result = cmd.ExecuteNonQuery();这是同步操作。 你自已去百度cmd.BeginExecuteNonQuery()吧,例子多的是,看几个就明白了
学渣进阶 2020-04-24
  • 打赏
  • 举报
回复
引用 1 楼 杀马特丶蛮牛 的回复:
cmd.BeginExecuteNonQuery(); int result = cmd.ExecuteNonQuery(); 去掉上面那个看看
成了,您这方法可以 能说说是为什么吗 为什么不需要这句cmd.BeginExecuteNonQuery(); 期待您的回复!回复后结贴谢谢
杀马特丶蛮牛 2020-04-24
  • 打赏
  • 举报
回复
cmd.BeginExecuteNonQuery(); int result = cmd.ExecuteNonQuery(); 去掉上面那个看看

110,546

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧