将图像(字节)插入 3 层数据库
insert image (byte) to DB with 3-tier
我想在 UI 层中使用程序和 3 层对接错误在数据库中插入图片!
我在我的项目中有一个过程,数据访问层和业务逻辑层......
在 Btn_Insert 代码中,输入值发送到参数...然后发送到 SP !
这个存储过程:
ALTER PROCEDURE [dbo].[Agent_Insert]
@Agent NVARCHAR(MAX),
@Mob NVARCHAR(MAX),
@Pic IMAGE,
@Toz NVARCHAR(MAX)
AS
BEGIN
INSERT INTO Agent(Agent, Mob, Pic, Toz)
VALUES (@Agent, @Mob, @Pic, @Toz)
END
此 DAL 代码:
public int Agent_Insert(string Agent, string Mob, byte Pic, string Toz)
{
var returnValue = 0;
using (SqlConnection conn = new SqlConnection(connStr))
{
using (SqlCommand dCmd = new SqlCommand("Agent_Insert", conn))
{
dCmd.CommandType = CommandType.StoredProcedure;
SqlParameter[] prms = new SqlParameter[4];
prms[0] = new SqlParameter("@Agent", SqlDbType.NVarChar);
prms[0].Value = Agent;
prms[1] = new SqlParameter("@Mob", SqlDbType.NVarChar);
prms[1].Value = Mob;
prms[2] = new SqlParameter("@Pic", SqlDbType.Image);
prms[2].Value = Pic;
prms[3] = new SqlParameter("@Toz", SqlDbType.NVarChar);
prms[3].Value = Toz;
dCmd.Parameters.AddRange(prms);
conn.Open();
returnValue = dCmd.ExecuteNonQuery();
conn.Close();
}
}
return returnValue;
}
此 BLL 代码:
public class Agent_Bll
{
public int Insert(string Agent, string Mob, byte Pic, string Toz)
{
return new Agent_Dal().Agent_Insert(Agent, Mob, Pic, Toz);
}
public int Update(int Id, string Agent, string Mob, byte Pic, string Toz)
{
return new Agent_Dal().Agent_Update(Id, Agent, Mob, Pic, Toz);
}
public int Delete(int Id)
{
return new Agent_Dal().Agent_Delete(Id);
}
public DataTable Select()
{
return new Agent_Dal().Agent_Select();
}
}
这是我的代码形式:
public static byte[] ImageToByte(Image img)
{
ImageConverter converter = new ImageConverter();
return (byte[])converter.ConvertTo(img, typeof(byte[]));
}
private void Btn_Insert_Click(object sender, EventArgs e)
{
var result = 0;
try
{
result = new Agent_Bll().Insert(Txt_Agent.Text.Trim(), Txt_Mob.Text.Trim(), pictureBox1.Image(ImageToByte), Txt_Toz.Text.Trim());
对接错误:(无字节)
pictureBox1.Image(ImageToByte)
在 DAL 和 BLL 方法中放置近字节 ...[] !
byte[]
我想在 UI 层中使用程序和 3 层对接错误在数据库中插入图片! 我在我的项目中有一个过程,数据访问层和业务逻辑层...... 在 Btn_Insert 代码中,输入值发送到参数...然后发送到 SP !
这个存储过程:
ALTER PROCEDURE [dbo].[Agent_Insert]
@Agent NVARCHAR(MAX),
@Mob NVARCHAR(MAX),
@Pic IMAGE,
@Toz NVARCHAR(MAX)
AS
BEGIN
INSERT INTO Agent(Agent, Mob, Pic, Toz)
VALUES (@Agent, @Mob, @Pic, @Toz)
END
此 DAL 代码:
public int Agent_Insert(string Agent, string Mob, byte Pic, string Toz)
{
var returnValue = 0;
using (SqlConnection conn = new SqlConnection(connStr))
{
using (SqlCommand dCmd = new SqlCommand("Agent_Insert", conn))
{
dCmd.CommandType = CommandType.StoredProcedure;
SqlParameter[] prms = new SqlParameter[4];
prms[0] = new SqlParameter("@Agent", SqlDbType.NVarChar);
prms[0].Value = Agent;
prms[1] = new SqlParameter("@Mob", SqlDbType.NVarChar);
prms[1].Value = Mob;
prms[2] = new SqlParameter("@Pic", SqlDbType.Image);
prms[2].Value = Pic;
prms[3] = new SqlParameter("@Toz", SqlDbType.NVarChar);
prms[3].Value = Toz;
dCmd.Parameters.AddRange(prms);
conn.Open();
returnValue = dCmd.ExecuteNonQuery();
conn.Close();
}
}
return returnValue;
}
此 BLL 代码:
public class Agent_Bll
{
public int Insert(string Agent, string Mob, byte Pic, string Toz)
{
return new Agent_Dal().Agent_Insert(Agent, Mob, Pic, Toz);
}
public int Update(int Id, string Agent, string Mob, byte Pic, string Toz)
{
return new Agent_Dal().Agent_Update(Id, Agent, Mob, Pic, Toz);
}
public int Delete(int Id)
{
return new Agent_Dal().Agent_Delete(Id);
}
public DataTable Select()
{
return new Agent_Dal().Agent_Select();
}
}
这是我的代码形式:
public static byte[] ImageToByte(Image img)
{
ImageConverter converter = new ImageConverter();
return (byte[])converter.ConvertTo(img, typeof(byte[]));
}
private void Btn_Insert_Click(object sender, EventArgs e)
{
var result = 0;
try
{
result = new Agent_Bll().Insert(Txt_Agent.Text.Trim(), Txt_Mob.Text.Trim(), pictureBox1.Image(ImageToByte), Txt_Toz.Text.Trim());
对接错误:(无字节)
pictureBox1.Image(ImageToByte)
在 DAL 和 BLL 方法中放置近字节 ...[] !
byte[]