语法注意点(原创):
1.cmd.CommandType = CommandType.Text;时,
在方法
public SqlCommand(string cmdText, SqlConnection connection);中cmdText可以是“存储过程名称”,也可以是“EXEC 存储过程名称”;
如果存储过程有参数,必须带上参数;输出参数必须用OUTPUT标识,否则返回空值;
原理:CommandType.Text指定类型是SQL文本命令,要求必须符合sql语法;而在SqlServer中存储过程可以不加“EXEC”直接使用存储过程名称执行,也可以在名称前加“EXEC”;
举例:
SqlCommand cmd = new SqlCommand(" EXEC dbo.ProTest @OrderNO,@OrderName,@RMDSC,@ID OUTPUT", conn);
(或)SqlCommand cmd = new SqlCommand("dbo.ProTest @OrderNO,@OrderName,@RMDSC,@ID OUTPUT", conn);
cmd.CommandType = CommandType.Text;
2.cmd.CommandType = CommandType.StoredProcedure;时,
在方法
public SqlCommand(string cmdText, SqlConnection connection);中cmdText只能是“存储过程名称”;
如果存储过程有参数,cmdText中也不能出现
原理:CommandType.StoredProcedure指定类型是存储过程名称,所以不能加EXEC;也不能加参数;注意:也不能在存储过程名前出现空白
举例:
SqlCommand cmd = new SqlCommand("dbo.ProTest", conn);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add("@OrderNO", SqlDbType.NVarChar);
cmd.Parameters["@OrderNO"].Value = "ADO测试单号01";--"@OrderNO"必须与存储过程定义时的参数相同,@符号必须加
cmd.Parameters.Add("@OrderName", SqlDbType.NVarChar);
cmd.Parameters["@OrderName"].Value = "ADO测试名称01";
cmd.Parameters.Add("@RMDSC", SqlDbType.NVarChar);
cmd.Parameters["@RMDSC"].Value = "ADO测试备注01";
cmd.Parameters.Add("@ID", SqlDbType.UniqueIdentifier);
cmd.Parameters["@ID"].Direction = ParameterDirection.Output;//默认input,Output标识输出参数
3.如果存储过程的参数标记为可为空,如下:
CREATE PROCEDURE [dbo].[ProTest]
@OrderNO NVARCHAR(50) ,
@OrderName NVARCHAR(50) ,
@RMDSC NVARCHAR(500) = NULL,
@ID UNIQUEIDENTIFIER OUTPUT --输出参数要用output标识
AS
......
则Parameters集合中可以不加该参数
转载部分:
1.获取Return返回值
复制代码代码如下:
//存储过程
//Create PROCEDURE MYSQL
// @a int,
// @b int
//AS
// return @a + @b
//GO
SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["LocalSqlServer"].ToString());
conn.Open();
SqlCommand MyCommand = new SqlCommand("MYSQL", conn);
MyCommand.CommandType = CommandType.StoredProcedure;
MyCommand.Parameters.Add(new SqlParameter("@a", SqlDbType.Int));
MyCommand.Parameters["@a"].Value = 10;
MyCommand.Parameters.Add(new SqlParameter("@b", SqlDbType.Int));
MyCommand.Parameters["@b"].Value = 20;
MyCommand.Parameters.Add(new SqlParameter("@return", SqlDbType.Int));
MyCommand.Parameters["@return"].Direction = ParameterDirection.ReturnValue;
MyCommand.ExecuteNonQuery();
Response.Write(MyCommand.Parameters["@return"].Value.ToString());
2.获取Output输出参数值
复制代码代码如下:
//存储过程
//Create PROCEDURE MYSQL
// @a int,
// @b int,
// @c int output
//AS
// Set @c = @a + @b
//GO
SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["LocalSqlServer"].ToString());
conn.Open();
SqlCommand MyCommand = new SqlCommand("MYSQL", conn);
MyCommand.CommandType = CommandType.StoredProcedure;
MyCommand.Parameters.Add(new SqlParameter("@a", SqlDbType.Int));
MyCommand.Parameters["@a"].Value = 20;
MyCommand.Parameters.Add(new SqlParameter("@b", SqlDbType.Int));
MyCommand.Parameters["@b"].Value = 20;
MyCommand.Parameters.Add(new SqlParameter("@c", SqlDbType.Int));
MyCommand.Parameters["@c"].Direction = ParameterDirection.Output;
MyCommand.ExecuteNonQuery();
Response.Write(MyCommand.Parameters["@c"].Value.ToString());
C#接收存储过程返回值:
复制代码代码如下:
public static int User_Add(User us)
{
int iRet;
SqlConnection conn = new SqlConnection(Conn_Str);
SqlCommand cmd = new SqlCommand("User_Add", conn);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@UName", us.UName);
cmd.Parameters.AddWithValue("@UPass", us.UPass);
cmd.Parameters.AddWithValue("@PassQuestion", us.PassQuestion);
cmd.Parameters.AddWithValue("@PassKey", us.PassKey);
cmd.Parameters.AddWithValue("@Email", us.Email);
cmd.Parameters.AddWithValue("@RName", us.RName);
cmd.Parameters.AddWithValue("@Area", us.Area);
cmd.Parameters.AddWithValue("@Address", us.Address);
cmd.Parameters.AddWithValue("@ZipCodes", us.ZipCodes);
cmd.Parameters.AddWithValue("@Phone", us.Phone);
cmd.Parameters.AddWithValue("@QQ", us.QQ);
cmd.Parameters.Add("@RETURN_VALUE", "").Direction = ParameterDirection.ReturnValue;
try
{
conn.Open();
cmd.ExecuteNonQuery();
iRet = (int)cmd.Parameters["@RETURN_VALUE"].Value;
}
catch (SqlException ex)
{
throw ex;
}
finally
{
conn.Close();
}
return iRet;
}
C#接收存储过程输出参数:
复制代码代码如下:
public static decimal Cart_UserAmount(int UID)
{
decimal iRet;
SqlConnection conn = new SqlConnection(Conn_Str);
SqlCommand cmd = new SqlCommand("Cart_UserAmount", conn);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@UID", UID);
cmd.Parameters.Add("@Amount", SqlDbType.Decimal).Direction=ParameterDirection.Output;
try
{
conn.Open();
cmd.ExecuteNonQuery();
iRet = (decimal)cmd.Parameters["@Amount"].Value;
}
catch (SqlException ex)
{
throw ex;
}
finally
{
conn.Close();
}
return iRet;
}
转载:http://www.jb51.net/article/45342.htm
1.运用Command对象
- SqlConnection con = new SqlConnection(constring);
- con.Open();
- string sql = "myproc";
- SqlCommand com = new SqlCommand(sql,con);
-
- com.CommandType = CommandType.StoredProcedure;
- SqlParameter output =
- com.Parameters.Add("@i", SqlDbType.Int);
-
- output.Direction = ParameterDirection.Output;
-
- com.ExecuteNonQuery();
-
- this.textBox1.Text = (output.Value).ToString();
-
-
- con.Close();
2.运用DataAdapter对象
- DataSet ds = new DataSet();
- string sql = "myproc";
- SqlDataAdapter da = new SqlDataAdapter(sql, con);
-
- SqlParameter accp =
- da.SelectCommand.Parameters.Add("@i", SqlDbType.Int);
-
- accp.Direction = ParameterDirection.Output;
-
- da.SelectCommand.CommandType = CommandType.StoredProcedure;
-
- da.Fill(ds);
- this.dataGridView1.DataSource = ds.Tables[0];
-
- this.textBox2.Text = da.SelectCommand.Parameters[0].Value.ToString();
转载:http://blog.csdn.net/dange_h/article/details/6426556