62,046
社区成员
发帖
与我相关
我的任务
分享
StringBuilder strSql = new StringBuilder();
strSql.Append("insert into [User](");
strSql.Append("LoginName,Password,Name,Role,IsEnlable,CreatedTime)");
strSql.Append(" values (");
strSql.Append("@LoginName,@Password,@Name,@Role,@IsEnlable,@CreatedTime)");
strSql.Append(";select @@IDENTITY");
SqlParameter[] parameters = {
new SqlParameter("@LoginName", SqlDbType.VarChar,20),
new SqlParameter("@Password", SqlDbType.VarChar,32),
new SqlParameter("@Name", SqlDbType.VarChar,10),
new SqlParameter("@Role", SqlDbType.VarChar,10),
new SqlParameter("@IsEnlable", SqlDbType.Bit,1),
new SqlParameter("@CreatedTime", SqlDbType.DateTime)};
parameters[0].Value = model.LoginName;
parameters[1].Value = model.Password;
parameters[2].Value = model.Name;
parameters[3].Value = model.Role;
parameters[4].Value = model.IsEnlable;
parameters[5].Value = model.CreatedTime;
object obj = DbHelperSQL.GetSingle(strSql.ToString(), parameters);
if (obj == null)
{
return 1;
}
else
{
return Convert.ToInt32(obj);
}
//实例化Connection对象
SqlConnection connection = new SqlConnection("Data Source=(local);Initial Catalog=AspNetStudy;Persist Security Info=True;User ID=sa;Password=sa");
//实例化Command对象
SqlCommand command = new SqlCommand("select * from UserInfo where sex=@sex and age>@age", connection);
//第一种添加查询参数的例子
command.Parameters.AddWithValue("@sex", true);
//第二种添加查询参数的例子
SqlParameter parameter = new SqlParameter("@age", SqlDbType.Int);
//注意UserInfo表里age字段是int类型的
parameter.Value = 30;
command.Parameters.Add(parameter);
//添加参数
//实例化DataAdapter
SqlDataAdapter adapter = new SqlDataAdapter(command); DataTable data = new DataTable();
//实例化Connection对象 SqlConnection connection = new SqlConnection("Data Source=(local);Initial Catalog=AspNetStudy;Persist Security Info=True;User ID=sa;Password=sa"); //实例化Command对象 SqlCommand command = new SqlCommand("select * from UserInfo where sex=@sex and age>@age", connection); //第一种添加查询参数的例子 command.Parameters.AddWithValue("@sex", true); //第二种添加查询参数的例子 SqlParameter parameter = new SqlParameter("@age", SqlDbType.Int);//注意UserInfo表里age字段是int类型的 parameter.Value = 30; command.Parameters.Add(parameter);//添加参数 //实例化DataAdapter SqlDataAdapter adapter = new SqlDataAdapter(command); DataTable data = new DataTable();