C# 中用 Sqlparameter 的两种用法
https://www.jb51.net/article/146754.htm
sql新建一个表:
create table abc
(
id int IDENTITY(1,1) NOT NULL,
name nvarchar(100) ,
sex nvarchar(10)
)
insert into abc values(‘asf','男')
insert into abc values(‘ai','女')
创建表格完成。
sql新建一个存储过程:
create procedure selbyid
(
@id int,
@thename nvarchar(100) output
)
as
select @thename= name from abc where id=@id
c#
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Sqlparameter_test.aspx.cs" Inherits="Sqlparameter_test" %><!DOCTYPE html><html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/><title></title>
</head>
<body><form id="form1" runat="server"><div><asp:Button ID="btnsqlparauseing" runat="server" Text="Button" OnClick="btnsqlparauseing_Click" /><asp:textbox runat="server" ID="txtname"></asp:textbox><asp:textbox runat="server" ID="txtsex"></asp:textbox>
返回值为1 证明添加成功<asp:textbox runat="server" ID="messagebox"></asp:textbox></div></form>
</body>
</html>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;public partial class Sqlparameter_test : System.Web.UI.Page
{public string conString = ConfigurationManager.ConnectionStrings["longyuanConnectionString"].ConnectionString;public SqlConnection gecon(){SqlConnection conn = new SqlConnection();conn.ConnectionString = conString;return conn;}protected void Page_Load(object sender, EventArgs e){}protected void btnsqlparauseing_Click(object sender, EventArgs e){SqlConnection con = gecon();con.Open();string sqlstr = "insert into abc values(@name,@sex)";SqlCommand cmd = new SqlCommand();cmd.Connection = con;cmd.CommandText = sqlstr;SqlParameter para = new SqlParameter();para = new SqlParameter("@name", SqlDbType.NVarChar, 10);para.Value = txtname.Text.Trim();cmd.Parameters.Add(para);para = new SqlParameter("@sex", SqlDbType.NVarChar, 10);para.Value = txtsex.Text.ToString().Trim();cmd.Parameters.Add(para);int i = cmd.ExecuteNonQuery(); //执行sql语句,并且返回影响的行数。Response.Write("<script>alert(\"原因:" + i + "\");</script>");con.Close();}
}
C# 中用 Sqlparameter 的两种用法相关推荐
- C# 中用 Sqlparameter 的几种用法
新建一个表: create table abc ( id int IDENTITY(1,1) NOT NULL, name nvarchar(100) , sex nvarc ...
- C++ operator两种用法【转】
C++中的operator,有两种用法,一种是operator overloading(操作符重载),一种是operator casting(操作隐式转换).下面分别进行介绍: 1.operator ...
- html如何让a标签提交表单提交,html post请求之a标签的两种用法解析
这篇文章主要介绍了html post请求之a标签的两种用法,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友参考下吧 html post请求之a标签的两种用法 ...
- html post举例,html post请求之a标签的两种用法举例
html post请求之a标签的两种用法举例 1.使用ajax来发起POST请求 HTML代码如下:发起POST请求a> JQuery代码如下:$(".a_post").on ...
- java sort 第二个参数_详解java Collections.sort的两种用法
Collections是一个工具类,sort是其中的静态方法,是用来对List类型进行排序的,它有两种参数形式: public static > void sort(List list) { l ...
- c++ operator操作符的两种用法:重载和隐式类型转换,string转其他基本数据类型的简洁实现string_cast...
C++中的operator主要有两个作用,一是操作符的重载,一是自定义对象类型的隐式转换.对于操作符的重载,许多人都不陌生,但是估计不少人都不太熟悉operator的第二种用法,即自定义对象类型的隐式 ...
- include virtual引入html文件,ASP文件引用include file和include virtual两种用法
在 ASP 编程中,我们经常遇到需要重复使用的模块代码,通常我们将这些模块代码保存为一个单独的文件,需要使用此模块代码时,在程序中通过 #include 指令引用此文件,这样使得工作效率和程序可读性大 ...
- python匿名函数Lambda的两种用法
python匿名函数Lambda的两种用法 第一种方式:作为函数对象单独使用 第二种方式:放在另一函数里使用 一般来说匿名函数Lambda的使用就是为了避免单独def一个函数,而且不用给函数命名,但是 ...
- operator的两种用法
C++ operator两种用法 C++中的operator,有两种用法,一种是operator overloading(操作符重载),一种是operator casting(操作隐式转换).下面分别 ...
最新文章
- windows 2008 R2无法创建性能收集器
- OCP读书笔记(10) - 使用闪回技术I
- Vue入门三、过滤器filter
- java中sleep()、wait()相同与不同详解
- Redis 的各项功能解决了哪些问题?
- socket通信入门
- 为什么女生会有体香?
- Exynos4412 Uboot 编译工具 —— 交叉工具链 arm-linux-gcc 的安装
- Linux 多核下绑定硬件中断到不同 CPU(IRQ Affinity)
- WEB安全基础-CSRF漏洞
- HTML-盒子模型(padding-margining)-样式继承-浮动
- iOS 检测网络状态
- jQueryEasyUI应用 – datagrid之CRUD应用
- goeasy小demo
- 16讲项目实战详细页制作及与首页和内页的链接添加
- Dev C++ 下载地址和安装教程
- kafka内存缓冲池机制造成的频繁GC问题
- 在word/wps插入代码+保持源代码风格+加行号
- Python可视化34|matplotlib-多子图绘制(为所欲为版)
- 知到网课大美劳动期末考试试题答案集合