C# 通过将字节转换为字符串来设置图像url,并从数据库存储过程中访问其他值
存储过程:C# 通过将字节转换为字符串来设置图像url,并从数据库存储过程中访问其他值,c#,asp.net,sql-server,stored-procedures,web,C#,Asp.net,Sql Server,Stored Procedures,Web,存储过程: create procedure spGetImageId @Id int as Begin select imageData from uploadTable where ID = @Id End C#代码调用它: protected void Page_Load(object sender, EventArgs e) { string cs = ConfigurationManager.ConnectionStrings["CH
create procedure spGetImageId
@Id int
as
Begin
select imageData
from uploadTable
where ID = @Id
End
C#代码调用它:
protected void Page_Load(object sender, EventArgs e)
{
string cs = ConfigurationManager.ConnectionStrings["CHTproductionConnectionString"].ConnectionString;
using (SqlConnection con=new SqlConnection(cs))
{
SqlCommand cmd = new SqlCommand("spGetImageId", con);
cmd.CommandType = CommandType.StoredProcedure;
SqlParameter paramID = new SqlParameter()
{
ParameterName = "@Id",
Value = Request.QueryString["ID"]
};
cmd.Parameters.Add(paramID);
con.Open();
byte[] bytes = (byte[])cmd.ExecuteScalar();
string strBase64 = Convert.ToBase64String(bytes);
Image1.ImageUrl = "data:Image/png;base64," + strBase64;
}
}
它正在从数据库查看上传的图像,id正在从url读取。如果我将存储过程更改为
create procedure spGetImageId
@Id int
as
Begin
select title, description, imageData
from uploadTable
where ID = @Id
End
如何通过在此代码中添加来访问标题和说明?图像随url中id的更改而更改
这个过程正在发挥作用。我只需要访问标题和描述
cmd.ExecuteScalar
这将返回返回的第一行的第一列。如果您只需要从一个过程中获取一个内容,那么这非常有用,但在您的情况下,您需要获取多个内容
为此,需要使用SqlDataAdapter。在不重构代码的情况下,您可以执行以下操作。尽管如此,我还是建议拆分为一个用于检索数据的函数和另一个用于将数据加载到您可以使用的对象中的函数
string cs = ConfigurationManager.ConnectionStrings["CHTproductionConnectionString"].ConnectionString;
using (SqlConnection con = new SqlConnection(cs))
{
SqlDataAdapter Adapter = new SqlDataAdapter();
DataSet TempData = new DataSet();
SqlCommand cmd = new SqlCommand("spGetImageId", con);
cmd.CommandType = CommandType.StoredProcedure;
SqlParameter paramID = new SqlParameter()
{
ParameterName = "@Id",
Value = Request.QueryString["ID"]
};
cmd.Parameters.Add(paramID);
con.Open();
Adapter.SelectCommand = cmd;
Adapter.Fill(TempData);
string title = TempData.Tables[0].Rows[0]["title"].ToString();
string description = TempData.Tables[0].Rows[0]["description"].ToString();
string imageBytes = TempData.Tables[0].Rows[0]["imageData"].ToString();
string strBase64 = Convert.ToBase64String(imageBytes );
Image1.ImageUrl = "data:Image/png;base64," + strBase64;
}
尝试获取额外数据时的主要问题是
ExecuteScalar()
只返回记录集所包含的第一行的第一列。要检索更多内容,需要使用ExecuteReader()
方法,并使用DataReader、DataTable或DataAdapter遍历集合
其他变化:
- Request.QueryString返回对象中的字符串,可以为null。如果有人将字符串更改为文本,则在SQL执行时将抛出错误。我添加了“int ImageID”来包含这个
- 我添加了字符串变量,并为其他数据(title,desc)提升了它们
- 尝试/抓住/最后
- 在分配给图像控件之前,对图像数据执行空检查。标题和说明值也存在,以方便您使用
- 我用direct
cmd.Parameters.AddWithValue()方法替换了distinct Sql参数
protected void Page_Load(object sender, EventArgs e) {
string cs = ConfigurationManager.ConnectionStrings["CHTproductionConnectionString"].ConnectionString;
int ImageID;
string ImgTitle;
string ImgDesc;
string ImgBytes;
string strBase64;
if (!int.TryParse((string)Request.QueryString["ID"]), out ImageID) {
// STOP: querystring is not a number
}
else {
using (SqlConnection con=new SqlConnection(cs)) {
SqlCommand cmd = new SqlCommand("spGetImageId", con);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@Id", ImageID);
try {
con.Open();
var reader = cmd.ExecuteReader();
if (reader.Read()) {
ImgTitle = reader[0].ToString();
ImgDesc = reader[1].ToString();
ImgBytes = reader[2].ToString();
strBase64 = Convert.ToBase64String(ImgBytes);
}
}
catch (Exception ex) {
// handle exception
}
finally {
cmd.Dispose();
conn.Close();
}
}
}
if (strBase64 != null) {
Image1.ImageUrl = "data:Image/png;base64," + strBase64;
// utilize ImgTitle
// utilize ImgDesc
}
}
其他人已经给了你一个答案,但我认为把所有事情都分开应该是一个好建议 为可能需要重用的代码位创建一个方法是一个很好的实践。执行存储过程通常是合格的
public string ConnectionString
{
get
{
return ConfigurationManager.ConnectionStrings["CHTproductionConnectionString"].ConnectionString;
}
}
public DataTable GetDataTableFromSproc(string sproc, SqlParameter[] parameters)
{
using (SqlConnection con = new SqlConnection(this.ConnectionString))
using (SqlCommand cmd = new SqlCommand(sproc, con) { CommandType = CommandType.StoredProcedure })
using (SqlDataAdapter adapter = new SqlDataAdapter(cmd))
{
con.Open();
cmd.Parameters.AddRange(parameters);
DataTable dt = new DataTable();
adapter.Fill(dt);
return dt;
}
}
然后,您就可以像这样调用一个可重用的方法
protected void Page_Load(object sender, EventArgs e)
{
SqlParameter[] parameters = new SqlParameter[1];
parameters[0] = new SqlParameter("@Id", SqlDbType.Int) { Value = Convert.ToInt32(Request.QueryString["ID"]) };
DataTable dt = this.GetDataTableFromSproc("spGetImageId", parameters);
string title = dt.Rows[0]["title"].ToString();
string description = dt.Rows[0]["description"].ToString();
Image1.ImageUrl = "data:Image/png;base64," + Convert.ToBase64String((byte[])dt.Rows[0]["imageData"]);
}
你可能的副本必须比我快得多。