总是提示这个错误 必须声明标量变量 content1,再有想一次批量向数据库1行的3列导入图片二进制流,有啥简单方法吗

baidu_34767154 2016-04-30 07:47:39
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Data.Common;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.IO;


using System.Data.SqlClient;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{


}

protected void Button1_Click(object sender, EventArgs e)
{
string name = FileUpload1.PostedFile.FileName;
string name1 = FileUpload2.PostedFile.FileName;
string name2 = FileUpload3.PostedFile.FileName;
string name3 = FileUpload4.PostedFile.FileName;

FileStream fs = File.OpenRead(name);
byte[] content = new byte[fs.Length];
fs.Read(content, 0, content.Length);
fs.Close();

FileStream fs1 = File.OpenRead(name1);
byte[] content1 = new byte[fs1.Length];
fs1.Read(content1, 0, content1.Length);
fs1.Close();

FileStream fs2 = File.OpenRead(name2);
byte[] content2 = new byte[fs2.Length];
fs2.Read(content2, 0, content2.Length);
fs2.Close();

FileStream fs3 = File.OpenRead(name3);
byte[] content3 = new byte[fs3.Length];
fs3.Read(content3, 0, content3.Length);
fs3.Close();

string strcon;
strcon = "server='10.30.42.166';database='cyunicom';uid='sa';pwd='lining1983';";
SqlConnection conn = new SqlConnection(strcon);
SqlConnection conn1 = new SqlConnection(strcon);
SqlConnection conn2 = new SqlConnection(strcon);
SqlConnection conn3 = new SqlConnection(strcon);

SqlCommand cmd = conn.CreateCommand();
SqlCommand cmd1 = conn1.CreateCommand();
SqlCommand cmd2 = conn2.CreateCommand();
SqlCommand cmd3 = conn3.CreateCommand();
conn.Open();
conn1.Open();
conn2.Open();
conn3.Open();
cmd.CommandText = "insert into Image (image_id,image_content) values('" + TextBox1.Text + "',@content)";
cmd.CommandType = CommandType.Text;
cmd1.CommandText = "insert into Image (image_content_1) values(@content1) where image_id=" + TextBox1.Text + "";
cmd1.CommandType = CommandType.Text;
cmd2.CommandText = "insert into Image (image_content_2) values(@content2) where image_id = " + TextBox1.Text + "";
cmd2.CommandType = CommandType.Text;
cmd3.CommandText = "insert into Image (image_content_3) values(@content3) where image_id = " + TextBox1.Text + "";
cmd3.CommandType = CommandType.Text;

SqlParameter para = cmd.Parameters.Add("@content", SqlDbType.Image);
para.Value = content;
cmd.ExecuteNonQuery();

SqlParameter para1 = cmd.Parameters.Add("@content1", SqlDbType.Image);
para1.Value =content1;

cmd1.ExecuteNonQuery();

//SqlParameter para2 = cmd.Parameters.Add("@content2", SqlDbType.Image);
// para2.Value =content2;
// cmd2.ExecuteNonQuery();

//SqlParameter para3 = cmd.Parameters.Add("@content3", SqlDbType.Image);
//para3.Value =content3;
//cmd3.ExecuteNonQuery();

conn.Close();
conn1.Close();
conn2.Close();
conn3.Close();
}

}

...全文
86 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
全栈极简 2016-04-30
  • 打赏
  • 举报
回复
SqlParameter [] parameters = new SqlParameter[] { new SqlParameter("@content",SqlDbType.Image), new SqlParameter("@content1",SqlDbType.Image), new SqlParameter("@content2",SqlDbType.Image), new SqlParameter("@content3",SqlDbType.Image), }; parameters[0].Value=content; parameters[1].Value=content1; parameters[2].Value=content2; parameters[3].Value=content3;
baidu_34767154 2016-04-30
  • 打赏
  • 举报
回复
是这样吗 protected void Button1_Click(object sender, EventArgs e) { string name = FileUpload1.PostedFile.FileName; string name1 = FileUpload2.PostedFile.FileName; string name2 = FileUpload3.PostedFile.FileName; string name3 = FileUpload4.PostedFile.FileName; FileStream fs = File.OpenRead(name); byte[] content = new byte[fs.Length]; fs.Read(content, 0, content.Length); fs.Close(); FileStream fs1 = File.OpenRead(name1); byte[] content1 = new byte[fs1.Length]; fs1.Read(content1, 0, content1.Length); fs1.Close(); FileStream fs2 = File.OpenRead(name2); byte[] content2 = new byte[fs2.Length]; fs2.Read(content2, 0, content2.Length); fs2.Close(); FileStream fs3 = File.OpenRead(name3); byte[] content3 = new byte[fs3.Length]; fs3.Read(content3, 0, content3.Length); fs3.Close(); string strcon; strcon = "server='10.30.42.166';database='cyunicom';uid='sa';pwd='lining1983';"; SqlConnection conn = new SqlConnection(strcon); SqlCommand cmd = conn.CreateCommand(); conn.Open(); cmd.CommandText = "insert into Image (image_id,image_content,image_content_1,image_content_2,image_content_3) values('" + TextBox1.Text + "',@content,@content1,@content2,@content3)"; cmd.CommandType = CommandType.Text; SqlParameter para = cmd.Parameters.Add("@content", SqlDbType.Image); para = cmd.Parameters.Add("@content1", SqlDbType.Image); para = cmd.Parameters.Add("@content2", SqlDbType.Image); para = cmd.Parameters.Add("@content3", SqlDbType.Image); cmd.Parameters["@content"].Value = content; cmd.Parameters["@content1"].Value = content1; cmd.Parameters["@content2"].Value = content2; cmd.Parameters["@content3"].Value = content3; cmd.ExecuteNonQuery();
全栈极简 2016-04-30
  • 打赏
  • 举报
回复
不需要写这么多重复的数据链接和执行对象,一个方法有3个byte数组的参数(文件字节流),最后一次性插入。

62,046

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

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