一个小案例

ContractedBlock.gif ExpandedBlockStart.gif 代码

   
create table tb_login
(
lid
int identity( 1 , 1 ) primary key,
lName nvarchar(
30 ) not null ,
lPwd nvarchar(
30 ) not null ,
lPurview
int default ( 3 )
)
go
insert into tb_login
select
' admin ' , ' admin123 ' , 2 union
select
' marry ' , ' marry123 ' ,
go

create table tb_userInfo
(
uid
int identity( 1 , 1 ) primary key,
uName nvarchar(
30 ) not null ,
uAge
int ,
uCardNum
char ( 18 ),
uDescription nvarchar(
50 )
)
go
insert into tb_userInfo
select
' 刘德华 ' , 43 , ' 123456789012345678 ' , ' 歌星,演员 ' union
select
' 任贤齐 ' 40 ' 123456789012345679 ' , ' 歌星 ' union
select
' 韩寒 ' , 29 , ' 1234567890123456780 ' , ' 作家 '
go

name
= " conString " connectionString = " Data Source=.;Initial Catalog=BRTData_guangzhou;Persist Security Info=True;User ID=sa;Password=sa "


public static string connectionString = ConfigurationManager.ConnectionStrings[ " conString " ].ConnectionString;
private SqlConnection conn;
private SqlCommand com;

// 根据条件查询
public List < userInfoModel > GetUserInfoAll()
{
List
< userInfoModel > list = new List < userInfoModel > ();
UserInfoModel userInfo;
using (conn = new SqlConnection( " conString " ))
{
if (conn.State != ConnectionState.open)
conn.open();

com
= new Sqlcommand( "" ,conn)
com.CommandType
= CommandType.storeProcedure

SqlParameter[] spt
= new SqlParameter[]
{
new SqlParameter( " @num " ,SqlDbType.Int, 4 );
...
};
// 标记输出参数
sqlPmr[ 0 ].Direction = ParameterDirection.Output;
...
com.Parameters.AddRange(sqlPmr);
// 获取输出参数
com.ExecuteNonQuery();
num
= Convert.ToInt32 (com.Parameters[ " @num " ].Value);

using (SqlDateReader sdr = com.ExecuteReader)
{
while (sdr.Read())
userInfo
= new UserInfoModel();
.....
list.add(userInfo);
}

}
return list;
}
// 插入数据
public void InsertUserInfo(userInfoModel userInfo)
{
using (conn = new SqlConnection( " conString " ))
{
if (conn.State != ConnectionState.open)
conn.open();

com
= new Sqlcommand( "" ,conn)
com.CommandType
= CommandType.storeProcedure

SqlParameter[] spt
= new SqlParameter[]
{
new SqlParameter( " @num " ,SqlDbType.Int, 4 ),
new SqlParameter( " @num1 " ,SqlDbType.Int, 4 ),

.....
};
sqlPmr[
0 ].Value = userInfo.uName;
.....
com.Parameters.AddRange(sqlPmr);

com.ExecuteNonQurey();
}
}

 

转载于:https://www.cnblogs.com/sogo/archive/2010/06/09/1755078.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值