加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > MsSql教程 > 正文

SqlParameter

发布时间:2020-12-12 15:35:46 所属栏目:MsSql教程 来源:网络整理
导读:string strSql = "Insert into News(TypeId,NewsCaption,NewsContent) values(@TypeId,@NewsCaption,@NewsContent)"; ???SqlParameter[] paras ={ ????new SqlParameter("@TypeId",SqlDbType.Int), ????new SqlParameter("@NewsCaption",SqlDbType.NVarChar,

string strSql = "Insert into News(TypeId,NewsCaption,NewsContent) values(@TypeId,@NewsCaption,@NewsContent)";
???SqlParameter[] paras ={
????new SqlParameter("@TypeId",SqlDbType.Int),
????new SqlParameter("@NewsCaption",SqlDbType.NVarChar,200),
????new SqlParameter("@NewsContent",SqlDbType.NText)
???};
???paras[0].Value = typeId;
???paras[1].Value = caption;
???paras[2].Value = content;

???int rows = new SqlHelp().ExecuteNonQuery(strSql,paras);

?

?

public SqlHelp() {
??????connString = ConfigurationManager.AppSettings["sqlServer2005DbName"];

?}

?

public int ExecuteNonQuery(string strQuery,SqlParameter[] paras) {
??int rows = 0;?//影响行数
??try {
???using (SqlConnection conn = new SqlConnection(connString)) {
????using (SqlCommand command = new SqlCommand()) {
?????PrepareCommand(conn,command,strQuery,paras);
?????rows = command.ExecuteNonQuery();
?????return rows;
????}
???}
??} catch {
???throw;
??}
?}

?

//带参数的DML操作 ?private void PrepareCommand(SqlConnection conn,SqlCommand command,string strQuery,SqlParameter[] paras) { ??if (conn.State != ConnectionState.Open) { ???conn.Open(); ??} ??command.Connection = conn; ??command.CommandText = strQuery; ??command.CommandType = CommandType.Text; ??if (paras != null) { ???foreach (SqlParameter parm in paras) ????command.Parameters.Add(parm); ??} ?}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读