C#代码详解
发布网友
发布时间:2022-04-12 08:56
我来回答
共3个回答
热心网友
时间:2022-04-12 10:25
public static DepartmentDetails GetDepartmentDetails(string departmentId)
{
//创建一个命令对象
DbCommand comm = GenericData.CreateCommand();
//命令对象的文本
comm.CommandText = "GetDepartmentDetails";
//创建参数对象
DbParameter param = comm.CreateParameter();
//"@ID" 带@证明他是数据库里面的存储过程内的一个参数
param.ParameterName = "@ID";
//参数的值
param.Value = departmentId;
//参数的数据类型
param.DbType = DbType.Int32;
//添加到参数集合
comm.Parameters.Add(param);
//执行查询
DataTable table = GenericDataAccess.ExecuteSelectCommand(comm);
//实例化对象 准备接受返回结果
DepartmentDetails details = new DepartmentDetails();
//是否有数据
if (table.Rows.Count > 0)
{
//找到名字
details.Name = table.Rows[0]["Name"].ToString();
//找到Description
details.Description = table.Rows[0]["Description"].ToString();
}
//返回结果
return details;
}
其中:
public struct CategoryDetails
{
public int Id;
public string Name;
public string Description;
}
GenericData.CreateCommand():
public static DbCommand CreateCommand()
{
//数据库名字吧好像
string dataProviderName = BalloonShopConfiguration.DbProviderName;
//连接字符串
string connectionString = BalloonShopConfiguration.DbConnectionString;
//接口
DbProviderFactory factory = DbProviderFactories.GetFactory(dataProviderName);
//利用接口创建一个连接
DbConnection conn = factory.CreateConnection();
//连接里面的连接字符串
conn.ConnectionString = connectionString;
//利用接口创建一个命令对象
DbCommand comm = conn.CreateCommand();
//命令对象的类型为执行存储过程
comm.CommandType = CommandType.StoredProcere;
//返回
return comm;
}
热心网友
时间:2022-04-12 11:43
public static DbCommand CreateCommand()
{
string dataProviderName = BalloonShopConfiguration.DbProviderName;//数据库驱动类型。区分数据库类型
string connectionString = BalloonShopConfiguration.DbConnectionString;//连接字符串
DbProviderFactory factory = DbProviderFactories.GetFactory(dataProviderName);//实例化工厂
DbConnection conn = factory.CreateConnection();//工厂创建连接
conn.ConnectionString = connectionString;///....
DbCommand comm = conn.CreateCommand();//...命令行赋值
comm.CommandType = CommandType.StoredProcere;//执行类似为存储。。
return comm;
}
DbParameter 就是参数。不过我咋没看见在哪呢?
DbParameter param = comm.CreateParameter();//创建一个参数
param.ParameterName = "@ID";//参数对应的参数名
param.Value = departmentId;//参数值
param.DbType = DbType.Int32;//参数类型
comm.Parameters.Add(param);//添加到参数集合。。
那个不是变量,为方法传参
最后就是参数的值了