我要在C#窗体中做一个文件保存到数据库的功能,请问要用到那些控件?
展开全部
pictureBox 控件 Buttob控件
参考代码(显示用户选择的图片,点击上传按钮之后就以二进制保存到数据库中):
private void button1_Click(object sender, EventArgs e)
{
if (openFileDialog1.ShowDialog() == DialogResult.OK && openFileDialog1.FileName != "")
{
System.Drawing.Bitmap objPic;
objPic = new System.Drawing.Bitmap(openFileDialog1.FileName.ToString().Trim());
pictureBox1.Image = new System.Drawing.Bitmap(objPic, 200, 200);
pictureBox1.Image = Image.FromFile(openFileDialog1.FileName);
}
}
private void button2_Click(object sender, EventArgs e)
{
try
{
string conString = "Data Source =.; Initial Catalog = ;User ID = ; Pwd = ";
SqlConnection connection = new SqlConnection(conString);
string sql = "insert c values (1,@TP)";
SqlCommand command = new SqlCommand(sql, connection);
string picturePath = openFileDialog1.FileName;
FileStream fs = new FileStream(picturePath, FileMode.Open, FileAccess.Read);
Byte[] mybyte = new byte[fs.Length];
fs.Read(mybyte, 0, mybyte.Length);
fs.Close();
SqlParameter prm = new SqlParameter
("@TP", SqlDbType.VarBinary, mybyte.Length, ParameterDirection.Input, false, 0, 0, null, DataRowVersion.Current, mybyte);
command.Parameters.Add(prm);
connection.Open();
int result = command.ExecuteNonQuery();
connection.Close();
if (result > 0) { MessageBox.Show("上传成功!", "提示");}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
2015-01-10
展开全部
来了来了
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询