SqlParameter
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);
}
}

  

 

SqlParameter 2的更多相关文章

  1. SQL参数化查询自动生成SqlParameter列表

    string sql = @"INSERT INTO stu VALUES (@id,@name) "; 参数化查询是经常用到的,它可以有效防止SQL注入.但是需要手动去匹配参数@ ...

  2. SqlParameter的感悟

    1.在更新DataTable或是DataSet时,如果不采用SqlParameter,那么当输入的Sql语句出现歧义时,如字符串中含有单引号,程序就会发生错误,并且他人可以轻易地通过拼接Sql语句来进 ...

  3. Ibatis.net总是报:【ExecuteStoreCommand SqlParameterCollection 中已包含 SqlParameter】(转)

    今天很奇怪调用EF的ExecuteStoreCommand 出现了个这样的错误,怎么也调试不过,痛定思痛 原来 command被连着调用了而没有销毁掉   public static DataTabl ...

  4. Send SqlParameter to Dapper

    Question: I' using Dapper in my project. I have a list of SqlParameters and I want to send it to Dap ...

  5. Ado.net 三[SQL注入,DataAdapter,sqlParameter,DataSet]

    1.SQL注入:SQL注入攻击是web应用程序的一种安全漏洞,可以将不安全的数据提交给运用程序,使应用程序在服务器上执行不安全的sql命令.使用该攻击可以轻松的登录运用程序. 例如:该管理员账号密码为 ...

  6. 关于SqlParameter设定size和value

    1.设定size 字段定义:column [Text] nvarchar(max) NULL 设定字段最大长度: new SqlParameter("@Text",SqlDbTyp ...

  7. SqlParameter设定value为0却变成null

    直接MSDN:http://msdn.microsoft.com/zh-cn/library/0881fz2y(VS.80).aspx 当在 value 参数中指定 Object 时,SqlDbTyp ...

  8. sqlhelper sqlparameter 实现增删改查

    这是sqlHelper.cs类,类内里封装了方法 using System; using System.Collections.Generic; using System.Linq; using Sy ...

  9. 用SqlParameter 给SQL传递参数

    1.数据访问层 using的用法: 01.可以using System;导命名控空间 02.using 的语法结构 using(变量类型  变量名 =new 变量类型()) { } 案例: 03.us ...

  10. SqlParameter中的size

    SqlParameter中size对于需要指定大小的数据库中的数据类型参数有影响[如nvarchar],如果对于这些类型没有指定size则会默认根据赋的值进行推导应该指定的size,而对于那些大小固定 ...

随机推荐

  1. Css3中拖拽效果的实例(带有注释~欢迎指教)

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...

  2. 从golang-gin-realworld-example-app项目学写httpapi (一)

    https://wangzitian0.github.io/2013/06/29/zero-to-one-1/ https://github.com/gothinkster/golang-gin-re ...

  3. leetcode coding base

    1. Contains Duplicate 2. Contains Duplicate II 3. Contains Duplicate III

  4. CSV输入输出

    读取csv文件: import csv cf = open('D:\pywe.csv','rb') cf.readline() #读取标题行,光标移动到下一行(相当于调过标题行) for l in c ...

  5. 计算机作业(HTML简单框架网页制作) 物联网 王罗红

  6. 沉淀,再出发:Java基础知识汇总

    沉淀,再出发:Java基础知识汇总 一.前言 不管走得多远,基础知识是最重要的,这些知识就是建造一座座高楼大厦的基石和钢筋水泥.对于Java这门包含了编程方方面面的语言,有着太多的基础知识了,从最初的 ...

  7. August 09th 2017 Week 32nd Wednesday

    Find hope from despair, life will become brilliant. 从绝望中寻找希望,人生终将辉煌. Have you ever seen the movie Ba ...

  8. npm WARN network …… request to https://cnpmjs.…… failed, reason: socket hang up

    出现类似问题的原因是由于之前配置了镜像导致的 解决方案:删掉镜像,使用npm本身进行安装 删除镜像的命令: 方法1: npm config delete registry 方法2: npm confi ...

  9. HTTP协议图--HTTP 工作过程

                  HTTP请求响应模型 HTTP通信机制是在一次完整的 HTTP 通信过程中,客户端与服务器之间将完成下列7个步骤: 建立 TCP 连接 在HTTP工作开始之前,客户端首先要 ...

  10. Python2.7 - IMOOC - 1

    第一章 课程介绍 python 特点:优雅.明确.简单 python适合的领域:1.Web网站和各种网络服务:2.系统工具和脚本:3.作为"胶水"语言把其他语言开发的模块包装起来方 ...