◆如何将文件插入Image类型的字段◆ C#如何将文件(比如:txt,gif,jpg,doc等格式的文档)以流的方式插入到Image类型的字段中.请附详细代码.谢谢! 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 private void button1_Click(object sender, EventArgs e) { Stream ms; byte[] picbyte; OpenFileDialog ofdSelectPic = new OpenFileDialog(); ofdSelectPic.ShowDialog(); ms = ofdSelectPic.OpenFile(); picbyte = new byte[ms.Length]; ms.Position = 0; ms.Read(picbyte, 0, Convert.ToInt32(ms.Length)); SqlConnection conn = new SqlConnection("server=.;uid=sa;pwd=;database=s"); conn.Open(); string sqlstring = "insert into ttt(image) values(@img)"; SqlCommand cmd = new SqlCommand(sqlstring, conn); cmd.Parameters.Add("@img", SqlDbType.Image, picbyte.Length).Value = picbyte; cmd.ExecuteNonQuery(); conn.Close(); }用例中数据库为s, 表名为ttt ,字段名为image, 类型为image的. 将选中的图片放入数据库中. string fileAbsolutePath = @"d:\a.gif"; //文件的路径byte[] fileContents = File.ReadAllBytes(fileAbsolutePath); using (SqlConnection cn = new SqlConnection("Server=.;Uid=sa;Pwd=你的密码;Database=pubs")) //连接数据库 { using (SqlCommand cmd = new SqlCommand("INSERT INTO test (imageField) VALUES (@img)", cn)) //插入到指定的表的指定的字段 { try { cn.Open(); cmd.Parameters.Add("@img", SqlDbType.Image).Value = fileContents; cmd.ExecuteNonQuery(); } catch (SqlException ex) { throw ex; } } } 转化为二进制格式再存到Image类型的字段中 转化为二进制格式再存到Image类型的字段中 try { FileInfo fi = new FileInfo(filename); if (!fi.Exists) { MessageBox.Show("该文件名不存在"); } byte[] contents = new byte[fi.Length]; FileStream fs = fi.OpenRead(); fs.Read(contents, 0, contents.Length); fs.Close(); string str1 = "server=127.0.0.1;database=xu;uid=sa;pwd=''"; SqlConnection sqlconn = new SqlConnection(str1); sqlconn.Open(); string str3 = "insert into sc(sno,image) values(@sno,@image) "; SqlCommand sqlcomm = new SqlCommand(str3, sqlconn); sqlcomm.Parameters.Add("@sno", SqlDbType.Char, 5); sqlcomm.Parameters.Add("@image", SqlDbType.Image, 16); sqlcomm.Parameters["@sno"].Value = "0521"; sqlcomm.Parameters["@image"].Value = contents; sqlcomm.ExecuteNonQuery(); sqlconn.Close(); MessageBox.Show("图像文件 " + fi.FullName + " 成功上传到数据库!"); } catch (Exception exc) { MessageBox.Show(exc.Message); } c#进程控制问题 webbrowser控件或webkit实现一个会话使用一个代理服务器? 怎样编写代码使图片框控件依次装载的图片(其图片依次部份覆盖)处于最上面? 如何重新绘制无边框窗体的标题栏 pocketpc 串口接收数据问题 怎么取得一个文件有多好列呢? 向服务器提交数据的问题 ADOX 如何将SQL数据表复制到ACCESS中去? ping的问题 怎么实现点击按钮弹出一个新窗口,然后在新窗口选定数据后点“确定”将新窗口的参数到父窗口页面? c#如何在一个窗体中吊用另一个窗体的控件的属性
private void button1_Click(object sender, EventArgs e)
{
Stream ms;
byte[] picbyte;
OpenFileDialog ofdSelectPic = new OpenFileDialog();
ofdSelectPic.ShowDialog();
ms = ofdSelectPic.OpenFile();
picbyte = new byte[ms.Length];
ms.Position = 0;
ms.Read(picbyte, 0, Convert.ToInt32(ms.Length)); SqlConnection conn = new SqlConnection("server=.;uid=sa;pwd=;database=s");
conn.Open();
string sqlstring = "insert into ttt(image) values(@img)";
SqlCommand cmd = new SqlCommand(sqlstring, conn);
cmd.Parameters.Add("@img", SqlDbType.Image, picbyte.Length).Value = picbyte;
cmd.ExecuteNonQuery();
conn.Close();
}用例中数据库为s, 表名为ttt ,字段名为image, 类型为image的.
将选中的图片放入数据库中.
string fileAbsolutePath = @"d:\a.gif"; //文件的路径byte[] fileContents = File.ReadAllBytes(fileAbsolutePath); using (SqlConnection cn = new SqlConnection("Server=.;Uid=sa;Pwd=你的密码;Database=pubs")) //连接数据库
{
using (SqlCommand cmd = new SqlCommand("INSERT INTO test (imageField) VALUES (@img)", cn)) //插入到指定的表的指定的字段
{
try
{
cn.Open();
cmd.Parameters.Add("@img", SqlDbType.Image).Value = fileContents;
cmd.ExecuteNonQuery();
}
catch (SqlException ex)
{
throw ex;
}
}
}
try
{
FileInfo fi = new FileInfo(filename);
if (!fi.Exists)
{
MessageBox.Show("该文件名不存在");
}
byte[] contents = new byte[fi.Length];
FileStream fs = fi.OpenRead();
fs.Read(contents, 0, contents.Length);
fs.Close();
string str1 = "server=127.0.0.1;database=xu;uid=sa;pwd=''";
SqlConnection sqlconn = new SqlConnection(str1);
sqlconn.Open();
string str3 = "insert into sc(sno,image) values(@sno,@image) ";
SqlCommand sqlcomm = new SqlCommand(str3, sqlconn);
sqlcomm.Parameters.Add("@sno", SqlDbType.Char, 5);
sqlcomm.Parameters.Add("@image", SqlDbType.Image, 16);
sqlcomm.Parameters["@sno"].Value = "0521";
sqlcomm.Parameters["@image"].Value = contents;
sqlcomm.ExecuteNonQuery();
sqlconn.Close();
MessageBox.Show("图像文件 " + fi.FullName + " 成功上传到数据库!");
}
catch (Exception exc)
{
MessageBox.Show(exc.Message);
}