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
出现问题如下:.
弹出消息框显示 :命令执行无法继续,因为正在处理一个挂起的异步操作
并且数据库没有相应的插入数据