c#中用Sqlparameter的两种用法

  

新建一个表:

        创建表abc   (   int id标识(1,1)非空,   名字nvarchar (100),   性nvarchar (10)   )   插入abc值(asf,“男”)   插入abc值(“人工智能”,“女”)      

创建表格完成。

  

新建一个存储过程:

        创建过程selbyid   (   @ id int,   @thename nvarchar(100)的输出   )   作为   选择@thename=名称从abc id=@ id      

在执行的过程中可以用sqlparameter的几种格式来调用存储过程:

  

<强>第一种是:

        公共字符串connString=ConfigurationManager.ConnectionStrings [“connStr”] .ConnectionString;//存储链接字符串,方便资源复用。   公共SqlConnection getcon ()   {   SqlConnection康涅狄格州=new SqlConnection ();   conn.ConnectionString=connString;   返回康涅狄格州;   }   私人空间btnsqlparauseing_Click(对象发送方,EventArgs e)   {   SqlConnection con=getcon ();   con.Open ();   字符串sqlstr="插入abc值(@ name, @sex)”;//免除sql注入攻击   SqlCommand cmd=new SqlCommand ();   cmd。连接=反对;   cmd。CommandText=sqlstr;   SqlParameter对位=new SqlParameter ();//声明参数   帕拉=new SqlParameter (“@ name”, 100年SqlDbType.NVarChar);//生成一个名字为@ id的参数,必须以@开头表示是添加的参数,并设置其类型长度,类型长度与数据库中对应字段相同,但是不能超出数据库字段大小的范围,否则报的错。   帕拉。.Trim Value=https://www.yisu.com/zixun/txtname.Text.ToString () ();//这个是输入参数,所以可以赋值。   cmd.Parameters.Add (para);//参数增加到cmd中。   帕拉=new SqlParameter (”SqlDbType @sex”。NVarChar (10);   帕拉。.Trim Value=https://www.yisu.com/zixun/txtsex.Text.ToString () ();   cmd.Parameters.Add (para);   int i=cmd.ExecuteNonQuery ();//执行sql语句,并且返回影响的行数。   MessageBox.Show (i.ToString() +”命令完成行受影响插入成功”,“提示”,MessageBoxButtons.OK, MessageBoxIcon.Information);   con.Close ();   }      

<强> 2。第二种是调用sqlparameter几种方式来调用存储过程:

  

1。         私人空间btnshuchu_Click(对象发送方,EventArgs e)   {   SqlConnection con=getcon ();   con.Open ();   SqlCommand cmd=new SqlCommand ();   cmd。连接=反对;   cmd。CommandText=" selbyid”;//存储过程的名称   cmd。CommandType=CommandType.StoredProcedure;//说明是存储过程   SqlParameter对位=new SqlParameter ();//声明sqlparameter参数   帕拉=new SqlParameter (“@ id”, SqlDbType.Int);//这个参数是输入参数   帕拉。.Trim Value=https://www.yisu.com/zixun/int.Parse (txtid.Text.ToString () ());//因为是输入参数所以可以赋值   cmd.Parameters.Add (para);//加入cmd中   帕拉=new SqlParameter (“@thename”, 100年SqlDbType.NVarChar);//参数的大小可以小于数据库的参数规定值,但不能够大于数据库的参数大小。   cmd.Parameters.Add (para);//和下面一句不可掉乱,先增加再指明它是输出参数来的。   cmd.Parameters (“@thename”)。方向=ParameterDirection.Output;//增加后,用输出说明是输出参数。   int i=cmd.ExecuteNonQuery ();   字符串名称=cmd.Parameters (“@thename”) .Value.ToString ();//经过执行,存储过程返回了输出参数。   对话框。显示(“命令完成“+名字+”是所查记录”、“提示”,MessageBoxButtons。好的,MessageBoxIcon.Information);   con.Close ();   }      

套路就是:输出参数先声明,再赋值,再加入cmd的参数中,最后用cmd.ExecuteNonQuery()执行。

  

2。用AddWithValue:

        私人空间btnothers_Click(对象发送方,EventArgs e)   {   SqlConnection con=getcon ();   SqlCommand cmd=new SqlCommand ();   cmd。连接=反对;   cmd。CommandText=" selbyid”;   cmd。CommandType=CommandType.StoredProcedure;   SqlParameter对位=new SqlParameter ();   cmd.Parameters。AddWithValue (“@ id”, Convert.ToInt32 (txtid.Text.Trim()));//输入参数可以用AddWithValue来格式化参数,但输出参数只能用添加   cmd.Parameters。添加(“SqlDbType.NVarChar @thename”, 100)。方向=ParameterDirection.Output;//和下面一句不可顺序掉乱,否则会报的错,先加入cmd中再指明它是输出参数来的。   con.Open ();   int i=cmd.ExecuteNonQuery ();   字符串名称=cmd.Parameters (“@thename”) .Value.ToString ();//输出参数返回一个数的值。   对话框。显示(“命令完成“+名字+”是所查记录”、“提示”,MessageBoxButtons。好的,MessageBoxIcon.Information);   con.Close ();   }

c#中用Sqlparameter的两种用法