DataCommand를 사용하여 단일 값을 반환하는 저장 프로시저를 실행하려면

 

SqlConnection sqlConnection1 = new SqlConnection("Your Connection String");
SqlCommand cmd = new SqlCommand();
Object returnValue;

cmd.CommandText = "StoredProcedureName";
cmd.CommandType = CommandType. StoredProcedure;
cmd.Connection = sqlConnection1;

sqlConnection1.Open();

returnValue = cmd.ExecuteScalar();

sqlConnection1.Close();


 

 

 

            SqlConnection conn=new SqlConnection(strConn);

            SqlCommand cmd=new SqlCommand("usp_거래처상호탐색",conn);

            conn.Open();

 

            cmd.CommandType=CommandType.StoredProcedure;

            //input거래처코드

            cmd.Parameters.Add("@거래처코드",SqlDbType.VarChar,10);

            cmd.Parameters["@거래처코드"].Value=txt거래처코드.Text;

            cmd.Parameters["@거래처코드"].Direction=ParameterDirection.Input;

 

            //out put 상호

            cmd.Parameters.Add("@상호",SqlDbType.VarChar,30);

            cmd.Parameters["@상호"].Direction=ParameterDirection.Output;

 

            //리턴값

            SqlParameter param=cmd.Parameters.Add("@리턴값",SqlDbType.Int);

            param.Direction=ParameterDirection.ReturnValue;

 

            cmd.ExecuteNonQuery();

            textBox1.Text=cmd.Parameters["@상호"].Value.ToString();

            textBox2.Text=cmd.Parameters["@리턴값"].Value.ToString();

+ Recent posts