📄 picturedb.cs
字号:
using System;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
namespace Example_11_5
{
/// <summary>
/// Summary description for PictureDB.
/// </summary>
public class PictureDB
{
private readonly string SQLCONNECTIONSTRING = ConfigurationSettings.AppSettings["SQLCONNECTIONSTRING"].ToString();
public int AddPicture(String sPictureDesn,byte[] bPictureData,String sPictureType)
{
//定义数据库的Connection and Command
SqlConnection myConnection = new SqlConnection(SQLCONNECTIONSTRING);
SqlCommand myCommand = new SqlCommand("Pr_AddPicture",myConnection);
//定义访问数据库的方式为存储过程
myCommand.CommandType = CommandType.StoredProcedure;
//创建访问数据库的参数
SqlParameter parameterPictureDesn = new SqlParameter("@PictureDesn",SqlDbType.VarChar,50);
parameterPictureDesn.Value = sPictureDesn;
myCommand.Parameters.Add(parameterPictureDesn);
SqlParameter parameterPictureData = new SqlParameter("@PictureData",SqlDbType.Image);
parameterPictureData.Value = bPictureData;
myCommand.Parameters.Add(parameterPictureData);
SqlParameter parameterPictureType = new SqlParameter("@PictureType",SqlDbType.VarChar,50);
parameterPictureType.Value = sPictureType;
myCommand.Parameters.Add(parameterPictureType);
SqlParameter parameterPictureID = new SqlParameter("@PictureID",SqlDbType.Int,4);
parameterPictureID.Direction = ParameterDirection.ReturnValue;
myCommand.Parameters.Add(parameterPictureID);
try
{
//打开数据库的连接
myConnection.Open();
}
catch(Exception ex)
{
throw new Exception("数据库连接失败!",ex);
}
try
{
//执行数据库的存储过程(访问数据库)
myCommand.ExecuteNonQuery();
}
catch(Exception ex)
{
throw new Exception(ex.Message,ex);
}
finally
{
if (myConnection.State == ConnectionState.Open)
{
//关闭数据库的连接
myConnection.Close();
}
}
return (int)parameterPictureID.Value;
}
public SqlDataReader GetSinglePicture(int nPictureID)
{
//定义数据库的Connection and Command
SqlConnection myConnection = new SqlConnection(SQLCONNECTIONSTRING);
SqlCommand myCommand = new SqlCommand("Pr_GetSinglePicture",myConnection);
//定义访问数据库的方式为存储过程
myCommand.CommandType = CommandType.StoredProcedure;
//添加储存过程的参数
SqlParameter parameterPictureID = new SqlParameter("@PictureID",SqlDbType.Int,4);
parameterPictureID.Value = nPictureID;
myCommand.Parameters.Add(parameterPictureID);
SqlDataReader dr = null;
try
{
//打开数据库的连接
myConnection.Open();
}
catch(Exception ex)
{
throw new Exception("数据库连接失败!",ex);
}
try
{
//执行数据库的存储过程(访问数据库)
dr = myCommand.ExecuteReader(CommandBehavior.CloseConnection);
}
catch(Exception ex)
{
throw new Exception(ex.Message,ex);
}
//返回 dr
return dr;
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -